Skip to content

Commit

Permalink
add additional finish conditions for OpenAI API and Custom API (both …
Browse files Browse the repository at this point in the history
…can be customized, thus requiring more condition checks, now the api has better support for ollama and LM Studio) (#631, #648)
  • Loading branch information
josStorer committed Mar 23, 2024
1 parent d49280c commit 680900b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/services/apis/azure-openai-api.mjs
Expand Up @@ -51,7 +51,7 @@ export async function generateAnswersWithAzureOpenaiApi(port, question, session)
answer += data.choices[0].delta.content
port.postMessage({ answer: answer, done: false, session: null })
}
if (data.choices[0].finish_reason === 'stop') {
if (data.choices[0]?.finish_reason) {
pushRecord(session, question, answer)
console.debug('conversation history', { content: session.conversationRecords })
port.postMessage({ answer: null, done: true, session: session })
Expand Down
10 changes: 9 additions & 1 deletion src/services/apis/custom-api.mjs
Expand Up @@ -31,6 +31,7 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
const apiUrl = config.customModelApiUrl

let answer = ''
let finished = false
await fetchSSE(apiUrl, {
method: 'POST',
signal: controller.signal,
Expand All @@ -47,7 +48,8 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
}),
onMessage(message) {
console.debug('sse message', message)
if (message.trim() === '[DONE]') {
if (!finished && message.trim() === '[DONE]') {
finished = true
pushRecord(session, question, answer)
console.debug('conversation history', { content: session.conversationRecords })
port.postMessage({ answer: null, done: true, session: session })
Expand All @@ -60,6 +62,12 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
console.debug('json error', error)
return
}
if (!finished && data.choices[0]?.finish_reason) {
finished = true
pushRecord(session, question, answer)
console.debug('conversation history', { content: session.conversationRecords })
port.postMessage({ answer: null, done: true, session: session })
}

if (data.response) answer = data.response
else {
Expand Down
22 changes: 20 additions & 2 deletions src/services/apis/openai-api.mjs
Expand Up @@ -38,6 +38,7 @@ export async function generateAnswersWithGptCompletionApi(
const apiUrl = config.customOpenAiApiUrl

let answer = ''
let finished = false
await fetchSSE(`${apiUrl}/v1/completions`, {
method: 'POST',
signal: controller.signal,
Expand All @@ -55,7 +56,8 @@ export async function generateAnswersWithGptCompletionApi(
}),
onMessage(message) {
console.debug('sse message', message)
if (message.trim() === '[DONE]') {
if (!finished && message.trim() === '[DONE]') {
finished = true
pushRecord(session, question, answer)
console.debug('conversation history', { content: session.conversationRecords })
port.postMessage({ answer: null, done: true, session: session })
Expand All @@ -68,6 +70,13 @@ export async function generateAnswersWithGptCompletionApi(
console.debug('json error', error)
return
}
if (!finished && data.choices[0]?.finish_reason) {
finished = true
pushRecord(session, question, answer)
console.debug('conversation history', { content: session.conversationRecords })
port.postMessage({ answer: null, done: true, session: session })
}

answer += data.choices[0].text
port.postMessage({ answer: answer, done: false, session: null })
},
Expand Down Expand Up @@ -125,6 +134,7 @@ export async function generateAnswersWithChatgptApiCompat(
prompt.push({ role: 'user', content: question })

let answer = ''
let finished = false
await fetchSSE(`${baseUrl}/v1/chat/completions`, {
method: 'POST',
signal: controller.signal,
Expand All @@ -141,7 +151,8 @@ export async function generateAnswersWithChatgptApiCompat(
}),
onMessage(message) {
console.debug('sse message', message)
if (message.trim() === '[DONE]') {
if (!finished && message.trim() === '[DONE]') {
finished = true
pushRecord(session, question, answer)
console.debug('conversation history', { content: session.conversationRecords })
port.postMessage({ answer: null, done: true, session: session })
Expand All @@ -154,6 +165,13 @@ export async function generateAnswersWithChatgptApiCompat(
console.debug('json error', error)
return
}
if (!finished && data.choices[0]?.finish_reason) {
finished = true
pushRecord(session, question, answer)
console.debug('conversation history', { content: session.conversationRecords })
port.postMessage({ answer: null, done: true, session: session })
}

const delta = data.choices[0]?.delta?.content
const content = data.choices[0]?.message?.content
const text = data.choices[0]?.text
Expand Down

0 comments on commit 680900b

Please sign in to comment.