From d37a76e0d70b77d99ad77428e42a19ca1d57ac14 Mon Sep 17 00:00:00 2001 From: pgayvallet Date: Sun, 15 Sep 2024 20:43:18 +0200 Subject: [PATCH] OpenAI: Ignore chunks with an empty `choices` list (bis) --- .../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;