diff options
| author | Yash <[email protected]> | 2024-04-11 04:52:44 +0000 |
|---|---|---|
| committer | Yash <[email protected]> | 2024-04-11 04:52:44 +0000 |
| commit | 6dcc7d18c9be5e3a5e0a3ff60668424ee0158b4e (patch) | |
| tree | 179aa936536510cc707368fc7c330c4c7fbdc3f8 /apps/cf-ai-backend/src/routes | |
| parent | novel editor (diff) | |
| parent | save user ID with url to ensure that same website can be saved by users (diff) | |
| download | supermemory-new-ui.tar.xz supermemory-new-ui.zip | |
Merge branch 'main' of https://github.com/Dhravya/supermemory into new-uinew-ui
Diffstat (limited to 'apps/cf-ai-backend/src/routes')
| -rw-r--r-- | apps/cf-ai-backend/src/routes/add.ts | 52 | ||||
| -rw-r--r-- | apps/cf-ai-backend/src/routes/ask.ts | 24 | ||||
| -rw-r--r-- | apps/cf-ai-backend/src/routes/chat.ts | 77 | ||||
| -rw-r--r-- | apps/cf-ai-backend/src/routes/query.ts | 58 |
4 files changed, 114 insertions, 97 deletions
diff --git a/apps/cf-ai-backend/src/routes/add.ts b/apps/cf-ai-backend/src/routes/add.ts index 9b05e9f0..b9a6da8f 100644 --- a/apps/cf-ai-backend/src/routes/add.ts +++ b/apps/cf-ai-backend/src/routes/add.ts @@ -1,36 +1,38 @@ -import { Request } from "@cloudflare/workers-types"; -import { type CloudflareVectorizeStore } from "@langchain/cloudflare"; +import { Request } from '@cloudflare/workers-types'; +import { type CloudflareVectorizeStore } from '@langchain/cloudflare'; export async function POST(request: Request, store: CloudflareVectorizeStore) { - const body = await request.json() as { - pageContent: string, - title?: string, - description?: string, - space?: string, - url: string, - user: string + const body = (await request.json()) as { + pageContent: string; + title?: string; + description?: string; + space?: string; + url: string; + user: string; }; if (!body.pageContent || !body.url) { - return new Response(JSON.stringify({ message: "Invalid Page Content" }), { status: 400 }); + return new Response(JSON.stringify({ message: 'Invalid Page Content' }), { status: 400 }); } - const newPageContent = `Title: ${body.title}\nDescription: ${body.description}\nURL: ${body.url}\nContent: ${body.pageContent}` + const newPageContent = `Title: ${body.title}\nDescription: ${body.description}\nURL: ${body.url}\nContent: ${body.pageContent}`; - - await store.addDocuments([ - { - pageContent: newPageContent, - metadata: { - title: body.title ?? "", - description: body.description ?? "", - space: body.space ?? "", - url: body.url, - user: body.user, + await store.addDocuments( + [ + { + pageContent: newPageContent, + metadata: { + title: body.title ?? '', + description: body.description ?? '', + space: body.space ?? '', + url: body.url, + user: body.user, + }, }, + ], + { + ids: [`${body.url}-${body.user}`], }, - ], { - ids: [`${body.url}`] - }) + ); - return new Response(JSON.stringify({ message: "Document Added" }), { status: 200 }); + return new Response(JSON.stringify({ message: 'Document Added' }), { status: 200 }); } diff --git a/apps/cf-ai-backend/src/routes/ask.ts b/apps/cf-ai-backend/src/routes/ask.ts index 1c48dde8..267c1513 100644 --- a/apps/cf-ai-backend/src/routes/ask.ts +++ b/apps/cf-ai-backend/src/routes/ask.ts @@ -1,18 +1,18 @@ -import { GenerativeModel } from "@google/generative-ai"; -import { OpenAIEmbeddings } from "../OpenAIEmbedder"; -import { CloudflareVectorizeStore } from "@langchain/cloudflare"; -import { Request } from "@cloudflare/workers-types"; +import { GenerativeModel } from '@google/generative-ai'; +import { OpenAIEmbeddings } from '../OpenAIEmbedder'; +import { CloudflareVectorizeStore } from '@langchain/cloudflare'; +import { Request } from '@cloudflare/workers-types'; export async function POST(request: Request, _: CloudflareVectorizeStore, embeddings: OpenAIEmbeddings, model: GenerativeModel, env?: Env) { - const body = await request.json() as { - query: string + const body = (await request.json()) as { + query: string; }; if (!body.query) { - return new Response(JSON.stringify({ message: "Invalid Page Content" }), { status: 400 }); + return new Response(JSON.stringify({ message: 'Invalid Page Content' }), { status: 400 }); } - const prompt = `You are an agent that answers a question based on the query. don't say 'based on the context'.\n\n Context:\n${body.query} \nAnswer this question based on the context. Question: ${body.query}\nAnswer:` + const prompt = `You are an agent that answers a question based on the query. don't say 'based on the context'.\n\n Context:\n${body.query} \nAnswer this question based on the context. Question: ${body.query}\nAnswer:`; const output = await model.generateContentStream(prompt); const response = new Response( @@ -22,14 +22,14 @@ export async function POST(request: Request, _: CloudflareVectorizeStore, embedd for await (const chunk of output.stream) { const chunkText = await chunk.text(); console.log(chunkText); - const encodedChunk = converter.encode("data: " + JSON.stringify({ "response": chunkText }) + "\n\n"); + const encodedChunk = converter.encode('data: ' + JSON.stringify({ response: chunkText }) + '\n\n'); controller.enqueue(encodedChunk); } - const doneChunk = converter.encode("data: [DONE]"); + const doneChunk = converter.encode('data: [DONE]'); controller.enqueue(doneChunk); controller.close(); - } - }) + }, + }), ); return response; } diff --git a/apps/cf-ai-backend/src/routes/chat.ts b/apps/cf-ai-backend/src/routes/chat.ts index 95788d03..75e298b8 100644 --- a/apps/cf-ai-backend/src/routes/chat.ts +++ b/apps/cf-ai-backend/src/routes/chat.ts @@ -1,28 +1,28 @@ -import { Content, GenerativeModel } from "@google/generative-ai"; -import { OpenAIEmbeddings } from "../OpenAIEmbedder"; -import { CloudflareVectorizeStore } from "@langchain/cloudflare"; -import { Request } from "@cloudflare/workers-types"; +import { Content, GenerativeModel } from '@google/generative-ai'; +import { OpenAIEmbeddings } from '../OpenAIEmbedder'; +import { CloudflareVectorizeStore } from '@langchain/cloudflare'; +import { Request } from '@cloudflare/workers-types'; export async function POST(request: Request, _: CloudflareVectorizeStore, embeddings: OpenAIEmbeddings, model: GenerativeModel, env?: Env) { const queryparams = new URL(request.url).searchParams; - const query = queryparams.get("q"); - const topK = parseInt(queryparams.get("topK") ?? "5"); - const user = queryparams.get("user") - const spaces = queryparams.get("spaces") - const spacesArray = spaces ? spaces.split(",") : undefined + const query = queryparams.get('q'); + const topK = parseInt(queryparams.get('topK') ?? '5'); + const user = queryparams.get('user'); + const spaces = queryparams.get('spaces'); + const spacesArray = spaces ? spaces.split(',') : undefined; - const sourcesOnly = (queryparams.get("sourcesOnly") ?? "false") + const sourcesOnly = queryparams.get('sourcesOnly') ?? 'false'; if (!user) { - return new Response(JSON.stringify({ message: "Invalid User" }), { status: 400 }); + return new Response(JSON.stringify({ message: 'Invalid User' }), { status: 400 }); } if (!query) { - return new Response(JSON.stringify({ message: "Invalid Query" }), { status: 400 }); + return new Response(JSON.stringify({ message: 'Invalid Query' }), { status: 400 }); } const filter: VectorizeVectorMetadataFilter = { - user - } + user, + }; const responses: VectorizeMatches = { matches: [], count: 0 }; @@ -34,12 +34,12 @@ export async function POST(request: Request, _: CloudflareVectorizeStore, embedd const resp = await env!.VECTORIZE_INDEX.query(queryAsVector, { topK, - filter + filter, }); if (resp.count > 0) { - responses.matches.push(...resp.matches) - responses.count += resp.count + responses.matches.push(...resp.matches); + responses.count += resp.count; } } } else { @@ -47,13 +47,13 @@ export async function POST(request: Request, _: CloudflareVectorizeStore, embedd const resp = await env!.VECTORIZE_INDEX.query(queryAsVector, { topK, filter: { - user - } + user, + }, }); if (resp.count > 0) { - responses.matches.push(...resp.matches) - responses.count += resp.count + responses.matches.push(...resp.matches); + responses.count += resp.count; } } @@ -61,27 +61,36 @@ export async function POST(request: Request, _: CloudflareVectorizeStore, embedd // return new Response(JSON.stringify({ message: "No Results Found" }), { status: 404 }); // } - const highScoreIds = responses.matches.filter(({ score }) => score > 0.35).map(({ id }) => id) + const highScoreIds = responses.matches.filter(({ score }) => score > 0.35).map(({ id }) => id); - if (sourcesOnly === "true") { + if (sourcesOnly === 'true') { return new Response(JSON.stringify({ ids: highScoreIds }), { status: 200 }); } - const vec = await env!.VECTORIZE_INDEX.getByIds(highScoreIds) + const vec = await env!.VECTORIZE_INDEX.getByIds(highScoreIds); - const preparedContext = vec.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[] + const body = (await request.json()) as { + chatHistory?: Content[]; }; const defaultHistory = [ { - role: "user", - parts: [{ text: `You are an agent that summarizes a page based on the query. don't say 'based on the context'. I expect you to be like a 'Second Brain'. you will be provided with the context (old saved posts) and questions. Answer accordingly. Answer in markdown format` }], + role: 'user', + parts: [ + { + text: `You are an agent that summarizes a page based on the query. don't say 'based on the context'. I expect you to be like a 'Second Brain'. you will be provided with the context (old saved posts) and questions. Answer accordingly. Answer in markdown format`, + }, + ], }, { - role: "model", + role: 'model', parts: [{ text: "Ok, I am a personal assistant, and will act as a second brain to help with user's queries." }], }, ] as Content[]; @@ -100,14 +109,14 @@ export async function POST(request: Request, _: CloudflareVectorizeStore, embedd const converter = new TextEncoder(); for await (const chunk of output.stream) { const chunkText = await chunk.text(); - const encodedChunk = converter.encode("data: " + JSON.stringify({ "response": chunkText }) + "\n\n"); + const encodedChunk = converter.encode('data: ' + JSON.stringify({ response: chunkText }) + '\n\n'); controller.enqueue(encodedChunk); } - const doneChunk = converter.encode("data: [DONE]"); + const doneChunk = converter.encode('data: [DONE]'); controller.enqueue(doneChunk); controller.close(); - } - }) + }, + }), ); return response; } diff --git a/apps/cf-ai-backend/src/routes/query.ts b/apps/cf-ai-backend/src/routes/query.ts index be237d7d..cd5295c5 100644 --- a/apps/cf-ai-backend/src/routes/query.ts +++ b/apps/cf-ai-backend/src/routes/query.ts @@ -1,59 +1,65 @@ -import { GenerativeModel } from "@google/generative-ai"; -import { OpenAIEmbeddings } from "../OpenAIEmbedder"; -import { CloudflareVectorizeStore } from "@langchain/cloudflare"; -import { Request } from "@cloudflare/workers-types"; +import { GenerativeModel } from '@google/generative-ai'; +import { OpenAIEmbeddings } from '../OpenAIEmbedder'; +import { CloudflareVectorizeStore } from '@langchain/cloudflare'; +import { Request } from '@cloudflare/workers-types'; export async function GET(request: Request, _: CloudflareVectorizeStore, embeddings: OpenAIEmbeddings, model: GenerativeModel, env?: Env) { const queryparams = new URL(request.url).searchParams; - const query = queryparams.get("q"); - const topK = parseInt(queryparams.get("topK") ?? "5"); - const user = queryparams.get("user") - const space = queryparams.get("space") + const query = queryparams.get('q'); + const topK = parseInt(queryparams.get('topK') ?? '5'); + const user = queryparams.get('user'); + const space = queryparams.get('space'); - const sourcesOnly = (queryparams.get("sourcesOnly") ?? "false") + const sourcesOnly = queryparams.get('sourcesOnly') ?? 'false'; if (!user) { - return new Response(JSON.stringify({ message: "Invalid User" }), { status: 400 }); + return new Response(JSON.stringify({ message: 'Invalid User' }), { status: 400 }); } if (!query) { - return new Response(JSON.stringify({ message: "Invalid Query" }), { status: 400 }); + return new Response(JSON.stringify({ message: 'Invalid Query' }), { status: 400 }); } const filter: VectorizeVectorMetadataFilter = { - user - } + user, + }; if (space) { - filter.space + filter.space; } const queryAsVector = await embeddings.embedQuery(query); const resp = await env!.VECTORIZE_INDEX.query(queryAsVector, { topK, - filter + filter, }); if (resp.count === 0) { - return new Response(JSON.stringify({ message: "No Results Found" }), { status: 404 }); + return new Response(JSON.stringify({ message: 'No Results Found' }), { status: 404 }); } - const highScoreIds = resp.matches.filter(({ score }) => score > 0.3).map(({ id }) => id) + const highScoreIds = resp.matches.filter(({ score }) => score > 0.3).map(({ id }) => id); - if (sourcesOnly === "true") { + if (sourcesOnly === 'true') { return new Response(JSON.stringify({ ids: highScoreIds }), { status: 200 }); } - const vec = await env!.VECTORIZE_INDEX.getByIds(highScoreIds) + const vec = await env!.VECTORIZE_INDEX.getByIds(highScoreIds); if (vec.length === 0 || !vec[0].metadata) { - return new Response(JSON.stringify({ message: "No Results Found" }), { status: 400 }); + return new Response(JSON.stringify({ message: 'No Results Found' }), { status: 400 }); } - 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 + .slice(0, 3) + .map( + ({ metadata }) => + `Website title: ${metadata!.title}\nDescription: ${metadata!.description}\nURL: ${metadata!.url}\nContent: ${metadata!.text}`, + ) + .join('\n\n'); - const prompt = `You are an agent that summarizes a page based on the query. Be direct and concise, don't say 'based on the context'.\n\n Context:\n${preparedContext} \nAnswer this question based on the context. Question: ${query}\nAnswer:` + const prompt = `You are an agent that summarizes a page based on the query. Be direct and concise, don't say 'based on the context'.\n\n Context:\n${preparedContext} \nAnswer this question based on the context. Question: ${query}\nAnswer:`; const output = await model.generateContentStream(prompt); const response = new Response( @@ -62,14 +68,14 @@ export async function GET(request: Request, _: CloudflareVectorizeStore, embeddi const converter = new TextEncoder(); for await (const chunk of output.stream) { const chunkText = await chunk.text(); - const encodedChunk = converter.encode("data: " + JSON.stringify({ "response": chunkText }) + "\n\n"); + const encodedChunk = converter.encode('data: ' + JSON.stringify({ response: chunkText }) + '\n\n'); controller.enqueue(encodedChunk); } - const doneChunk = converter.encode("data: [DONE]"); + const doneChunk = converter.encode('data: [DONE]'); controller.enqueue(doneChunk); controller.close(); - } - }) + }, + }), ); return response; } |