Class: ActiveGenie::Providers::GoogleProvider

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

Overview

Provider for interacting with the Google Generative Language API.

Constant Summary collapse

API_VERSION_PATH =
'v1beta/models'
ROLE_TO_GOOGLE_ROLE =
{
  user: 'user',
  assistant: 'model'
}.freeze

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 Google Generative Language model based on a schema.

Parameters:

  • messages (Array<Hash>)

    A list of messages representing the conversation history. Each hash should have :role (‘user’ or ‘model’) and :content (String). Google Generative Language uses ‘user’ and ‘model’ roles.

  • 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.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/active_genie/providers/google_provider.rb', line 19

def function_calling(messages, function)
  contents = convert_messages_to_contents(messages, function)
  contents << output_as_json_schema(function)

  payload = {
    contents:,
    generationConfig: {
      response_mime_type: 'application/json',
      temperature: 0.1
    }
  }
  params = { key: provider_config.api_key }

  response = retry_with_backoff do
    request(payload, params)
  end

  json_string = response&.dig('candidates', 0, 'content', 'parts', 0, 'text')
  return nil if json_string.nil? || json_string.empty?

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

  normalize_response(json_string)
end