From b3ea9bf4b993b5411c8228b8bbd4529be4dd2b9f Mon Sep 17 00:00:00 2001 From: Pierre Gayvallet Date: Mon, 16 Sep 2024 11:06:54 +0200 Subject: [PATCH] OpenAI: Ignore chunks with an empty `choices` list (bis) (#192961) ## Summary Follow-up of https://github.com/elastic/kibana/pull/192951 to address the missing bits (given I don't know how to grep) (cherry picked from commit 08b682fbd5f78ac82b42ddc7109e79f89e4ed961) --- .../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;