From efc4658a51c8a14a35fe781a5b5145006d777c9a Mon Sep 17 00:00:00 2001 From: Abhishek Hegde Date: Fri, 24 Nov 2023 20:39:13 +0530 Subject: [PATCH] added api/chat/route --- src/app/api/chat/route.ts | 59 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 src/app/api/chat/route.ts diff --git a/src/app/api/chat/route.ts b/src/app/api/chat/route.ts new file mode 100644 index 0000000..fdb2efd --- /dev/null +++ b/src/app/api/chat/route.ts @@ -0,0 +1,59 @@ +import { notesIndex } from "@/lib/db/pinecone"; +import prisma from "@/lib/db/prisma"; +import openai, { getEmbedding } from "@/lib/openai"; +import { auth } from "@clerk/nextjs"; +import { OpenAIStream, StreamingTextResponse } from "ai"; +import { ChatCompletionMessage } from "openai/resources/index.mjs"; + +export async function POST(req: Request) { + try { + const body = await req.json(); + const messages: ChatCompletionMessage[] = body.messages; + + const messagesTruncated = messages.slice(-6); + + const embedding = await getEmbedding( + messagesTruncated.map((message) => message.content).join("\n"), + ); + + const { userId } = auth(); + + const vectorQueryResponse = await notesIndex.query({ + vector: embedding, + topK: 4, + filter: { userId }, + }); + + const relevantNotes = await prisma.note.findMany({ + where: { + id: { + in: vectorQueryResponse.matches.map((match) => match.id), + }, + }, + }); + + console.log("Relevant notes found: ", relevantNotes); + + const systemMessage: ChatCompletionMessage = { + role: "system", + content: + "You are an intelligent note-taking app. You answer the user's question based on their existing notes. " + + "The relevant notes for this query are:\n" + + relevantNotes + .map((note) => `Title: ${note.title}\n\nContent:\n${note.content}`) + .join("\n\n"), + }; + + const response = await openai.chat.completions.create({ + model: "gpt-3.5-turbo", + stream: true, + messages: [systemMessage, ...messagesTruncated], + }); + + const stream = OpenAIStream(response); + return new StreamingTextResponse(stream); + } catch (error) { + console.error(error); + return Response.json({ error: "Internal server error" }, { status: 500 }); + } +}