Class: Google::ADK::GeminiClient
- Inherits:
-
Object
- Object
- Google::ADK::GeminiClient
- Defined in:
- lib/google/adk/clients/gemini_client.rb
Overview
Client for interacting with Google’s Gemini API
Constant Summary collapse
- API_BASE_URL =
"https://generativelanguage.googleapis.com"
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
Instance Method Summary collapse
-
#generate_content(model:, messages:, tools: nil, system_instruction: nil) ⇒ Hash
Generate content using Gemini API.
-
#initialize(api_key: nil) ⇒ GeminiClient
constructor
A new instance of GeminiClient.
Constructor Details
#initialize(api_key: nil) ⇒ GeminiClient
Returns a new instance of GeminiClient.
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/google/adk/clients/gemini_client.rb', line 14 def initialize(api_key: nil) @api_key = api_key || ENV["GEMINI_API_KEY"] raise ConfigurationError, "GEMINI_API_KEY not set" unless @api_key @client = Faraday.new(API_BASE_URL) do |conn| conn.request :json conn.response :json conn.adapter Faraday.default_adapter end end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
12 13 14 |
# File 'lib/google/adk/clients/gemini_client.rb', line 12 def api_key @api_key end |
Instance Method Details
#generate_content(model:, messages:, tools: nil, system_instruction: nil) ⇒ Hash
Generate content using Gemini API
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/google/adk/clients/gemini_client.rb', line 32 def generate_content(model:, messages:, tools: nil, system_instruction: nil) url = "/v1beta/models/#{model}:generateContent" payload = { contents: (), generationConfig: { temperature: 0.7, topK: 40, topP: 0.95, maxOutputTokens: 8192 } } payload[:systemInstruction] = { parts: [{ text: system_instruction }] } if system_instruction payload[:tools] = [{ functionDeclarations: tools }] if tools && !tools.empty? response = @client.post("#{url}?key=#{@api_key}") do |req| req.body = payload end handle_response(response) end |