-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from abhishekHegde2000/test-branch
added api/chat/route
- Loading branch information
Showing
1 changed file
with
59 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 }); | ||
} | ||
} |