mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-05-21 18:45:03 +00:00
switch to cloudfalre
This commit is contained in:
parent
7f475470c9
commit
ad422c6b66
3 changed files with 36 additions and 23 deletions
|
|
@ -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;
|
||||
|
|
@ -112,28 +114,39 @@ 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 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 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);
|
||||
const output: AiTextGenerationOutput = (await ai.run('@hf/thebloke/mistral-7b-instruct-v0.1-awq', {
|
||||
prompt,
|
||||
stream: true,
|
||||
})) as ReadableStream;
|
||||
|
||||
return new Response(output, {
|
||||
headers: {
|
||||
'content-type': 'text/event-stream',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
{
|
||||
"manifest_version": 3,
|
||||
"name": "SuperMemory",
|
||||
"version": "1.0.0",
|
||||
"version": "2.0.0",
|
||||
"action": {
|
||||
"default_popup": "index.html"
|
||||
},
|
||||
|
|
|
|||
|
|
@ -62,7 +62,7 @@ export function FilterSpaces({
|
|||
<div className="anycontext-flex anycontext-flex-wrap anycontext-gap-1 anycontext-text-sm anycontext-">
|
||||
{selectedSpaces.map((spaceid) => {
|
||||
const space = spaces.find((s) => s.id === spaceid)!;
|
||||
return <SpaceItem {...space} key={spaceid} onRemove={() => {}} />;
|
||||
return <SpaceItem {...space} key={spaceid} />;
|
||||
})}
|
||||
</div>
|
||||
);
|
||||
|
|
@ -143,7 +143,7 @@ export function FilterSpaces({
|
|||
);
|
||||
}
|
||||
|
||||
function SpaceItem({ name, onRemove }: { onRemove: () => void } & Space) {
|
||||
function SpaceItem({ name }: Space) {
|
||||
return (
|
||||
<div className="anycontext-flex anycontext-justify-center anycontext-items-center anycontext-gap-2 anycontext-p-1 anycontext-pl-2 anycontext-pr-3 anycontext-rounded-full anycontext-bg-black/5 dark:anycontext-bg-white/5 anycontext-border-white/20 dark:anycontext-border-black/20 border">
|
||||
<button className="anycontext-flex hover:anycontext-bg-transparent anycontext-justify-center anycontext-scale-110 anycontext-items-center focus-visible:anycontext-outline-none anycontext-rounded-full anycontext-w-3 anycontext-bg-black/5 dark:anycontext-bg-white/5 anycontext-h-3 anycontext-text-transparent hover:anycontext-text-black dark:hover:anycontext-text-white">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue