Skip to content

Commit

Permalink
Merge pull request #15 from abhishekHegde2000/test-branch
Browse files Browse the repository at this point in the history
added api/chat/route
  • Loading branch information
abhishekHegde2000 authored Nov 24, 2023
2 parents 4fff40a + efc4658 commit d33ccb2
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/app/api/chat/route.ts
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 });
}
}

0 comments on commit d33ccb2

Please sign in to comment.