added zod validation to embedQuery

This commit is contained in:
Dhravya 2024-06-12 19:52:09 -05:00
parent 778ddec27e
commit a711d1b3ee
2 changed files with 24 additions and 21 deletions

View file

@ -100,34 +100,25 @@ app.post(
const query = c.req.valid("query");
const body = c.req.valid("json");
if (body.chatHistory) {
body.chatHistory = body.chatHistory.map((i) => ({
...i,
content: i.parts
? i.parts.length > 0
? i.parts.join(" ")
: i.content
: i.content,
}));
}
const sourcesOnly = query.sourcesOnly === "true";
const spaces = query.spaces?.split(",") ?? [undefined];
console.log(spaces);
const spaces = query.spaces?.split(",") ?? [""];
// Get the AI model maker and vector store
const { model, store } = await initQuery(c, query.model);
const filter: VectorizeVectorMetadataFilter = { user: query.user };
console.log("Spaces", spaces);
// Converting the query to a vector so that we can search for similar vectors
const queryAsVector = await store.embeddings.embedQuery(query.query);
const responses: VectorizeMatches = { matches: [], count: 0 };
console.log("hello world", spaces);
// SLICED to 5 to avoid too many queries
for (const space of spaces.slice(0, 5)) {
if (space !== undefined) {
console.log("space", space);
if (space !== "") {
// it's possible for space list to be [undefined] so we only add space filter conditionally
filter.space = space;
}

View file

@ -1,3 +1,5 @@
import { z } from "zod";
interface OpenAIEmbeddingsParams {
apiKey: string;
modelName: string;
@ -32,12 +34,22 @@ export class OpenAIEmbeddings {
}),
});
const data = (await response.json()) as {
data: {
embedding: number[];
}[];
};
const data = await response.json();
return data.data[0].embedding;
const zodTypeExpected = z.object({
data: z.array(
z.object({
embedding: z.array(z.number()),
}),
),
});
const json = zodTypeExpected.safeParse(data);
if (!json.success) {
throw new Error("Invalid response from OpenAI: " + json.error.message);
}
return json.data.data[0].embedding;
}
}