mirror of
https://github.com/wassname/chatGPTBox.git
synced 2026-07-04 01:56:37 +08:00
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)
This commit is contained in:
@@ -51,7 +51,7 @@ export async function generateAnswersWithAzureOpenaiApi(port, question, session)
|
|||||||
answer += data.choices[0].delta.content
|
answer += data.choices[0].delta.content
|
||||||
port.postMessage({ answer: answer, done: false, session: null })
|
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)
|
pushRecord(session, question, answer)
|
||||||
console.debug('conversation history', { content: session.conversationRecords })
|
console.debug('conversation history', { content: session.conversationRecords })
|
||||||
port.postMessage({ answer: null, done: true, session: session })
|
port.postMessage({ answer: null, done: true, session: session })
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
|
|||||||
const apiUrl = config.customModelApiUrl
|
const apiUrl = config.customModelApiUrl
|
||||||
|
|
||||||
let answer = ''
|
let answer = ''
|
||||||
|
let finished = false
|
||||||
await fetchSSE(apiUrl, {
|
await fetchSSE(apiUrl, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
@@ -47,7 +48,8 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
|
|||||||
}),
|
}),
|
||||||
onMessage(message) {
|
onMessage(message) {
|
||||||
console.debug('sse message', message)
|
console.debug('sse message', message)
|
||||||
if (message.trim() === '[DONE]') {
|
if (!finished && message.trim() === '[DONE]') {
|
||||||
|
finished = true
|
||||||
pushRecord(session, question, answer)
|
pushRecord(session, question, answer)
|
||||||
console.debug('conversation history', { content: session.conversationRecords })
|
console.debug('conversation history', { content: session.conversationRecords })
|
||||||
port.postMessage({ answer: null, done: true, session: session })
|
port.postMessage({ answer: null, done: true, session: session })
|
||||||
@@ -60,6 +62,12 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
|
|||||||
console.debug('json error', error)
|
console.debug('json error', error)
|
||||||
return
|
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
|
if (data.response) answer = data.response
|
||||||
else {
|
else {
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ export async function generateAnswersWithGptCompletionApi(
|
|||||||
const apiUrl = config.customOpenAiApiUrl
|
const apiUrl = config.customOpenAiApiUrl
|
||||||
|
|
||||||
let answer = ''
|
let answer = ''
|
||||||
|
let finished = false
|
||||||
await fetchSSE(`${apiUrl}/v1/completions`, {
|
await fetchSSE(`${apiUrl}/v1/completions`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
@@ -55,7 +56,8 @@ export async function generateAnswersWithGptCompletionApi(
|
|||||||
}),
|
}),
|
||||||
onMessage(message) {
|
onMessage(message) {
|
||||||
console.debug('sse message', message)
|
console.debug('sse message', message)
|
||||||
if (message.trim() === '[DONE]') {
|
if (!finished && message.trim() === '[DONE]') {
|
||||||
|
finished = true
|
||||||
pushRecord(session, question, answer)
|
pushRecord(session, question, answer)
|
||||||
console.debug('conversation history', { content: session.conversationRecords })
|
console.debug('conversation history', { content: session.conversationRecords })
|
||||||
port.postMessage({ answer: null, done: true, session: session })
|
port.postMessage({ answer: null, done: true, session: session })
|
||||||
@@ -68,6 +70,13 @@ export async function generateAnswersWithGptCompletionApi(
|
|||||||
console.debug('json error', error)
|
console.debug('json error', error)
|
||||||
return
|
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
|
answer += data.choices[0].text
|
||||||
port.postMessage({ answer: answer, done: false, session: null })
|
port.postMessage({ answer: answer, done: false, session: null })
|
||||||
},
|
},
|
||||||
@@ -125,6 +134,7 @@ export async function generateAnswersWithChatgptApiCompat(
|
|||||||
prompt.push({ role: 'user', content: question })
|
prompt.push({ role: 'user', content: question })
|
||||||
|
|
||||||
let answer = ''
|
let answer = ''
|
||||||
|
let finished = false
|
||||||
await fetchSSE(`${baseUrl}/v1/chat/completions`, {
|
await fetchSSE(`${baseUrl}/v1/chat/completions`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
signal: controller.signal,
|
signal: controller.signal,
|
||||||
@@ -141,7 +151,8 @@ export async function generateAnswersWithChatgptApiCompat(
|
|||||||
}),
|
}),
|
||||||
onMessage(message) {
|
onMessage(message) {
|
||||||
console.debug('sse message', message)
|
console.debug('sse message', message)
|
||||||
if (message.trim() === '[DONE]') {
|
if (!finished && message.trim() === '[DONE]') {
|
||||||
|
finished = true
|
||||||
pushRecord(session, question, answer)
|
pushRecord(session, question, answer)
|
||||||
console.debug('conversation history', { content: session.conversationRecords })
|
console.debug('conversation history', { content: session.conversationRecords })
|
||||||
port.postMessage({ answer: null, done: true, session: session })
|
port.postMessage({ answer: null, done: true, session: session })
|
||||||
@@ -154,6 +165,13 @@ export async function generateAnswersWithChatgptApiCompat(
|
|||||||
console.debug('json error', error)
|
console.debug('json error', error)
|
||||||
return
|
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 delta = data.choices[0]?.delta?.content
|
||||||
const content = data.choices[0]?.message?.content
|
const content = data.choices[0]?.message?.content
|
||||||
const text = data.choices[0]?.text
|
const text = data.choices[0]?.text
|
||||||
|
|||||||
Reference in New Issue
Block a user