Skip to content

Commit

Permalink
[Observability AI Assistant] fix: Improve tool choice handling in Obs…
Browse files Browse the repository at this point in the history
…ervability AI Assistant client (elastic#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 <[email protected]>
  • Loading branch information
arturoliduena and viduni94 authored Dec 12, 2024
1 parent cdea0e4 commit 4455087
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
]);
});
Expand Down Expand Up @@ -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);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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(
Expand Down

0 comments on commit 4455087

Please sign in to comment.