From ffa902c68936cf362737d9edd1e9dcac6ac70fbc Mon Sep 17 00:00:00 2001 From: Liao Junxuan Date: Tue, 12 Dec 2023 21:24:10 +0800 Subject: [PATCH] Fix json parsing See https://github.com/mckaywrigley/chatbot-ui/issues/580#issuecomment-1523023238 --- utils/server/index.ts | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/utils/server/index.ts b/utils/server/index.ts index d75b8f1..6a6abc3 100644 --- a/utils/server/index.ts +++ b/utils/server/index.ts @@ -90,17 +90,19 @@ export const OpenAIStream = async ( if (event.type === 'event') { const data = event.data; - try { - const json = JSON.parse(data); - if (json.choices[0].finish_reason != null) { - controller.close(); - return; + if (data !== '[DONE]') { + try { + const json = JSON.parse(data); + if (json.choices[0].finish_reason != null) { + controller.close(); + return; + } + const text = json.choices[0].delta.content; + const queue = encoder.encode(text); + controller.enqueue(queue); + } catch (e) { + controller.error(e); } - const text = json.choices[0].delta.content; - const queue = encoder.encode(text); - controller.enqueue(queue); - } catch (e) { - controller.error(e); } } }; -- 2.43.0