From 861ad49762bb88a90f3790607769f5180e213b3b Mon Sep 17 00:00:00 2001 From: Dhravya Date: Sun, 14 Apr 2024 00:05:12 -0700 Subject: [PATCH] added edit route --- apps/cf-ai-backend/src/routes.ts | 2 + apps/cf-ai-backend/src/routes/edit.ts | 61 +++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 apps/cf-ai-backend/src/routes/edit.ts diff --git a/apps/cf-ai-backend/src/routes.ts b/apps/cf-ai-backend/src/routes.ts index bce9aaf7..0f16b4d2 100644 --- a/apps/cf-ai-backend/src/routes.ts +++ b/apps/cf-ai-backend/src/routes.ts @@ -6,6 +6,7 @@ import * as apiChat from './routes/chat'; import * as apiBatchUploadTweets from './routes/batchUploadTweets'; import * as apiGetPageContent from './routes/getPageContent'; import * as apiDelete from './routes/delete'; +import * as apiEdit from './routes/edit'; import { OpenAIEmbeddings } from './OpenAIEmbedder'; import { GenerativeModel } from '@google/generative-ai'; import { Request } from '@cloudflare/workers-types'; @@ -32,6 +33,7 @@ routeMap.set('/chat', apiChat); routeMap.set('/batchUploadTweets', apiBatchUploadTweets); routeMap.set('/getPageContent', apiGetPageContent); routeMap.set('/delete', apiDelete); +routeMap.set('/edit', apiEdit); // Add more route mappings as needed // routeMap.set('/api/otherRoute', { ... }); diff --git a/apps/cf-ai-backend/src/routes/edit.ts b/apps/cf-ai-backend/src/routes/edit.ts new file mode 100644 index 00000000..e32c93ca --- /dev/null +++ b/apps/cf-ai-backend/src/routes/edit.ts @@ -0,0 +1,61 @@ +import { Request } from '@cloudflare/workers-types'; +import { type CloudflareVectorizeStore } from '@langchain/cloudflare'; +import { OpenAIEmbeddings } from '../OpenAIEmbedder'; +import { GenerativeModel } from '@google/generative-ai'; +import { seededRandom } from '../util'; + +export async function POST(request: Request, store: CloudflareVectorizeStore, _: OpenAIEmbeddings, m: GenerativeModel, env: Env) { + 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 }); + } + + const { searchParams } = new URL(request.url); + const uniqueUrl = searchParams.get('uniqueUrl'); + + const toBeDeleted = `${uniqueUrl}-${body.user}`; + const tbduuid = await env.KV.get(toBeDeleted); + if (tbduuid) { + await store.delete({ ids: [tbduuid] }); + } + + // TODO: FIX THIS,BUT TEMPERORILY TRIM page content to 1000 words + body.pageContent = body.pageContent.split(' ').slice(0, 1000).join(' '); + + const newPageContent = `Title: ${body.title}\nDescription: ${body.description}\nURL: ${body.url}\nContent: ${body.pageContent}`; + + const ourID = `${body.url}-${body.user}`; + + const random = seededRandom(ourID); + const uuid = random().toString(36).substring(2, 15) + random().toString(36).substring(2, 15); + + await env.KV.put(uuid, ourID); + + await store.addDocuments( + [ + { + pageContent: newPageContent, + metadata: { + title: body.title?.slice(0, 50) ?? '', + description: body.description ?? '', + space: body.space ?? '', + url: body.url, + user: body.user, + }, + }, + ], + { + ids: [uuid], + }, + ); + + return new Response(JSON.stringify({ message: 'Document Added' }), { status: 200 }); +}