Class: LLMs::Executors::GoogleGeminiExecutor

Inherits:
BaseExecutor show all
Defined in:
lib/llms/executors/google_gemini_executor.rb

Constant Summary

Constants inherited from BaseExecutor

BaseExecutor::DEFAULT_TEMPERATURE

Instance Attribute Summary

Attributes inherited from BaseExecutor

#available_tools, #base_url, #client, #last_error, #last_received_message, #last_received_message_id, #last_sent_message, #last_usage_data, #max_completion_tokens, #max_thinking_tokens, #max_tokens, #model_name, #system_prompt, #temperature, #thinking_effort, #thinking_mode

Instance Method Summary collapse

Methods inherited from BaseExecutor

#execute_prompt, #initialize

Constructor Details

This class inherits a constructor from LLMs::Executors::BaseExecutor

Instance Method Details

#execute_conversation(conversation, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/llms/executors/google_gemini_executor.rb', line 10

def execute_conversation(conversation, &block)
  if block_given?
    stream_conversation(conversation) do |handler|
      handler.on(:text_delta) do |event|
        yield event.text
      end
    end
  else
    send_conversation(conversation)
  end
end

#send_conversation(conversation) ⇒ Object



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
# File 'lib/llms/executors/google_gemini_executor.rb', line 50

def send_conversation(conversation)
  init_new_request(conversation)

  start_time = Time.now
  begin
    http_response = client_request
  rescue StandardError => e
    @last_error = {'error' => e.message, 'backtrace' => e.backtrace}
    return nil
  end
  execution_time = Time.now - start_time

  if http_response && http_response['error']
    @last_error = http_response
    return nil
  end

  @last_received_message = LLMs::Adapters::GoogleGeminiMessageAdapter.message_from_api_format(http_response)
  if @last_received_message.nil?
    @last_error = {'error' => 'No message found in the response. Can happen with thinking models if max_tokens is too low.'}
    return nil
  end

  @last_received_message_id = "gemini-#{Time.now.to_i}" ## no message id in the response
  @last_usage_data = calculate_usage(http_response, execution_time)


  @last_received_message
end

#stream_conversation(conversation) {|emitter| ... } ⇒ Object

Yields:

  • (emitter)


22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/llms/executors/google_gemini_executor.rb', line 22

def stream_conversation(conversation)
  init_new_request(conversation)

  emitter = Stream::EventEmitter.new
  yield emitter if block_given?

  start_time = Time.now
  begin
    http_response, stream_parsed_response = stream_client_request(emitter)
  rescue StandardError => e
    @last_error = {'error' => e.message, 'backtrace' => e.backtrace}
    return nil
  end
  execution_time = Time.now - start_time

  if http_response && http_response['error']
    @last_error = http_response
    return nil
  end

  response_data = stream_parsed_response || http_response

  @last_received_message = LLMs::Adapters::GoogleGeminiMessageAdapter.message_from_api_format(response_data)
  @last_usage_data = calculate_usage(response_data, execution_time)

  @last_received_message
end