Class: LittleGhost::Providers::Gemini::CatalogSource

Inherits:
Models::Catalog::Source show all
Defined in:
lib/little_ghost/providers/gemini/catalog_source.rb

Overview

Enriches Gemini availability and limits from the Developer API.

Constant Summary collapse

URL =

:nodoc:

URI("https://generativelanguage.googleapis.com/v1beta/models")

Constants inherited from Models::Catalog::Source

Models::Catalog::Source::DEFAULT_ATTRIBUTE_MERGE_STRATEGIES

Instance Attribute Summary

Attributes inherited from Models::Catalog::Source

#name

Instance Method Summary collapse

Methods inherited from Models::Catalog::Source

#attribute_merge_strategies

Constructor Details

#initialize(provider:, credential_resolver:) ⇒ CatalogSource

Creates a source for the named provider connection.



11
12
13
14
15
# File 'lib/little_ghost/providers/gemini/catalog_source.rb', line 11

def initialize(provider:, credential_resolver:)
  super(name: "gemini")
  @provider = provider
  @credential_resolver = credential_resolver
end

Instance Method Details

#refresh(target: nil) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/little_ghost/providers/gemini/catalog_source.rb', line 17

def refresh(target: nil)
  api_key = @credential_resolver.call.fetch("api_key")
  values = JSON.parse(
    Support::HTTPClient.new(open_timeout: 5, read_timeout: 30, max_response_bytes: 25 * 1024 * 1024)
      .request(uri: URL, headers: {"x-goog-api-key" => api_key})
  ).fetch("models")
  values.select! { |value| value["name"].to_s.delete_prefix("models/") == target.model_id } if target
  values.to_h do |value|
    id = value.fetch("name").delete_prefix("models/")
    ["#{@provider}:#{id}", {available: true, context_window: value["inputTokenLimit"],
                            max_output_tokens: value["outputTokenLimit"]}.compact]
  end
rescue JSON::ParserError, KeyError => error
  raise ProviderError, "Gemini returned an invalid catalog: #{error.message}"
end