Skip to content

Commit

Permalink
[8.x] OpenAI: Ignore chunks with an empty `choices` list (b…
Browse files Browse the repository at this point in the history
…is) (#192961) (#192978)

# Backport

This will backport the following commits from `main` to `8.x`:
- [OpenAI: Ignore chunks with an empty `choices` list (bis)
(#192961)](#192961)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Pierre
Gayvallet","email":"pierre.gayvallet@elastic.co"},"sourceCommit":{"committedDate":"2024-09-16T09:06:54Z","message":"OpenAI:
Ignore chunks with an empty `choices` list (bis) (#192961)\n\n##
Summary\r\n\r\nFollow-up of
#192951 to address\r\nthe missing
bits (given I don't know how to
grep)","sha":"08b682fbd5f78ac82b42ddc7109e79f89e4ed961","branchLabelMapping":{"^v9.0.0$":"main","^v8.16.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","v9.0.0","backport:prev-minor","Team:Obs
AI Assistant","ci:project-deploy-observability","v8.16.0","Team:AI
Infra"],"title":"OpenAI: Ignore chunks with an empty `choices` list
(bis)","number":192961,"url":"https://github.com/elastic/kibana/pull/192961","mergeCommit":{"message":"OpenAI:
Ignore chunks with an empty `choices` list (bis) (#192961)\n\n##
Summary\r\n\r\nFollow-up of
#192951 to address\r\nthe missing
bits (given I don't know how to
grep)","sha":"08b682fbd5f78ac82b42ddc7109e79f89e4ed961"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/192961","number":192961,"mergeCommit":{"message":"OpenAI:
Ignore chunks with an empty `choices` list (bis) (#192961)\n\n##
Summary\r\n\r\nFollow-up of
#192951 to address\r\nthe missing
bits (given I don't know how to
grep)","sha":"08b682fbd5f78ac82b42ddc7109e79f89e4ed961"}},{"branch":"8.x","label":"v8.16.0","branchLabelMappingKey":"^v8.16.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Pierre Gayvallet <pierre.gayvallet@elastic.co>
  • Loading branch information
kibanamachine and pgayvallet authored Sep 16, 2024
1 parent 0f26581 commit 949f650
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 949f650

Please sign in to comment.