[8.x] [Observability AI Assistant] fix: Improve tool choice handling in Observability AI Assistant client (#203928) (#204029)

# Backport

This will backport the following commits from `main` to `8.x`:
- [[Observability AI Assistant] fix: Improve tool choice handling in
Observability AI Assistant client
(#203928)](https://github.com/elastic/kibana/pull/203928)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Arturo
Lidueña","email":"arturo.liduena@elastic.co"},"sourceCommit":{"committedDate":"2024-12-12T13:58:07Z","message":"[Observability
AI Assistant] fix: Improve tool choice handling in Observability AI
Assistant client
(#203928)\n\nError:\r\n\r\n\r\nhttps://github.com/user-attachments/assets/3ada87df-9c60-4916-aa5a-7f95fb2dc275\r\n\r\nlogs:\r\n```
bash\r\n[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\r\n[2024-12-11T11:03:19.669-05:00][ERROR][plugins.observabilityAIAssistant.service]
Error: Unexpected error\r\n at createInferenceInternalError
(/Users/viduni/Workspace/Elastic/kibana/x-pack/packages/ai-infra/inference-common/src/errors.ts:49:10\r\n```\r\n\r\n---------\r\n\r\nCo-authored-by:
Viduni Wickramarachchi
<viduni.ushanka@gmail.com>","sha":"4455087bd3ff74faf4b503cd8b8d2e4d8455e2f2","branchLabelMapping":{"^v9.0.0$":"main","^v8.18.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:fix","v9.0.0","Team:Obs
AI
Assistant","ci:project-deploy-observability","backport:version","v8.18.0"],"title":"[Observability
AI Assistant] fix: Improve tool choice handling in Observability AI
Assistant
client","number":203928,"url":"https://github.com/elastic/kibana/pull/203928","mergeCommit":{"message":"[Observability
AI Assistant] fix: Improve tool choice handling in Observability AI
Assistant client
(#203928)\n\nError:\r\n\r\n\r\nhttps://github.com/user-attachments/assets/3ada87df-9c60-4916-aa5a-7f95fb2dc275\r\n\r\nlogs:\r\n```
bash\r\n[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\r\n[2024-12-11T11:03:19.669-05:00][ERROR][plugins.observabilityAIAssistant.service]
Error: Unexpected error\r\n at createInferenceInternalError
(/Users/viduni/Workspace/Elastic/kibana/x-pack/packages/ai-infra/inference-common/src/errors.ts:49:10\r\n```\r\n\r\n---------\r\n\r\nCo-authored-by:
Viduni Wickramarachchi
<viduni.ushanka@gmail.com>","sha":"4455087bd3ff74faf4b503cd8b8d2e4d8455e2f2"}},"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/203928","number":203928,"mergeCommit":{"message":"[Observability
AI Assistant] fix: Improve tool choice handling in Observability AI
Assistant client
(#203928)\n\nError:\r\n\r\n\r\nhttps://github.com/user-attachments/assets/3ada87df-9c60-4916-aa5a-7f95fb2dc275\r\n\r\nlogs:\r\n```
bash\r\n[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\r\n[2024-12-11T11:03:19.669-05:00][ERROR][plugins.observabilityAIAssistant.service]
Error: Unexpected error\r\n at createInferenceInternalError
(/Users/viduni/Workspace/Elastic/kibana/x-pack/packages/ai-infra/inference-common/src/errors.ts:49:10\r\n```\r\n\r\n---------\r\n\r\nCo-authored-by:
Viduni Wickramarachchi
<viduni.ushanka@gmail.com>","sha":"4455087bd3ff74faf4b503cd8b8d2e4d8455e2f2"}},{"branch":"8.x","label":"v8.18.0","branchLabelMappingKey":"^v8.18.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Arturo Lidueña <arturo.liduena@elastic.co>
This commit is contained in:
Kibana Machine 2024-12-13 23:05:05 +11:00 committed by GitHub
parent 17a7b5980b
commit d3236620f1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 15 deletions

View file

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

View file

@ -481,13 +481,24 @@ export class ObservabilityAIAssistantClient {
tracer: LangTracer;
}
): Observable<ChatCompletionChunkEvent | TokenCountEvent | ChatCompletionMessageEvent> => {
const tools = functions?.reduce((acc, fn) => {
acc[fn.name] = {
description: fn.description,
schema: fn.parameters,
};
return acc;
}, {} as Record<string, { description: string; schema: any }>);
let tools: Record<string, { description: string; schema: any }> | 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<string, { description: string; schema: any }>);
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(