Class: Spectre::Gemini::Embeddings
- Inherits:
-
Object
- Object
- Spectre::Gemini::Embeddings
- Defined in:
- lib/spectre/gemini/embeddings.rb
Constant Summary collapse
- API_URL =
Using Google’s OpenAI-compatible endpoint
'https://generativelanguage.googleapis.com/v1beta/openai/embeddings'- DEFAULT_MODEL =
'gemini-embedding-001'- DEFAULT_TIMEOUT =
60
Class Method Summary collapse
-
.create(text, model: DEFAULT_MODEL, **args) ⇒ Object
Generate embeddings for text.
Class Method Details
.create(text, model: DEFAULT_MODEL, **args) ⇒ Object
Generate embeddings for text
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/spectre/gemini/embeddings.rb', line 16 def self.create(text, model: DEFAULT_MODEL, **args) api_key = Spectre.gemini_configuration&.api_key raise APIKeyNotConfiguredError, "API key is not configured" unless api_key uri = URI(API_URL) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = true http.read_timeout = args.fetch(:read_timeout, DEFAULT_TIMEOUT) http.open_timeout = args.fetch(:open_timeout, DEFAULT_TIMEOUT) request = Net::HTTP::Post.new(uri.path, { 'Content-Type' => 'application/json', 'Authorization' => "Bearer #{api_key}" }) request.body = { model: model, input: text }.to_json response = http.request(request) unless response.is_a?(Net::HTTPSuccess) raise "Gemini API Error: #{response.code} - #{response.}: #{response.body}" end JSON.parse(response.body).dig('data', 0, 'embedding') rescue JSON::ParserError => e raise "JSON Parse Error: #{e.}" end |