Skip to content

Commit

Permalink
vecor embeddings
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekHegde2000 committed Nov 24, 2023
1 parent 58976ad commit f423876
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 13 deletions.
63 changes: 50 additions & 13 deletions src/app/api/notes/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { notesIndex } from "@/lib/db/pinecone";
import prisma from "@/lib/db/prisma";
import { getEmbedding } from "@/lib/openai";
import {
createNoteSchema,
deleteNoteSchema,
Expand All @@ -25,12 +27,26 @@ export async function POST(req: Request) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}

const note = await prisma.note.create({
data: {
title,
content,
userId,
},
const embedding = await getEmbeddingForNote(title, content);

const note = await prisma.$transaction(async (tx) => {
const note = await tx.note.create({
data: {
title,
content,
userId,
},
});

await notesIndex.upsert([
{
id: note.id,
values: embedding,
metadata: { userId },
},
]);

return note;
});

return Response.json({ note }, { status: 201 });
Expand Down Expand Up @@ -65,12 +81,26 @@ export async function PUT(req: Request) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}

const updatedNote = await prisma.note.update({
where: { id },
data: {
title,
content,
},
const embedding = await getEmbeddingForNote(title, content);

const updatedNote = await prisma.$transaction(async (tx) => {
const updatedNote = await tx.note.update({
where: { id },
data: {
title,
content,
},
});

await notesIndex.upsert([
{
id,
values: embedding,
metadata: { userId },
},
]);

return updatedNote;
});

return Response.json({ updatedNote }, { status: 200 });
Expand Down Expand Up @@ -105,11 +135,18 @@ export async function DELETE(req: Request) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}

await prisma.note.delete({ where: { id } });
await prisma.$transaction(async (tx) => {
await tx.note.delete({ where: { id } });
await notesIndex.deleteOne(id);
});

return Response.json({ message: "Note deleted" }, { status: 200 });
} catch (error) {
console.error(error);
return Response.json({ error: "Internal server error" }, { status: 500 });
}
}

async function getEmbeddingForNote(title: string, content: string | undefined) {
return getEmbedding(title + "\n\n" + content ?? "");
}
15 changes: 15 additions & 0 deletions src/lib/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,18 @@ if (!apiKey) {
const openai = new OpenAI({ apiKey });

export default openai;

export async function getEmbedding(text: string) {
const response = await openai.embeddings.create({
model: "text-embedding-ada-002",
input: text,
});

const embedding = response.data[0].embedding;

if (!embedding) throw Error("Error generating embedding.");

console.log(embedding);

return embedding;
}

0 comments on commit f423876

Please sign in to comment.