From 949f6501b3dc8670a66ec5196f2f2dc1bdc9eb3c Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 16 Sep 2024 21:29:00 +1000 Subject: [PATCH] [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) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Pierre Gayvallet --- .../actions/server/lib/get_token_count_from_invoke_stream.ts | 4 +++- x-pack/plugins/elastic_assistant/server/lib/parse_stream.ts | 4 +++- .../common/utils/process_openai_stream.ts | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/x-pack/plugins/actions/server/lib/get_token_count_from_invoke_stream.ts b/x-pack/plugins/actions/server/lib/get_token_count_from_invoke_stream.ts index 604e623699136..909b7e09abda0 100644 --- a/x-pack/plugins/actions/server/lib/get_token_count_from_invoke_stream.ts +++ b/x-pack/plugins/actions/server/lib/get_token_count_from_invoke_stream.ts @@ -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) => { diff --git a/x-pack/plugins/elastic_assistant/server/lib/parse_stream.ts b/x-pack/plugins/elastic_assistant/server/lib/parse_stream.ts index 55be80cbb522d..3aef870be8116 100644 --- a/x-pack/plugins/elastic_assistant/server/lib/parse_stream.ts +++ b/x-pack/plugins/elastic_assistant/server/lib/parse_stream.ts @@ -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) => { diff --git a/x-pack/plugins/observability_solution/observability_ai_assistant/common/utils/process_openai_stream.ts b/x-pack/plugins/observability_solution/observability_ai_assistant/common/utils/process_openai_stream.ts index 11fe48721812c..184b4817abf64 100644 --- a/x-pack/plugins/observability_solution/observability_ai_assistant/common/utils/process_openai_stream.ts +++ b/x-pack/plugins/observability_solution/observability_ai_assistant/common/utils/process_openai_stream.ts @@ -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;