Skip to content

Commit 680900b

Browse files
committed
add additional finish conditions for OpenAI API and Custom API (both can be customized, thus requiring more condition checks, now the api has better support for ollama and LM Studio) (#631, #648)
1 parent d49280c commit 680900b

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/services/apis/azure-openai-api.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export async function generateAnswersWithAzureOpenaiApi(port, question, session)
5151
answer += data.choices[0].delta.content
5252
port.postMessage({ answer: answer, done: false, session: null })
5353
}
54-
if (data.choices[0].finish_reason === 'stop') {
54+
if (data.choices[0]?.finish_reason) {
5555
pushRecord(session, question, answer)
5656
console.debug('conversation history', { content: session.conversationRecords })
5757
port.postMessage({ answer: null, done: true, session: session })

src/services/apis/custom-api.mjs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
3131
const apiUrl = config.customModelApiUrl
3232

3333
let answer = ''
34+
let finished = false
3435
await fetchSSE(apiUrl, {
3536
method: 'POST',
3637
signal: controller.signal,
@@ -47,7 +48,8 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
4748
}),
4849
onMessage(message) {
4950
console.debug('sse message', message)
50-
if (message.trim() === '[DONE]') {
51+
if (!finished && message.trim() === '[DONE]') {
52+
finished = true
5153
pushRecord(session, question, answer)
5254
console.debug('conversation history', { content: session.conversationRecords })
5355
port.postMessage({ answer: null, done: true, session: session })
@@ -60,6 +62,12 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
6062
console.debug('json error', error)
6163
return
6264
}
65+
if (!finished && data.choices[0]?.finish_reason) {
66+
finished = true
67+
pushRecord(session, question, answer)
68+
console.debug('conversation history', { content: session.conversationRecords })
69+
port.postMessage({ answer: null, done: true, session: session })
70+
}
6371

6472
if (data.response) answer = data.response
6573
else {

src/services/apis/openai-api.mjs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export async function generateAnswersWithGptCompletionApi(
3838
const apiUrl = config.customOpenAiApiUrl
3939

4040
let answer = ''
41+
let finished = false
4142
await fetchSSE(`${apiUrl}/v1/completions`, {
4243
method: 'POST',
4344
signal: controller.signal,
@@ -55,7 +56,8 @@ export async function generateAnswersWithGptCompletionApi(
5556
}),
5657
onMessage(message) {
5758
console.debug('sse message', message)
58-
if (message.trim() === '[DONE]') {
59+
if (!finished && message.trim() === '[DONE]') {
60+
finished = true
5961
pushRecord(session, question, answer)
6062
console.debug('conversation history', { content: session.conversationRecords })
6163
port.postMessage({ answer: null, done: true, session: session })
@@ -68,6 +70,13 @@ export async function generateAnswersWithGptCompletionApi(
6870
console.debug('json error', error)
6971
return
7072
}
73+
if (!finished && data.choices[0]?.finish_reason) {
74+
finished = true
75+
pushRecord(session, question, answer)
76+
console.debug('conversation history', { content: session.conversationRecords })
77+
port.postMessage({ answer: null, done: true, session: session })
78+
}
79+
7180
answer += data.choices[0].text
7281
port.postMessage({ answer: answer, done: false, session: null })
7382
},
@@ -125,6 +134,7 @@ export async function generateAnswersWithChatgptApiCompat(
125134
prompt.push({ role: 'user', content: question })
126135

127136
let answer = ''
137+
let finished = false
128138
await fetchSSE(`${baseUrl}/v1/chat/completions`, {
129139
method: 'POST',
130140
signal: controller.signal,
@@ -141,7 +151,8 @@ export async function generateAnswersWithChatgptApiCompat(
141151
}),
142152
onMessage(message) {
143153
console.debug('sse message', message)
144-
if (message.trim() === '[DONE]') {
154+
if (!finished && message.trim() === '[DONE]') {
155+
finished = true
145156
pushRecord(session, question, answer)
146157
console.debug('conversation history', { content: session.conversationRecords })
147158
port.postMessage({ answer: null, done: true, session: session })
@@ -154,6 +165,13 @@ export async function generateAnswersWithChatgptApiCompat(
154165
console.debug('json error', error)
155166
return
156167
}
168+
if (!finished && data.choices[0]?.finish_reason) {
169+
finished = true
170+
pushRecord(session, question, answer)
171+
console.debug('conversation history', { content: session.conversationRecords })
172+
port.postMessage({ answer: null, done: true, session: session })
173+
}
174+
157175
const delta = data.choices[0]?.delta?.content
158176
const content = data.choices[0]?.message?.content
159177
const text = data.choices[0]?.text

0 commit comments

Comments
 (0)