From 1d9446ef8c03aa51eafa50ee3b29230bcb50d9f3 Mon Sep 17 00:00:00 2001 From: Dhravya Date: Sun, 30 Jun 2024 17:45:29 -0500 Subject: [PATCH] search endpoint --- apps/cf-ai-backend/src/index.ts | 45 +++++++++++++++++++++++++++++++++ apps/extension/background.ts | 2 +- apps/web/app/(dash)/menu.tsx | 4 +-- 3 files changed, 48 insertions(+), 3 deletions(-) diff --git a/apps/cf-ai-backend/src/index.ts b/apps/cf-ai-backend/src/index.ts index 78e1c596..2c756dcc 100644 --- a/apps/cf-ai-backend/src/index.ts +++ b/apps/cf-ai-backend/src/index.ts @@ -171,6 +171,51 @@ app.get( }, ); +app.get( + "/api/search", + zValidator("query", z.object({ query: z.string(), user: z.string() })), + async (c) => { + const { query, user } = c.req.valid("query"); + const filter: VectorizeVectorMetadataFilter = { + [`user-${user}`]: 1, + }; + + const { store } = await initQuery(c); + const queryAsVector = await store.embeddings.embedQuery(query); + + const resp = await c.env.VECTORIZE_INDEX.query(queryAsVector, { + topK: 5, + filter, + returnMetadata: true, + }); + + const minScore = Math.min(...resp.matches.map(({ score }) => score)); + const maxScore = Math.max(...resp.matches.map(({ score }) => score)); + + // This entire chat part is basically just a dumb down version of the /api/chat endpoint. + const normalizedData = resp.matches.map((data) => ({ + ...data, + normalizedScore: + maxScore !== minScore + ? 1 + ((data.score - minScore) / (maxScore - minScore)) * 98 + : 50, + })); + + const preparedContext = normalizedData.map( + ({ metadata, score, normalizedScore }) => ({ + context: `Website title: ${metadata!.title}\nDescription: ${metadata!.description}\nURL: ${metadata!.url}\nContent: ${metadata!.text}`, + score, + normalizedScore, + }), + ); + + return c.json({ + status: "ok", + response: preparedContext, + }); + }, +); + // This is a special endpoint for our "chatbot-only" solutions. // It does both - adding content AND chatting with it. app.post( diff --git a/apps/extension/background.ts b/apps/extension/background.ts index 651ddc73..3ecb26fd 100644 --- a/apps/extension/background.ts +++ b/apps/extension/background.ts @@ -13,7 +13,7 @@ const tweetToMd = (tweet: Tweet) => { const BOOKMARKS_URL = `https://x.com/i/api/graphql/xLjCVTqYWz8CGSprLU349w/Bookmarks?features=${encodeURIComponent(JSON.stringify(features))}`; -const BACKEND_URL = "http://localhost:3000"; +const BACKEND_URL = "https://beta.supermemory.ai"; // This is to prevent going over the rate limit let lastTwitterFetch = 0; diff --git a/apps/web/app/(dash)/menu.tsx b/apps/web/app/(dash)/menu.tsx index a8ba4172..cc45016e 100644 --- a/apps/web/app/(dash)/menu.tsx +++ b/apps/web/app/(dash)/menu.tsx @@ -145,7 +145,7 @@ function Menu() { height={24} className="hover:brightness-125 focus:brightness-125 duration-200 text-white" /> -

+

Add

@@ -168,7 +168,7 @@ function Menu() { height={24} className="hover:brightness-125 duration-200" /> -

+

{item.text}