aboutsummaryrefslogtreecommitdiff
path: root/apps/web/app/api
diff options
context:
space:
mode:
authorDhravya <[email protected]>2024-06-23 17:54:05 -0500
committerDhravya <[email protected]>2024-06-23 17:54:05 -0500
commit9f751ba89cc37516daa261bbae8723d5566a6602 (patch)
tree3e7533aefbf7931c966c930349263ae248cc1135 /apps/web/app/api
parentadded indexes and stuff (diff)
downloadsupermemory-9f751ba89cc37516daa261bbae8723d5566a6602.tar.xz
supermemory-9f751ba89cc37516daa261bbae8723d5566a6602.zip
feat: vector lookup and chat is twice as fast now
Diffstat (limited to 'apps/web/app/api')
-rw-r--r--apps/web/app/api/chat/route.ts24
1 files changed, 15 insertions, 9 deletions
diff --git a/apps/web/app/api/chat/route.ts b/apps/web/app/api/chat/route.ts
index d0e53066..d1730baa 100644
--- a/apps/web/app/api/chat/route.ts
+++ b/apps/web/app/api/chat/route.ts
@@ -1,5 +1,10 @@
import { type NextRequest } from "next/server";
-import { ChatHistoryZod, convertChatHistoryList } from "@repo/shared-types";
+import {
+ ChatHistory,
+ ChatHistoryZod,
+ convertChatHistoryList,
+ SourcesFromApi,
+} from "@repo/shared-types";
import { ensureAuth } from "../ensureAuth";
import { z } from "zod";
@@ -23,7 +28,11 @@ export async function POST(req: NextRequest) {
const sourcesOnly = url.searchParams.get("sourcesOnly") ?? "false";
- const chatHistory = await req.json();
+ const jsonRequest = (await req.json()) as {
+ chatHistory: ChatHistory[];
+ sources: SourcesFromApi[] | undefined;
+ };
+ const { chatHistory, sources } = jsonRequest;
if (!query || query.trim.length < 0) {
return new Response(JSON.stringify({ message: "Invalid query" }), {
@@ -31,9 +40,7 @@ export async function POST(req: NextRequest) {
});
}
- const validated = z
- .object({ chatHistory: z.array(ChatHistoryZod) })
- .safeParse(chatHistory ?? []);
+ const validated = z.array(ChatHistoryZod).safeParse(chatHistory ?? []);
if (!validated.success) {
return new Response(
@@ -45,9 +52,7 @@ export async function POST(req: NextRequest) {
);
}
- const modelCompatible = await convertChatHistoryList(
- validated.data.chatHistory,
- );
+ const modelCompatible = await convertChatHistoryList(validated.data);
const resp = await fetch(
`${process.env.BACKEND_BASE_URL}/api/chat?query=${query}&user=${session.user.id}&sourcesOnly=${sourcesOnly}&spaces=${spaces}`,
@@ -59,12 +64,13 @@ export async function POST(req: NextRequest) {
method: "POST",
body: JSON.stringify({
chatHistory: modelCompatible,
+ sources,
}),
},
);
if (sourcesOnly == "true") {
- const data = await resp.json();
+ const data = (await resp.json()) as SourcesFromApi;
return new Response(JSON.stringify(data), { status: 200 });
}