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/ask.ts | |
| 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/ask.ts')
| -rw-r--r-- | apps/cf-ai-backend/src/routes/ask.ts | 24 |
1 files changed, 12 insertions, 12 deletions
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; } |