From 4455087bd3ff74faf4b503cd8b8d2e4d8455e2f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arturo=20Lidue=C3=B1a?= Date: Thu, 12 Dec 2024 14:58:07 +0100 Subject: [PATCH] [Observability AI Assistant] fix: Improve tool choice handling in Observability AI Assistant client (#203928) Error: https://github.com/user-attachments/assets/3ada87df-9c60-4916-aa5a-7f95fb2dc275 logs: ``` bash [2024-12-11T11:03:19.666-05:00][WARN ][plugins.actions.gen-ai] action execution failure: .gen-ai:azure-gpt4: GPT-4 Azure: an error occurred while running the action: Status code: 400. Message: API Error: #model_error - [] should be non-empty - 'tools'; retry: true [2024-12-11T11:03:19.669-05:00][ERROR][plugins.observabilityAIAssistant.service] Error: Unexpected error at createInferenceInternalError (/Users/viduni/Workspace/Elastic/kibana/x-pack/packages/ai-infra/inference-common/src/errors.ts:49:10 ``` --------- Co-authored-by: Viduni Wickramarachchi --- .../server/service/client/index.test.ts | 6 ++-- .../server/service/client/index.ts | 31 ++++++++++++------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/x-pack/platform/plugins/shared/observability_solution/observability_ai_assistant/server/service/client/index.test.ts b/x-pack/platform/plugins/shared/observability_solution/observability_ai_assistant/server/service/client/index.test.ts index 2456499b2d66f..199a6d8f1cbca 100644 --- a/x-pack/platform/plugins/shared/observability_solution/observability_ai_assistant/server/service/client/index.test.ts +++ b/x-pack/platform/plugins/shared/observability_solution/observability_ai_assistant/server/service/client/index.test.ts @@ -329,8 +329,8 @@ describe('Observability AI Assistant client', () => { { role: 'user', content: 'How many alerts do I have?' }, ]), functionCalling: 'native', - toolChoice: 'auto', - tools: {}, + toolChoice: undefined, + tools: undefined, }, ]); }); @@ -1373,7 +1373,7 @@ describe('Observability AI Assistant client', () => { expect(Object.keys(firstBody.tools ?? {}).length).toEqual(1); - expect(body.tools).toEqual({}); + expect(body.tools).toEqual(undefined); }); }); diff --git a/x-pack/platform/plugins/shared/observability_solution/observability_ai_assistant/server/service/client/index.ts b/x-pack/platform/plugins/shared/observability_solution/observability_ai_assistant/server/service/client/index.ts index c03f7d6333825..8d1ee6138e54f 100644 --- a/x-pack/platform/plugins/shared/observability_solution/observability_ai_assistant/server/service/client/index.ts +++ b/x-pack/platform/plugins/shared/observability_solution/observability_ai_assistant/server/service/client/index.ts @@ -481,13 +481,24 @@ export class ObservabilityAIAssistantClient { tracer: LangTracer; } ): Observable => { - const tools = functions?.reduce((acc, fn) => { - acc[fn.name] = { - description: fn.description, - schema: fn.parameters, - }; - return acc; - }, {} as Record); + let tools: Record | undefined; + let toolChoice: ToolChoiceType | { function: string } | undefined; + + if (functions?.length) { + tools = functions.reduce((acc, fn) => { + acc[fn.name] = { + description: fn.description, + schema: fn.parameters, + }; + return acc; + }, {} as Record); + + toolChoice = functionCall + ? { + function: functionCall, + } + : ToolChoiceType.auto; + } const chatComplete$ = defer(() => this.dependencies.inferenceClient.chatComplete({ @@ -497,11 +508,7 @@ export class ObservabilityAIAssistantClient { messages.filter((message) => message.message.role !== MessageRole.System) ), functionCalling: simulateFunctionCalling ? 'simulated' : 'native', - toolChoice: functionCall - ? { - function: functionCall, - } - : ToolChoiceType.auto, + toolChoice, tools, }) ).pipe(