From 759e8eed0c01de7a5615b99e82d5ab96dc409763 Mon Sep 17 00:00:00 2001 From: Cohee <18619528+Cohee1207@users.noreply.github.com> Date: Tue, 2 Apr 2024 16:38:39 +0300 Subject: [PATCH] Fix for Together --- public/scripts/sse-stream.js | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/public/scripts/sse-stream.js b/public/scripts/sse-stream.js index b817c4e45..ad50798d4 100644 --- a/public/scripts/sse-stream.js +++ b/public/scripts/sse-stream.js @@ -170,7 +170,20 @@ async function* parseStreamData(json) { if (isNotPrimary || json.choices.length === 0) { return null; } - if (typeof json.choices[0].delta === 'object') { + + if (typeof json.choices[0].text === 'string' && json.choices[0].text.length > 0) { + for (let j = 0; j < json.choices[0].text.length; j++) { + const str = json.choices[0].text[j]; + const choiceClone = structuredClone(json.choices[0]); + choiceClone.text = str; + const choices = [choiceClone]; + yield { + data: { ...json, choices }, + chunk: str, + }; + } + } + else if (typeof json.choices[0].delta === 'object') { if (typeof json.choices[0].delta.text === 'string' && json.choices[0].delta.text.length > 0) { for (let j = 0; j < json.choices[0].delta.text.length; j++) { const str = json.choices[0].delta.text[j]; @@ -182,7 +195,8 @@ async function* parseStreamData(json) { chunk: str, }; } - } else if (typeof json.choices[0].delta.content === 'string' && json.choices[0].delta.content.length > 0) { + } + else if (typeof json.choices[0].delta.content === 'string' && json.choices[0].delta.content.length > 0) { for (let j = 0; j < json.choices[0].delta.content.length; j++) { const str = json.choices[0].delta.content[j]; const choiceClone = structuredClone(json.choices[0]); @@ -209,18 +223,6 @@ async function* parseStreamData(json) { } } } - else if (typeof json.choices[0].text === 'string' && json.choices[0].text.length > 0) { - for (let j = 0; j < json.choices[0].text.length; j++) { - const str = json.choices[0].text[j]; - const choiceClone = structuredClone(json.choices[0]); - choiceClone.text = str; - const choices = [choiceClone]; - yield { - data: { ...json, choices }, - chunk: str, - }; - } - } } return null;