aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDhravya <[email protected]>2024-06-30 17:45:29 -0500
committerDhravya <[email protected]>2024-06-30 17:45:29 -0500
commit1d9446ef8c03aa51eafa50ee3b29230bcb50d9f3 (patch)
tree4c5de29d81f882695fb006006652a923dc655f6f
parentsave unique vectors for users from extension (diff)
downloadarchived-supermemory-1d9446ef8c03aa51eafa50ee3b29230bcb50d9f3.tar.xz
archived-supermemory-1d9446ef8c03aa51eafa50ee3b29230bcb50d9f3.zip
search endpoint
-rw-r--r--apps/cf-ai-backend/src/index.ts45
-rw-r--r--apps/extension/background.ts2
-rw-r--r--apps/web/app/(dash)/menu.tsx4
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"
/>
- <p className="opacity-0 duration-200 group-hover:opacity-100 group-focus-within:opacity-100">
+ <p className="opacity-0 duration-200 group-hover:opacity-100">
Add
</p>
</DialogTrigger>
@@ -168,7 +168,7 @@ function Menu() {
height={24}
className="hover:brightness-125 duration-200"
/>
- <p className="opacity-0 duration-200 group-hover:opacity-100 group-focus-within:opacity-100">
+ <p className="opacity-0 duration-200 group-hover:opacity-100">
{item.text}
</p>
</Link>