Class: Qaxpert::GeminiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/qaxpert/gemini_client.rb

Constant Summary collapse

GEMINI_API_URL =
'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash:generateContent'

Class Method Summary collapse

Class Method Details

.call(prompt) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/qaxpert/gemini_client.rb', line 5

def self.call(prompt)
  api_key = ENV['GEMINI_API_KEY']
  return "❌ Chave da API não encontrada." unless api_key

  uri = URI("#{GEMINI_API_URL}?key=#{api_key}")
  headers = { 'Content-Type' => 'application/json' }
  body = {
    contents: [
      {
        parts: [{ text: prompt }],
        role: 'user'
      }
    ]
  }.to_json

  response = Net::HTTP.post(uri, body, headers)

  json = JSON.parse(response.body)
  candidate = json.dig('candidates', 0, 'content', 'parts', 0, 'text')

  candidate || "❌ Resposta inválida da API Gemini."
rescue => e
  "❌ Erro ao chamar Gemini: #{e.message}"
end