Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
yxshv committed Apr 13, 2024
2 parents 1975939 + dac1f59 commit e046169
Show file tree
Hide file tree
Showing 28 changed files with 1,077 additions and 3,597 deletions.
1 change: 1 addition & 0 deletions apps/cf-ai-backend/src/env.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface Env {
GOOGLE_AI_API_KEY: string;
MY_QUEUE: Queue<TweetData[]>;
KV: KVNamespace;
MYBROWSER: BrowserWorker;
}

interface TweetData {
Expand Down
2 changes: 2 additions & 0 deletions apps/cf-ai-backend/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import * as apiQuery from './routes/query';
import * as apiAsk from './routes/ask';
import * as apiChat from './routes/chat';
import * as apiBatchUploadTweets from './routes/batchUploadTweets';
import * as apiGetPageContent from './routes/getPageContent';
import { OpenAIEmbeddings } from './OpenAIEmbedder';
import { GenerativeModel } from '@google/generative-ai';
import { Request } from '@cloudflare/workers-types';
Expand All @@ -28,6 +29,7 @@ routeMap.set('/ask', apiAsk);
routeMap.set('/chat', apiChat);

routeMap.set('/batchUploadTweets', apiBatchUploadTweets);
routeMap.set('/getPageContent', apiGetPageContent);

// Add more route mappings as needed
// routeMap.set('/api/otherRoute', { ... });
Expand Down
56 changes: 35 additions & 21 deletions apps/cf-ai-backend/src/routes/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { Content, GenerativeModel } from '@google/generative-ai';
import { OpenAIEmbeddings } from '../OpenAIEmbedder';
import { CloudflareVectorizeStore } from '@langchain/cloudflare';
import { Request } from '@cloudflare/workers-types';
import { AiTextGenerationOutput } from '@cloudflare/ai/dist/ai/tasks/text-generation';
import { Ai } from '@cloudflare/ai';

export async function POST(request: Request, _: CloudflareVectorizeStore, embeddings: OpenAIEmbeddings, model: GenerativeModel, env?: Env) {
const queryparams = new URL(request.url).searchParams;
Expand Down Expand Up @@ -112,28 +114,40 @@ export async function POST(request: Request, _: CloudflareVectorizeStore, embedd
},
] as Content[];

const chat = model.startChat({
history: [...defaultHistory, ...(body.chatHistory ?? [])],
});
// const chat = model.startChat({
// history: [...defaultHistory, ...(body.chatHistory ?? [])],
// });

const prompt = `Context:\n${preparedContext ?? ''}\n\nQuestion: ${query}\nAnswer:`;

const output = await chat.sendMessageStream(prompt);

const response = new Response(
new ReadableStream({
async start(controller) {
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');
controller.enqueue(encodedChunk);
}
const doneChunk = converter.encode('data: [DONE]');
controller.enqueue(doneChunk);
controller.close();
},
}),
);
return response;
// const output = await chat.sendMessageStream(prompt);

// const response = new Response(
// new ReadableStream({
// async start(controller) {
// 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');
// controller.enqueue(encodedChunk);
// }
// const doneChunk = converter.encode('data: [DONE]');
// controller.enqueue(doneChunk);
// controller.close();
// },
// }),
// );
// return response;
const ai = new Ai(env?.AI);
// @ts-ignore
const output: AiTextGenerationOutput = (await ai.run('@hf/mistralai/mistral-7b-instruct-v0.2', {
prompt,
stream: true,
})) as ReadableStream;

return new Response(output, {
headers: {
'content-type': 'text/event-stream',
},
});
}
31 changes: 31 additions & 0 deletions apps/cf-ai-backend/src/routes/getPageContent.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { GenerativeModel } from '@google/generative-ai';
import { OpenAIEmbeddings } from '../OpenAIEmbedder';
import { CloudflareVectorizeStore } from '@langchain/cloudflare';
import { Request } from '@cloudflare/workers-types';
import puppeteer from '@cloudflare/puppeteer';

export async function GET(request: Request, _: CloudflareVectorizeStore, embeddings: OpenAIEmbeddings, model: GenerativeModel, env?: Env) {
const { searchParams } = new URL(request.url);
let url = searchParams.get('url');
let img: Buffer;
if (url) {
url = new URL(url).toString(); // normalize
const browser = await puppeteer.launch(env?.MYBROWSER);
const page = await browser.newPage();
await page.goto(url);

// Innertext of content
const contentElement = await page.$('body');
const content = await page.evaluate((element) => element.innerText, contentElement);

await browser.close();

return new Response(content, {
headers: {
'content-type': 'text/html',
},
});
} else {
return new Response('Please add an ?url=https://example.com/ parameter');
}
}
8 changes: 8 additions & 0 deletions apps/cf-ai-backend/wrangler.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name = "cf-ai-backend"
main = "src/index.ts"
compatibility_date = "2024-02-23"
compatibility_flags = ['nodejs_compat']

[[vectorize]]
binding = "VECTORIZE_INDEX"
Expand All @@ -19,6 +20,13 @@ binding = "AI"
[[kv_namespaces]]
binding = "KV"
id = "37a90353da63401e84e20e71165531d0"
preview_id = "c58b6202814f4224acea97627d0c18aa"

[browser]
binding = "MYBROWSER"

[placement]
mode = "smart"

# Variable bindings. These are arbitrary, plaintext strings (similar to environment variables)
# Note: Use secrets to store sensitive data.
Expand Down
2 changes: 1 addition & 1 deletion apps/extension/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 3,
"name": "SuperMemory",
"version": "1.0.0",
"version": "2.0.0",
"action": {
"default_popup": "index.html"
},
Expand Down
Loading

0 comments on commit e046169

Please sign in to comment.