Class: Boxcars::GeminiAi

Inherits:
Engine
  • Object
show all
Includes:
OpenAICompatibleChatHelpers, UnifiedObservability
Defined in:
lib/boxcars/engine/gemini_ai.rb

Overview

An engine that uses GeminiAI’s API via an OpenAI-compatible interface.

Constant Summary collapse

DEFAULT_PARAMS =
{
  model: "gemini-2.5-flash",
  temperature: 0.1
}.freeze
DEFAULT_NAME =
"GeminiAI engine"
DEFAULT_DESCRIPTION =
"useful for when you need to use Gemini AI to answer questions. " \
"You should ask targeted questions"

Instance Attribute Summary collapse

Attributes inherited from Engine

#batch_size, #user_id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Engine

#add_usage_detail!, #aggregate_generate_usage!, #aggregate_token_usage_details!, #append_generate_choices!, #capabilities, #extract_answer, #generate, #generate_one, #generation_info, #get_num_tokens, #normalize_generate_response, #process_generate_prompt!, #process_generate_response!, #run, #supports?, #usage_nested_token_value, #usage_token_value, #validate_response!

Constructor Details

#initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, batch_size: 20, **kwargs) ⇒ GeminiAi

Returns a new instance of GeminiAi.

Raises:



21
22
23
24
25
26
27
# File 'lib/boxcars/engine/gemini_ai.rb', line 21

def initialize(name: DEFAULT_NAME, description: DEFAULT_DESCRIPTION, batch_size: 20, **kwargs)
  raise ArgumentError, "unknown keyword: :prompts" if kwargs.key?(:prompts)

  user_id = kwargs.delete(:user_id)
  @llm_params = DEFAULT_PARAMS.merge(kwargs)
  super(description:, name:, batch_size:, user_id:)
end

Instance Attribute Details

#llm_paramsObject (readonly)

Returns the value of attribute llm_params.



11
12
13
# File 'lib/boxcars/engine/gemini_ai.rb', line 11

def llm_params
  @llm_params
end

Class Method Details

.provider_client(gemini_api_key: nil) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/boxcars/engine/gemini_ai.rb', line 29

def self.provider_client(gemini_api_key: nil)
  access_token = Boxcars.configuration.gemini_api_key(gemini_api_key:)
  Boxcars::OpenAIClient.build(
    access_token: access_token,
    uri_base: "https://generativelanguage.googleapis.com/v1beta/"
  )
end

Instance Method Details

#client(prompt:, inputs: {}, gemini_api_key: nil, **kwargs) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/boxcars/engine/gemini_ai.rb', line 37

def client(prompt:, inputs: {}, gemini_api_key: nil, **kwargs)
  start_time = Time.now
  response_data = { response_obj: nil, parsed_json: nil, success: false, error: nil, status_code: nil }
  current_params = @llm_params.merge(kwargs)
  api_request_params = nil
  current_prompt_object = prompt

  begin
    clnt = self.class.provider_client(gemini_api_key:)
    api_request_params = prepare_openai_compatible_chat_request(current_prompt_object, inputs, current_params)

    log_messages_debug(api_request_params[:messages]) if Boxcars.configuration.log_prompts && api_request_params[:messages]
    execute_openai_compatible_chat_call(
      client: clnt,
      api_request_params: api_request_params,
      response_data: response_data,
      success_check: ->(raw) { raw[:choices] || raw[:candidates] },
      unknown_error_message: "Unknown Gemini API Error",
      preserve_existing_error: false
    )
    normalize_gemini_response!(response_data)
  rescue StandardError => e
    handle_openai_compatible_standard_error(e, response_data)
  ensure
    duration_ms = ((Time.now - start_time) * 1000).round
    request_context = {
      prompt: current_prompt_object,
      inputs:,
      conversation_for_api: api_request_params&.dig(:messages) || [],
      user_id:
    }
    track_ai_generation(
      duration_ms:,
      current_params:,
      request_context:,
      response_data:,
      provider: :gemini
    )
  end

  raise response_data[:error] if response_data[:error]

  response_data[:parsed_json]
end

#default_paramsObject



82
83
84
# File 'lib/boxcars/engine/gemini_ai.rb', line 82

def default_params
  @llm_params
end