async generator stream

This commit is contained in:
2023-08-10 18:36:29 +08:00
parent 682094485a
commit 968e6602f7
2 changed files with 85 additions and 93 deletions

View File

@@ -63,7 +63,7 @@ class Chat {
top_p = 1,
presence_penalty = 0,
frequency_penalty = 0,
} = {}
} = {},
) {
if (OPENAI_API_KEY === undefined) {
throw "OPENAI_API_KEY is undefined";
@@ -95,14 +95,14 @@ class Chat {
}
if (msg.role === "system") {
console.log(
"Warning: detected system message in the middle of history"
"Warning: detected system message in the middle of history",
);
}
}
for (const msg of this.messages) {
if (msg.name && msg.role !== "system") {
console.log(
"Warning: detected message where name field set but role is system"
"Warning: detected message where name field set but role is system",
);
}
}
@@ -127,6 +127,7 @@ class Chat {
});
}
async fetch(): Promise<FetchResponse> {
const resp = await this._fetch();
const j = await resp.json();
@@ -142,6 +143,50 @@ class Chat {
return this.messages.slice(-1)[0].content;
}
async *processStreamResponse(resp: Response) {
const reader = resp?.body?.pipeThrough(new TextDecoderStream()).getReader();
if (reader === undefined) {
console.log("reader is undefined");
return;
}
let receiving = true;
while (receiving) {
let lastText = "";
const { value, done } = await reader.read();
if (done) break;
const lines = (lastText + value)
.trim()
.split("\n")
.filter((line) => line.trim())
.map((line) => line.slice("data:".length))
.map((line) => line.trim())
.filter((i) => {
if (i === "[DONE]") {
receiving = false;
return false;
}
return true;
});
const jsons: ChunkMessage[] = lines
.map((line) => {
try {
const ret = JSON.parse(line.trim());
lastText = "";
return ret;
} catch (e) {
console.log(`Chunk parse error at: ${line}`);
lastText = line;
return null;
}
})
.filter((i) => i.choices[0].delta.content);
for (const j of jsons) {
yield j;
}
}
}
processFetchResponse(resp: FetchResponse): string {
if (resp.error !== undefined) {
throw JSON.stringify(resp.error);