[8.x] OpenAI: Ignore chunks with an empty `choices` list (bis) (#192961) (#192978)

# Backport

This will backport the following commits from `main` to `8.x`:
- [OpenAI: Ignore chunks with an empty `choices` list (bis)
(#192961)](https://github.com/elastic/kibana/pull/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
https://github.com/elastic/kibana/pull/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
https://github.com/elastic/kibana/pull/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
https://github.com/elastic/kibana/pull/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>
This commit is contained in:
Kibana Machine 2024-09-16 21:29:00 +10:00 committed by GitHub
parent 0f26581266
commit 949f6501b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 7 additions and 3 deletions

View file

@ -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) => {

View file

@ -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) => {

View file

@ -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;