aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDhravya <[email protected]>2024-04-10 00:11:59 -0700
committerDhravya <[email protected]>2024-04-10 00:11:59 -0700
commit338e4f378c0752e298f2a2021feda953830ddd02 (patch)
tree534747073b32c7ec3d6ae8b54a2bf65c79b8bf87
parentremoved all pnpm-lock files (diff)
downloadsupermemory-338e4f378c0752e298f2a2021feda953830ddd02.tar.xz
supermemory-338e4f378c0752e298f2a2021feda953830ddd02.zip
spaces in the API
-rw-r--r--apps/cf-ai-backend/src/routes/chat.ts50
-rw-r--r--apps/web/src/app/api/chat/route.ts4
-rw-r--r--apps/web/src/components/Main.tsx7
3 files changed, 43 insertions, 18 deletions
diff --git a/apps/cf-ai-backend/src/routes/chat.ts b/apps/cf-ai-backend/src/routes/chat.ts
index 1082998f..95788d03 100644
--- a/apps/cf-ai-backend/src/routes/chat.ts
+++ b/apps/cf-ai-backend/src/routes/chat.ts
@@ -8,7 +8,8 @@ export async function POST(request: Request, _: CloudflareVectorizeStore, embedd
const query = queryparams.get("q");
const topK = parseInt(queryparams.get("topK") ?? "5");
const user = queryparams.get("user")
- const space = queryparams.get("space")
+ const spaces = queryparams.get("spaces")
+ const spacesArray = spaces ? spaces.split(",") : undefined
const sourcesOnly = (queryparams.get("sourcesOnly") ?? "false")
@@ -19,27 +20,48 @@ export async function POST(request: Request, _: CloudflareVectorizeStore, embedd
if (!query) {
return new Response(JSON.stringify({ message: "Invalid Query" }), { status: 400 });
}
-
const filter: VectorizeVectorMetadataFilter = {
user
}
- if (space) {
- filter.space
- }
+ const responses: VectorizeMatches = { matches: [], count: 0 };
- const queryAsVector = await embeddings.embedQuery(query);
+ if (spacesArray) {
+ for (const space of spacesArray) {
+ filter.space = space;
- const resp = await env!.VECTORIZE_INDEX.query(queryAsVector, {
- topK,
- filter
- });
+ const queryAsVector = await embeddings.embedQuery(query);
+
+ const resp = await env!.VECTORIZE_INDEX.query(queryAsVector, {
+ topK,
+ filter
+ });
- if (resp.count === 0) {
- return new Response(JSON.stringify({ message: "No Results Found" }), { status: 404 });
+ if (resp.count > 0) {
+ responses.matches.push(...resp.matches)
+ responses.count += resp.count
+ }
+ }
+ } else {
+ const queryAsVector = await embeddings.embedQuery(query);
+ const resp = await env!.VECTORIZE_INDEX.query(queryAsVector, {
+ topK,
+ filter: {
+ user
+ }
+ });
+
+ if (resp.count > 0) {
+ responses.matches.push(...resp.matches)
+ responses.count += resp.count
+ }
}
- const highScoreIds = resp.matches.filter(({ score }) => score > 0.3).map(({ id }) => id)
+ // if (responses.count === 0) {
+ // return new Response(JSON.stringify({ message: "No Results Found" }), { status: 404 });
+ // }
+
+ const highScoreIds = responses.matches.filter(({ score }) => score > 0.35).map(({ id }) => id)
if (sourcesOnly === "true") {
return new Response(JSON.stringify({ ids: highScoreIds }), { status: 200 });
@@ -47,7 +69,7 @@ export async function POST(request: Request, _: CloudflareVectorizeStore, embedd
const vec = await env!.VECTORIZE_INDEX.getByIds(highScoreIds)
- const preparedContext = vec.slice(0, 3).map(({ metadata }) => `Website title: ${metadata!.title}\nDescription: ${metadata!.description}\nURL: ${metadata!.url}\nContent: ${metadata!.text}`).join("\n\n");
+ const preparedContext = vec.map(({ metadata }) => `Website title: ${metadata!.title}\nDescription: ${metadata!.description}\nURL: ${metadata!.url}\nContent: ${metadata!.text}`).join("\n\n");
const body = await request.json() as {
chatHistory?: Content[]
diff --git a/apps/web/src/app/api/chat/route.ts b/apps/web/src/app/api/chat/route.ts
index ef59fd43..aec5b0ea 100644
--- a/apps/web/src/app/api/chat/route.ts
+++ b/apps/web/src/app/api/chat/route.ts
@@ -25,6 +25,8 @@ export async function POST(req: NextRequest) {
const session = { session: sessionData[0], user: user[0] }
const query = new URL(req.url).searchParams.get("q");
+ const spaces = new URL(req.url).searchParams.get("spaces");
+
const sourcesOnly = new URL(req.url).searchParams.get("sourcesOnly") ?? "false";
const chatHistory = await req.json() as {
@@ -38,7 +40,7 @@ export async function POST(req: NextRequest) {
}
- const resp = await fetch(`https://cf-ai-backend.dhravya.workers.dev/chat?q=${query}&user=${session.user.email ?? session.user.name}&sourcesOnly=${sourcesOnly}`, {
+ const resp = await fetch(`https://cf-ai-backend.dhravya.workers.dev/chat?q=${query}&user=${session.user.email ?? session.user.name}&sourcesOnly=${sourcesOnly}&spaces=${spaces}`, {
headers: {
"X-Custom-Auth-Key": env.BACKEND_SECURITY_KEY,
},
diff --git a/apps/web/src/components/Main.tsx b/apps/web/src/components/Main.tsx
index 3d0fc18a..4e0392c9 100644
--- a/apps/web/src/components/Main.tsx
+++ b/apps/web/src/components/Main.tsx
@@ -37,6 +37,8 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) {
const textArea = useRef<HTMLDivElement>(null);
const main = useRef<HTMLDivElement>(null);
+ const [selectedSpaces, setSelectedSpaces] = useState<string[]>([]);
+
useEffect(() => {
const search = searchParams.get('q');
if (search && search.trim().length > 0) {
@@ -182,7 +184,7 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) {
ids: string[];
};
- setIsAiLoading(false)
+ setIsAiLoading(false);
setChatHistory((prev) => {
const lastMessage = prev[prev.length - 1];
return [
@@ -197,8 +199,7 @@ export default function Main({ sidebarOpen }: { sidebarOpen: boolean }) {
];
});
- // TODO: PASS THE `SPACE` TO THE API
- const response = await fetch(`/api/chat?q=${_value}`, {
+ const response = await fetch(`/api/chat?q=${_value}&spaces=${selectedSpaces.join(",")}`, {
method: 'POST',
body: JSON.stringify({
chatHistory: modifyChatHistory(chatHistory),