[Obs AI Assistant] Make sure arguments have a default (#185691)

In https://github.com/elastic/kibana/pull/184933, we removed the
parameters for the `context` function, however, OpenAI requires at least
an empty `arguments` object, so requests fail. This PR sets arguments to
an empty JSON object (`"{}"`) if `function_call.arguments` is empty.
This commit is contained in:
Dario Gieselaar 2024-06-10 12:42:08 +02:00 committed by GitHub
parent 8119aac0fe
commit 8f3359c43d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -6,7 +6,7 @@
*/
import { encode } from 'gpt-tokenizer';
import { compact, isEmpty, merge, omit, pick } from 'lodash';
import { compact, merge, pick } from 'lodash';
import OpenAI from 'openai';
import { identity } from 'rxjs';
import { CompatibleJSONSchema } from '../../../../common/functions/types';
@ -68,9 +68,12 @@ function messagesToOpenAI(messages: Message[]): OpenAI.ChatCompletionMessagePara
return {
role,
content: message.message.content,
function_call: isEmpty(message.message.function_call?.name)
? undefined
: omit(message.message.function_call, 'trigger'),
function_call: message.message.function_call?.name
? {
name: message.message.function_call.name,
arguments: message.message.function_call?.arguments || '{}',
}
: undefined,
name: message.message.name,
} as OpenAI.ChatCompletionMessageParam;
})