From 49eae4a2b9d9f6a79bdc1feee75bdc397e342623 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Fri, 15 Sep 2023 17:09:39 +0800 Subject: [PATCH] fix: parse stream error --- src/chatgpt.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/chatgpt.ts b/src/chatgpt.ts index afbcd22..3cbfd68 100644 --- a/src/chatgpt.ts +++ b/src/chatgpt.ts @@ -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,7 +127,6 @@ class Chat { }); } - async fetch(): Promise { const resp = await this._fetch(); const j = await resp.json(); @@ -170,7 +169,7 @@ class Chat { const jsons: ChunkMessage[] = lines .map((line) => { try { - const ret = JSON.parse(line.trim()); + const ret = JSON.parse(lastText + line.trim()); lastText = ""; return ret; } catch (e) {