Skip to content

Commit

Permalink
improve fetch onmessage
Browse files Browse the repository at this point in the history
  • Loading branch information
josStorer committed Apr 16, 2024
1 parent e29f8a1 commit 97fc651
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 36 deletions.
26 changes: 14 additions & 12 deletions src/services/apis/custom-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK

let answer = ''
let finished = false
const finish = () => {
finished = true
pushRecord(session, question, answer)
console.debug('conversation history', { content: session.conversationRecords })
port.postMessage({ answer: null, done: true, session: session })
}
await fetchSSE(apiUrl, {
method: 'POST',
signal: controller.signal,
Expand All @@ -48,11 +54,9 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
}),
onMessage(message) {
console.debug('sse message', message)
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 })
if (finished) return
if (message.trim() === '[DONE]') {
finish()
return
}
let data
Expand All @@ -62,13 +66,6 @@ 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 })
return
}

if (data.response) answer = data.response
else {
Expand All @@ -84,6 +81,11 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
}
}
port.postMessage({ answer: answer, done: false, session: null })

if (data.choices[0]?.finish_reason) {
finish()
return
}
},
async onStart() {},
async onEnd() {
Expand Down
52 changes: 28 additions & 24 deletions src/services/apis/openai-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ export async function generateAnswersWithGptCompletionApi(

let answer = ''
let finished = false
const finish = () => {
finished = true
pushRecord(session, question, answer)
console.debug('conversation history', { content: session.conversationRecords })
port.postMessage({ answer: null, done: true, session: session })
}
await fetchSSE(`${apiUrl}/v1/completions`, {
method: 'POST',
signal: controller.signal,
Expand All @@ -56,11 +62,9 @@ export async function generateAnswersWithGptCompletionApi(
}),
onMessage(message) {
console.debug('sse message', message)
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 })
if (finished) return
if (message.trim() === '[DONE]') {
finish()
return
}
let data
Expand All @@ -70,16 +74,14 @@ 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 })
return
}

answer += data.choices[0].text
port.postMessage({ answer: answer, done: false, session: null })

if (data.choices[0]?.finish_reason) {
finish()
return
}
},
async onStart() {},
async onEnd() {
Expand Down Expand Up @@ -136,6 +138,12 @@ export async function generateAnswersWithChatgptApiCompat(

let answer = ''
let finished = false
const finish = () => {
finished = true
pushRecord(session, question, answer)
console.debug('conversation history', { content: session.conversationRecords })
port.postMessage({ answer: null, done: true, session: session })
}
await fetchSSE(`${baseUrl}/v1/chat/completions`, {
method: 'POST',
signal: controller.signal,
Expand All @@ -152,11 +160,9 @@ export async function generateAnswersWithChatgptApiCompat(
}),
onMessage(message) {
console.debug('sse message', message)
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 })
if (finished) return
if (message.trim() === '[DONE]') {
finish()
return
}
let data
Expand All @@ -166,13 +172,6 @@ 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 })
return
}

const delta = data.choices[0]?.delta?.content
const content = data.choices[0]?.message?.content
Expand All @@ -185,6 +184,11 @@ export async function generateAnswersWithChatgptApiCompat(
answer += text
}
port.postMessage({ answer: answer, done: false, session: null })

if (data.choices[0]?.finish_reason) {
finish()
return
}
},
async onStart() {},
async onEnd() {
Expand Down

0 comments on commit 97fc651

Please sign in to comment.