mirror of
https://github.com/supermemoryai/supermemory.git
synced 2026-10-10 03:28:14 +00:00
Improve code, failed attempt at Streaming text
This commit is contained in:
parent
28a257f1f6
commit
f28f566473
4 changed files with 64 additions and 29 deletions
|
|
@ -338,12 +338,13 @@ app.get('/api/editorai', zValidator(
|
|||
}),
|
||||
), async (c)=> {
|
||||
const { context, request } = c.req.valid("query");
|
||||
|
||||
const { model } = await initQuery(c);
|
||||
|
||||
const {text} = await generateText({ model, prompt: `${request}-${context}`, maxTokens: 224 });
|
||||
|
||||
return c.json({completion: text});
|
||||
const response = await streamText({ model, prompt: `${request}-${context}`, maxTokens: 224 });
|
||||
|
||||
const r = response.toTextStreamResponse();
|
||||
|
||||
return r;
|
||||
})
|
||||
|
||||
export default app;
|
||||
|
|
|
|||
|
|
@ -121,7 +121,6 @@ function ChatWindow({
|
|||
|
||||
const reader = resp.body?.getReader();
|
||||
let done = false;
|
||||
let result = "";
|
||||
while (!done && reader) {
|
||||
const { value, done: d } = await reader.read();
|
||||
done = d;
|
||||
|
|
@ -134,8 +133,6 @@ function ChatWindow({
|
|||
return newChatHistory;
|
||||
});
|
||||
}
|
||||
|
||||
console.log(result);
|
||||
};
|
||||
|
||||
useEffect(() => {
|
||||
|
|
|
|||
|
|
@ -97,7 +97,7 @@ function ToolBar({
|
|||
onClick={() =>
|
||||
AigenerateContent({ idx, editor, setGeneratingfn })
|
||||
}
|
||||
className="absolute select-none inset-0 block h-full w-full rounded-xl bg-background-light"
|
||||
className="absolute select-none inset-0 block h-full w-full rounded-xl bg-[#33393D]"
|
||||
layoutId="hoverBackground"
|
||||
initial={{ opacity: 0 }}
|
||||
animate={{
|
||||
|
|
@ -140,30 +140,66 @@ async function AigenerateContent({
|
|||
"Translate to hindi written in english, do not write anything else",
|
||||
"change tone, improve the way be more formal",
|
||||
"ask, answer the question",
|
||||
"continue this, maximum 30 characters, do not repeat just continue don't use ... to denote start",
|
||||
"continue this, minimum 80 characters, do not repeat just continue don't use ... to denote start",
|
||||
]
|
||||
|
||||
const res = await fetch("/api/editorai", {
|
||||
const resp = await fetch("/api/editorai", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
context: text,
|
||||
request: request[idx],
|
||||
}),
|
||||
})
|
||||
const {completion}: {completion: string} = await res.json();
|
||||
console.log(completion)
|
||||
});
|
||||
|
||||
if (idx === 0 || idx === 1){
|
||||
const selectionLength = completion.length + from
|
||||
editor.chain().focus()
|
||||
.insertContentAt({from, to}, completion).setTextSelection({from, to: selectionLength})
|
||||
.run();
|
||||
} else {
|
||||
const selectionLength = completion.length + to + 1
|
||||
editor.chain().focus()
|
||||
.insertContentAt(to+1, completion).setTextSelection({from, to: selectionLength})
|
||||
.run();
|
||||
if (!resp.body) {
|
||||
console.error("No response body");
|
||||
return;
|
||||
}
|
||||
const reader = resp.body.getReader();
|
||||
// const decoder = new TextDecoder();
|
||||
let done = false;
|
||||
let position = to;
|
||||
|
||||
while (!done) {
|
||||
const { value, done: readerDone } = await reader.read();
|
||||
done = readerDone;
|
||||
|
||||
if (value) {
|
||||
const chunk = new TextDecoder().decode(value)
|
||||
// decoder.decode(value, { stream: true });
|
||||
console.log(chunk);
|
||||
// editor.chain().focus().insertContentAt(position + 1, chunk).run();
|
||||
position += chunk.length
|
||||
}
|
||||
}
|
||||
console.log("Stream complete");
|
||||
|
||||
|
||||
// const reader = resp.body?.getReader();
|
||||
// let done = false;
|
||||
// let position = from;
|
||||
// while (!done && reader) {
|
||||
// const { value, done: d } = await reader.read();
|
||||
// done = d;
|
||||
|
||||
// const cont = new TextDecoder().decode(value)
|
||||
// console.log(cont);
|
||||
//
|
||||
// }
|
||||
// const {completion}: {completion: string} = await res.json();
|
||||
// console.log(completion)
|
||||
|
||||
// if (idx === 0 || idx === 1){
|
||||
// const selectionLength = completion.length + from
|
||||
// editor.chain().focus()
|
||||
// .insertContentAt({from, to}, completion).setTextSelection({from, to: selectionLength})
|
||||
// .run();
|
||||
// } else {
|
||||
// const selectionLength = completion.length + to + 1
|
||||
// editor.chain().focus()
|
||||
// .insertContentAt(to+1, completion).setTextSelection({from, to: selectionLength})
|
||||
// .run();
|
||||
// }
|
||||
|
||||
setGeneratingfn(false);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,16 +4,17 @@ import { ensureAuth } from "../ensureAuth";
|
|||
export const runtime = "edge";
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
const d = await ensureAuth(request);
|
||||
if (!d) {
|
||||
return new Response("Unauthorized", { status: 401 });
|
||||
}
|
||||
// const d = await ensureAuth(request);
|
||||
// if (!d) {
|
||||
// return new Response("Unauthorized", { status: 401 });
|
||||
// }
|
||||
const res : {context: string, request: string} = await request.json()
|
||||
|
||||
try {
|
||||
const response = await fetch(`${process.env.BACKEND_BASE_URL}/api/editorai?context=${res.context}&request=${res.request}`);
|
||||
const result = await response.json();
|
||||
return new Response(JSON.stringify(result));
|
||||
return new Response(response.body, { status: 200 });
|
||||
// const result = await response.json();
|
||||
// return new Response(JSON.stringify(result));
|
||||
} catch (error) {
|
||||
return new Response(`Error, ${error}`)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue