fix: parse stream error

This commit is contained in:
2023-09-15 17:09:39 +08:00
parent 47af34c061
commit 49eae4a2b9

View File

@@ -63,7 +63,7 @@ class Chat {
top_p = 1, top_p = 1,
presence_penalty = 0, presence_penalty = 0,
frequency_penalty = 0, frequency_penalty = 0,
} = {}, } = {}
) { ) {
if (OPENAI_API_KEY === undefined) { if (OPENAI_API_KEY === undefined) {
throw "OPENAI_API_KEY is undefined"; throw "OPENAI_API_KEY is undefined";
@@ -95,14 +95,14 @@ class Chat {
} }
if (msg.role === "system") { if (msg.role === "system") {
console.log( 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) { for (const msg of this.messages) {
if (msg.name && msg.role !== "system") { if (msg.name && msg.role !== "system") {
console.log( 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,7 +127,6 @@ class Chat {
}); });
} }
async fetch(): Promise<FetchResponse> { async fetch(): Promise<FetchResponse> {
const resp = await this._fetch(); const resp = await this._fetch();
const j = await resp.json(); const j = await resp.json();
@@ -170,7 +169,7 @@ class Chat {
const jsons: ChunkMessage[] = lines const jsons: ChunkMessage[] = lines
.map((line) => { .map((line) => {
try { try {
const ret = JSON.parse(line.trim()); const ret = JSON.parse(lastText + line.trim());
lastText = ""; lastText = "";
return ret; return ret;
} catch (e) { } catch (e) {