Class: Legate::LLM::Gemini
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
- #available? ⇒ Boolean
- #generate(prompt, json: false, schema: nil) ⇒ Object
- #generate_with_tools(prompt, tools:) ⇒ Object
-
#initialize(model:, api_key: nil, logger: nil) ⇒ Gemini
constructor
A new instance of Gemini.
- #model_name ⇒ Object
-
#supports_function_calling? ⇒ Boolean
Gemini supports native function calling on the v1beta endpoint.
-
#supports_structured_output? ⇒ Boolean
Gemini supports structured output via responseSchema on the v1beta endpoint.
Constructor Details
#initialize(model:, api_key: nil, logger: nil) ⇒ Gemini
Returns a new instance of Gemini.
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
29 30 31 |
# File 'lib/legate/llm/gemini.rb', line 29 def available? !@client.nil? end |
#generate(prompt, json: false, schema: nil) ⇒ Object
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_name ⇒ Object
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.
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.
46 47 48 |
# File 'lib/legate/llm/gemini.rb', line 46 def supports_structured_output? true end |