Class: Legate::LLM::Gemini

Inherits:
Adapter
  • Object
show all
Defined in:
lib/legate/llm/gemini.rb

Overview

LLM adapter backed by the gemini-ai gem (Google Gemini, v1beta endpoint).

Constant Summary collapse

MAX_RETRIES =
2
RETRY_BASE_DELAY =

seconds, exponential: 1s, 2s

1

Instance Method Summary collapse

Constructor Details

#initialize(model:, api_key: nil, logger: nil) ⇒ Gemini

Returns a new instance of Gemini.

Parameters:

  • model (String)

    the Gemini model id

  • api_key (String, nil) (defaults to: nil)

    defaults to ENV, then ENV — so either env var works directly (no need to go through Legate.load_environment for the alias).

  • logger (Logger, nil) (defaults to: nil)

    defaults to Legate.logger



21
22
23
24
25
26
27
# File 'lib/legate/llm/gemini.rb', line 21

def initialize(model:, api_key: nil, logger: nil)
  super()
  @model = model
  @api_key = api_key || ENV['GOOGLE_API_KEY'] || ENV['GEMINI_API_KEY']
  @logger = logger || Legate.logger
  @client = build_client
end

Instance Method Details

#available?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/legate/llm/gemini.rb', line 29

def available?
  !@client.nil?
end

#generate(prompt, json: false, schema: nil) ⇒ Object

See Also:



38
39
40
41
42
43
# File 'lib/legate/llm/gemini.rb', line 38

def generate(prompt, json: false, schema: nil)
  return nil unless @client

  response = request_with_retry(build_text_payload(prompt, json: json, schema: schema))
  response.dig('candidates', 0, 'content', 'parts', 0, 'text')
end

#generate_with_tools(prompt, tools:) ⇒ Object



56
57
58
59
60
61
# File 'lib/legate/llm/gemini.rb', line 56

def generate_with_tools(prompt, tools:)
  return { kind: :final, text: nil, thought: nil } unless @client

  response = request_with_retry(build_tools_payload(prompt, tools))
  parse_tool_response(response)
end

#model_nameObject



33
34
35
# File 'lib/legate/llm/gemini.rb', line 33

def model_name
  available? ? @model : nil
end

#supports_function_calling?Boolean

Gemini supports native function calling on the v1beta endpoint.

Returns:

  • (Boolean)


51
52
53
# File 'lib/legate/llm/gemini.rb', line 51

def supports_function_calling?
  true
end

#supports_structured_output?Boolean

Gemini supports structured output via responseSchema on the v1beta endpoint.

Returns:

  • (Boolean)


46
47
48
# File 'lib/legate/llm/gemini.rb', line 46

def supports_structured_output?
  true
end