Class: ActiveGenie::Providers::OpenaiProvider

Inherits:
BaseProvider
  • Object
show all
Defined in:
lib/active_genie/providers/openai_provider.rb

Defined Under Namespace

Classes: InvalidResponseError

Constant Summary

Constants inherited from BaseProvider

BaseProvider::DEFAULT_HEADERS, BaseProvider::DEFAULT_MAX_RETRIES, BaseProvider::DEFAULT_OPEN_TIMEOUT, BaseProvider::DEFAULT_RETRY_DELAY, BaseProvider::DEFAULT_TIMEOUT

Instance Method Summary collapse

Methods inherited from BaseProvider

#delete, #get, #initialize, #post, #put

Constructor Details

This class inherits a constructor from ActiveGenie::Providers::BaseProvider

Instance Method Details

#function_calling(messages, function) ⇒ Hash?

Requests structured JSON output from the OpenAI model based on a schema.

Parameters:

  • messages (Array<Hash>)

    A list of messages representing the conversation history. Each hash should have :role (‘user’, ‘assistant’, or ‘system’) and :content (String).

  • function (Hash)

    A JSON schema definition describing the desired output format.

Returns:

  • (Hash, nil)

    The parsed JSON object matching the schema, or nil if parsing fails or content is empty.

Raises:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/active_genie/providers/openai_provider.rb', line 19

def function_calling(messages, function)
  payload = {
    messages:,
    tools: [function_to_tool(function)],
    tool_choice: { type: 'function', function: { name: function[:name] } },
    stream: false,
    model:
  }

  response = retry_with_backoff do
    request(payload)
  end

  raise InvalidResponseError, "Invalid response: #{response}" if response.nil? || response.keys.empty?

  ActiveGenie.logger.call({ code: :function_calling, fine_tune: true, payload:, response: }, config: @config)

  response
end