Skip to content

Commit

Permalink
OpenAI: Ignore chunks with an empty choices list (bis) (elastic#192961
Browse files Browse the repository at this point in the history
)

## Summary

Follow-up of elastic#192951 to address
the missing bits (given I don't know how to grep)

(cherry picked from commit 08b682f)
  • Loading branch information
pgayvallet committed Sep 16, 2024
1 parent 5f8e564 commit b3ea9bf
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,9 @@ const parseOpenAIResponse = (responseBody: string) =>
delta: { content?: string; function_call?: { name?: string; arguments: string } };
}>;
} => {
return 'object' in line && line.object === 'chat.completion.chunk';
return (
'object' in line && line.object === 'chat.completion.chunk' && line.choices.length > 0
);
}
)
.reduce((prev, line) => {
Expand Down
4 changes: 3 additions & 1 deletion x-pack/plugins/elastic_assistant/server/lib/parse_stream.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,9 @@ const parseOpenAIResponse = (responseBody: string) =>
delta: { content?: string; function_call?: { name?: string; arguments: string } };
}>;
} => {
return 'object' in line && line.object === 'chat.completion.chunk';
return (
'object' in line && line.object === 'chat.completion.chunk' && line.choices.length > 0
);
}
)
.reduce((prev, line) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export function processOpenAiStream(logger: Logger) {
}),
filter(
(line): line is CreateChatCompletionResponseChunk =>
'object' in line && line.object === 'chat.completion.chunk'
'object' in line && line.object === 'chat.completion.chunk' && line.choices.length > 0
),
map((chunk): ChatCompletionChunkEvent => {
const delta = chunk.choices[0].delta;
Expand Down

0 comments on commit b3ea9bf

Please sign in to comment.