2
0

fix OpenAi function calling with optional parameters

This commit is contained in:
vforvilela
2024-01-22 15:53:47 -03:00
parent 5627105ce5
commit 2654e7277d

View File

@ -214,13 +214,16 @@ export const createChatCompletion = createAction({
if (!name) continue if (!name) continue
const toolDefinition = options.tools?.find((t) => t.name === name) const toolDefinition = options.tools?.find((t) => t.name === name)
if (!toolDefinition?.code || !toolDefinition.parameters) continue if (!toolDefinition?.code || !toolDefinition.parameters) continue
const toolParams = Object.fromEntries(
toolDefinition.parameters.map(({ name }) => [name, null])
)
const toolArgs = toolCall.function?.arguments const toolArgs = toolCall.function?.arguments
? JSON.parse(toolCall.function?.arguments) ? JSON.parse(toolCall.function?.arguments)
: undefined : undefined
if (!toolArgs) continue if (!toolArgs) continue
const { output, newVariables } = await executeFunction({ const { output, newVariables } = await executeFunction({
variables: variables.list(), variables: variables.list(),
args: toolArgs, args: { ...toolParams, ...toolArgs },
body: toolDefinition.code, body: toolDefinition.code,
}) })
newVariables?.forEach((v) => variables.set(v.id, v.value)) newVariables?.forEach((v) => variables.set(v.id, v.value))