added edit route

This commit is contained in:
Dhravya 2024-04-14 00:05:12 -07:00
parent b1caf038ef
commit 861ad49762
2 changed files with 63 additions and 0 deletions

View file

@ -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', { ... });

View file

@ -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 });
}