Class: ActiveAgent::Providers::Gemini

Inherits:
Base
  • Object
show all
Defined in:
lib/active_agent/providers/gemini.rb

Constant Summary collapse

DEFAULT_URL =
'https://generativelanguage.googleapis.com/v1beta/openai/chat/completions'

Instance Attribute Summary

Attributes inherited from Base

#api_key, #api_url, #model

Instance Method Summary collapse

Constructor Details

#initialize(model: 'gemini-1.5-flash', api_key: nil, api_url: nil) ⇒ Gemini

Returns a new instance of Gemini.

Raises:



10
11
12
13
14
15
16
# File 'lib/active_agent/providers/gemini.rb', line 10

def initialize(model: 'gemini-1.5-flash', api_key: nil, api_url: nil)
  key = api_key || ENV.fetch('GEMINI_API_KEY', nil) || ENV.fetch('GOOGLE_API_KEY', nil)
  url = api_url || DEFAULT_URL
  super(model: model, api_key: key, api_url: url)

  raise Error, 'GEMINI_API_KEY or GOOGLE_API_KEY is required for Gemini provider' unless @api_key || ENV['MOCK_AGENT_RESPONSE']
end

Instance Method Details

#chat(messages:, tools: []) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/active_agent/providers/gemini.rb', line 18

def chat(messages:, tools: [])
  headers = {
    'Content-Type' => 'application/json',
    'Authorization' => "Bearer #{@api_key}"
  }

  payload = {
    model: @model,
    messages: messages,
    temperature: 0.2
  }

  data = http_post(@api_url, headers, payload)
  message = data.dig('choices', 0, 'message') || {}

  { content: message['content'], tool_calls: nil }
end