class ActiveGenie::Clients::OpenaiClient

def function_calling(messages, function)

Returns:
  • (Hash, nil) - The parsed JSON object matching the schema, or nil if parsing fails or content is empty.

Parameters:
  • function (Hash) -- A JSON schema definition describing the desired output format.
  • messages (Array) -- A list of messages representing the conversation history.
def function_calling(messages, function)
  payload = {
    messages:,
    tools: [function_to_tool(function)],
    tool_choice: { type: 'function', function: { name: function[:name] } },
    stream: false,
    model:
  }
  retry_with_backoff do
    response = request(payload)
    raise InvalidResponseError, "Invalid response: #{response}" if response.nil? || response.keys.empty?
    ActiveGenie::Logger.call({ code: :function_calling, fine_tune: true, payload:, response: })
    response
  end
end