recursive streaming for infinite apply
This commit is contained in:
parent
260866a67f
commit
d9074641df
2 changed files with 103 additions and 11 deletions
101
core/edit/recursiveStream.ts
Normal file
101
core/edit/recursiveStream.ts
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
import { ChatMessage, ILLM, Prediction, PromptLog } from "..";
|
||||
import { DEFAULT_MAX_TOKENS } from "../llm/constants";
|
||||
import { countTokens } from "../llm/countTokens";
|
||||
import { renderChatMessage } from "../util/messageContent";
|
||||
|
||||
const INFINITE_STREAM_SAFETY = 0.8;
|
||||
|
||||
const DUD_PROMPT_LOG: PromptLog = {
|
||||
modelTitle: "",
|
||||
completionOptions: { model: "" },
|
||||
prompt: "",
|
||||
completion: "",
|
||||
};
|
||||
|
||||
const RECURSIVE_PROMPT = `Continue EXACTLY where you left`;
|
||||
|
||||
export async function* recursiveStream(
|
||||
llm: ILLM,
|
||||
prompt: ChatMessage[] | string,
|
||||
prediction: Prediction | undefined,
|
||||
currentBuffer = "",
|
||||
isContinuation = false,
|
||||
): AsyncGenerator<string | ChatMessage> {
|
||||
const maxTokens = llm.completionOptions?.maxTokens ?? DEFAULT_MAX_TOKENS;
|
||||
const safeTokens = maxTokens * INFINITE_STREAM_SAFETY;
|
||||
let totalTokens = 0;
|
||||
let buffer = currentBuffer;
|
||||
let whiteSpaceAtEndOfBuffer = buffer.match(/\s*$/)?.[0] ?? "";
|
||||
let firstChunk = true;
|
||||
|
||||
if (typeof prompt === "string") {
|
||||
const generator = llm.streamComplete(prompt, new AbortController().signal, {
|
||||
raw: true,
|
||||
prediction: undefined,
|
||||
reasoning: false,
|
||||
});
|
||||
|
||||
for await (const chunk of generator) {
|
||||
firstChunk = false;
|
||||
|
||||
yield chunk;
|
||||
buffer += chunk;
|
||||
totalTokens += countTokens(chunk);
|
||||
|
||||
if (totalTokens >= safeTokens) {
|
||||
const continuationPrompt = `${RECURSIVE_PROMPT}:\n\n${buffer}`;
|
||||
|
||||
await generator.return(DUD_PROMPT_LOG); // kill the previous generator
|
||||
|
||||
// TODO - Prediction capabilities lost because of partial input
|
||||
yield* recursiveStream(
|
||||
llm,
|
||||
continuationPrompt,
|
||||
undefined,
|
||||
buffer,
|
||||
true,
|
||||
); // Recursively stream the continuation
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const generator = llm.streamChat(prompt, new AbortController().signal, {
|
||||
prediction,
|
||||
reasoning: false,
|
||||
});
|
||||
|
||||
for await (const chunk of generator) {
|
||||
firstChunk = false;
|
||||
|
||||
yield chunk;
|
||||
const rendered = renderChatMessage(chunk);
|
||||
buffer += rendered;
|
||||
totalTokens += countTokens(chunk.content);
|
||||
|
||||
if (totalTokens >= safeTokens) {
|
||||
const continuationPrompt: ChatMessage[] = [
|
||||
...(isContinuation ? prompt.slice(0, -2) : prompt),
|
||||
{
|
||||
role: "assistant",
|
||||
content: buffer,
|
||||
},
|
||||
{
|
||||
role: "user",
|
||||
content: RECURSIVE_PROMPT,
|
||||
},
|
||||
];
|
||||
|
||||
await generator.return(DUD_PROMPT_LOG);
|
||||
yield* recursiveStream(
|
||||
llm,
|
||||
continuationPrompt,
|
||||
undefined,
|
||||
buffer,
|
||||
true,
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -22,6 +22,7 @@ import { getSystemMessageWithRules } from "../llm/rules/getSystemMessageWithRule
|
|||
import { gptEditPrompt } from "../llm/templates/edit";
|
||||
import { findLast } from "../util/findLast";
|
||||
import { Telemetry } from "../util/posthog";
|
||||
import { recursiveStream } from "./recursiveStream";
|
||||
|
||||
function constructPrompt(
|
||||
prefix: string,
|
||||
|
|
@ -156,17 +157,7 @@ export async function* streamDiffLines({
|
|||
content: highlighted,
|
||||
};
|
||||
|
||||
const completion =
|
||||
typeof prompt === "string"
|
||||
? llm.streamComplete(prompt, new AbortController().signal, {
|
||||
raw: true,
|
||||
prediction,
|
||||
reasoning: false,
|
||||
})
|
||||
: llm.streamChat(prompt, new AbortController().signal, {
|
||||
prediction,
|
||||
reasoning: false,
|
||||
});
|
||||
const completion = recursiveStream(llm, prompt, prediction);
|
||||
|
||||
let lines = streamLines(completion);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue