Skip to content

Commit

Permalink
Fix handling of empty choices array in Azure OpenAI API integration
Browse files Browse the repository at this point in the history
This commit refines the error handling for the Azure OpenAI API
integration to address the 'n.choices[0] is undefined' error by ensuring
'data.choices' and 'data.choices[0]' are properly checked before access.
This update is necessary due to the behavior changes introduced in the
API version update (#684), which can result in empty 'choices' arrays.

Fix #698.
  • Loading branch information
PeterDaveHello committed May 12, 2024
1 parent f177a33 commit 0357afc
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/services/apis/azure-openai-api.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,18 @@ export async function generateAnswersWithAzureOpenaiApi(port, question, session)
console.debug('json error', error)
return
}
if ('content' in data.choices[0].delta) {
if (
data.choices &&
data.choices.length > 0 &&
data.choices[0] &&
data.choices[0].delta &&
'content' in data.choices[0].delta
) {
answer += data.choices[0].delta.content
port.postMessage({ answer: answer, done: false, session: null })
}
if (data.choices[0]?.finish_reason) {

if (data.choices && data.choices.length > 0 && 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

0 comments on commit 0357afc

Please sign in to comment.