Skip to content

Commit

Permalink
Temporarily switch to vector search
Browse files Browse the repository at this point in the history
  • Loading branch information
Mansive committed Jan 8, 2024
1 parent 04ca0b3 commit ae6084d
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions src/app/api/search/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,42 @@ export async function GET(request: NextRequest) {
}

try {
const results = await xata.db.Books.search(searchParams, {
target: ["title"],
prefix: "phrase",
fuzziness: 0,
page: { size: 35, offset: 0 },
});
// const results = await xata.db.Books.search(searchParams, {
// target: ["title"],
// prefix: "phrase",
// fuzziness: 0,
// page: { size: 35, offset: 0 },
// });

// Generate embeddings
const embeddings = await fetch("https://api.embaas.io/v1/embeddings/", {
method: "POST",
body: JSON.stringify({
texts: ["query: " + searchParams],
model: "multilingual-e5-large",
}),
headers: {
"Content-Type": "application/json",
Authorization: "Bearer " + process.env.EMBAAS_API_KEY,
},
}).then((response) => response.json());

const results = await xata.db.Books.vectorSearch(
"embeddings",
embeddings["data"][0]["embedding"],
{
similarityFunction: "cosineSimilarity",
size: 35,
}
);

// Pasted from Copilot; could probably be done better
// Reduce network payload size for users
const records = JSON.parse(JSON.stringify(results))["records"];
records.forEach((book: Record<string, any>) => {
delete book.id;
delete book.embeddings;
delete book.xata;
//delete book.xata;
console.log(book);
});

return NextResponse.json({ records });
Expand Down

0 comments on commit ae6084d

Please sign in to comment.