From 909ae9a2338dd93e2a766a9bba8715d996a8984f Mon Sep 17 00:00:00 2001 From: Heiko Joerg Schick Date: Thu, 14 Dec 2023 19:58:35 +0100 Subject: [PATCH] Bugfix --- utils/server/index.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/utils/server/index.ts b/utils/server/index.ts index d75b8f1..7ccfc9e 100644 --- a/utils/server/index.ts +++ b/utils/server/index.ts @@ -89,22 +89,23 @@ export const OpenAIStream = async ( const onParse = (event: ParsedEvent | ReconnectInterval) => { 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 || json.choices[0].finish_details != 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); } } }; - + const parser = createParser(onParse); for await (const chunk of res.body as any) {