mirror of
https://github.com/wassname/chatGPTBox.git
synced 2026-07-05 20:13:56 +08:00
feat: support for non-streamed responses (#294)
This commit is contained in:
@@ -132,10 +132,12 @@ export async function generateAnswersWithChatgptApi(port, question, session, api
|
||||
console.debug('json error', error)
|
||||
return
|
||||
}
|
||||
if ('content' in data.choices[0].delta) {
|
||||
answer += data.choices[0].delta.content
|
||||
port.postMessage({ answer: answer, done: false, session: null })
|
||||
}
|
||||
answer +=
|
||||
data.choices[0]?.delta?.content ||
|
||||
data.choices[0]?.message?.content ||
|
||||
data.choices[0]?.text ||
|
||||
''
|
||||
port.postMessage({ answer: answer, done: false, session: null })
|
||||
},
|
||||
async onStart() {},
|
||||
async onEnd() {
|
||||
|
||||
+11
-1
@@ -19,7 +19,17 @@ export async function fetchSSE(resource, options) {
|
||||
let hasStarted = false
|
||||
for await (const chunk of streamAsyncIterable(resp.body)) {
|
||||
const str = new TextDecoder().decode(chunk)
|
||||
parser.feed(str)
|
||||
if (!str.startsWith('{') && !str.startsWith('"{')) {
|
||||
parser.feed(str)
|
||||
} else {
|
||||
const formattedStr =
|
||||
'data: ' +
|
||||
JSON.stringify(
|
||||
JSON.parse(str.replace(/^"|"$/g, '').replaceAll('\\"', '"').replaceAll('\\\\u', '\\u')),
|
||||
).replaceAll('\\\\n', '\\n') +
|
||||
'\n\ndata: [DONE]\n\n'
|
||||
parser.feed(formattedStr)
|
||||
}
|
||||
|
||||
if (!hasStarted) {
|
||||
hasStarted = true
|
||||
|
||||
Reference in New Issue
Block a user