Skip to content

Commit b886d7e

Browse files
committed
feat: support for non-streamed responses (#294)
1 parent 004ae87 commit b886d7e

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/services/apis/openai-api.mjs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,12 @@ export async function generateAnswersWithChatgptApi(port, question, session, api
132132
console.debug('json error', error)
133133
return
134134
}
135-
if ('content' in data.choices[0].delta) {
136-
answer += data.choices[0].delta.content
137-
port.postMessage({ answer: answer, done: false, session: null })
138-
}
135+
answer +=
136+
data.choices[0]?.delta?.content ||
137+
data.choices[0]?.message?.content ||
138+
data.choices[0]?.text ||
139+
''
140+
port.postMessage({ answer: answer, done: false, session: null })
139141
},
140142
async onStart() {},
141143
async onEnd() {

src/utils/fetch-sse.mjs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@ export async function fetchSSE(resource, options) {
1919
let hasStarted = false
2020
for await (const chunk of streamAsyncIterable(resp.body)) {
2121
const str = new TextDecoder().decode(chunk)
22-
parser.feed(str)
22+
if (!str.startsWith('{') && !str.startsWith('"{')) {
23+
parser.feed(str)
24+
} else {
25+
const formattedStr =
26+
'data: ' +
27+
JSON.stringify(
28+
JSON.parse(str.replace(/^"|"$/g, '').replaceAll('\\"', '"').replaceAll('\\\\u', '\\u')),
29+
).replaceAll('\\\\n', '\\n') +
30+
'\n\ndata: [DONE]\n\n'
31+
parser.feed(formattedStr)
32+
}
2333

2434
if (!hasStarted) {
2535
hasStarted = true

0 commit comments

Comments
 (0)