Class: Geminize::ModelInfo

Inherits:
Object
  • Object
show all
Defined in:
lib/geminize/model_info.rb

Overview

Handles retrieving model information from the Gemini API

Constant Summary collapse

DEFAULT_PAGE_SIZE =

Default page size for listing models

50

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client = nil, options = {}) ⇒ ModelInfo

Initialize a new ModelInfo instance

Parameters:

  • (defaults to: nil)

    The HTTP client to use

  • (defaults to: {})

    Additional options for the client



18
19
20
21
22
23
# File 'lib/geminize/model_info.rb', line 18

def initialize(client = nil, options = {})
  @client = client || Client.new(options)
  @cache = {}
  @cache_expiry = {}
  @cache_ttl = options[:cache_ttl] || 3600 # Default to 1 hour
end

Instance Attribute Details

#cache_ttl=(value) (writeonly)

This method returns an undefined value.

Set the cache time-to-live (TTL)

Parameters:

  • TTL in seconds



168
169
170
# File 'lib/geminize/model_info.rb', line 168

def cache_ttl=(value)
  @cache_ttl = value
end

#clientGeminize::Client (readonly)

Returns The HTTP client.

Returns:

  • The HTTP client



10
11
12
# File 'lib/geminize/model_info.rb', line 10

def client
  @client
end

Instance Method Details

#clear_cache

This method returns an undefined value.

Clear all cached model information



159
160
161
162
163
# File 'lib/geminize/model_info.rb', line 159

def clear_cache
  @cache = {}
  @cache_expiry = {}
  nil
end

#get_model(model_name, force_refresh: false) ⇒ Geminize::Models::Model

Get information about a specific model

Raises:

  • If the request fails or model is not found

Parameters:

  • The model name to retrieve (models/model)

  • (defaults to: false)

    Force a refresh from the API instead of using cache

Returns:

  • The model information



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/geminize/model_info.rb', line 106

def get_model(model_name, force_refresh: false)
  # Handle both formats: "models/gemini-2.0-flash" or just "gemini-2.0-flash"
  unless model_name.start_with?("models/")
    model_name = "models/#{model_name}"
  end

  cache_key = "model_#{model_name}"

  # Check if we have a valid cached result
  if !force_refresh && @cache[cache_key] && @cache_expiry[cache_key] > Time.now
    return @cache[cache_key]
  end

  # Make the API request
  begin
    response = client.get(model_name)

    # Create a Model from the response
    model = Models::Model.from_api_data(response)

    # Cache the result
    @cache[cache_key] = model
    @cache_expiry[cache_key] = Time.now + @cache_ttl

    model
  rescue Geminize::NotFoundError => e
    # Re-raise with a more descriptive message
    raise Geminize::NotFoundError.new("Model '#{model_name}' not found", e.code, e.http_status)
  end
end

#get_models_by_base_id(base_model_id, force_refresh: false) ⇒ Geminize::Models::ModelList

Get models by base model ID

Raises:

  • If the request fails

Parameters:

  • The base model ID to filter by

  • (defaults to: false)

    Force a refresh from the API instead of using cache

Returns:

  • List of models with the specified base model ID



152
153
154
155
# File 'lib/geminize/model_info.rb', line 152

def get_models_by_base_id(base_model_id, force_refresh: false)
  all_models = list_all_models(force_refresh: force_refresh)
  all_models.filter_by_base_model_id(base_model_id)
end

#get_models_by_method(method, force_refresh: false) ⇒ Geminize::Models::ModelList

Get models that support a specific generation method

Raises:

  • If the request fails

Parameters:

  • The generation method (e.g., "generateContent", "embedContent")

  • (defaults to: false)

    Force a refresh from the API instead of using cache

Returns:

  • List of models that support the method



142
143
144
145
# File 'lib/geminize/model_info.rb', line 142

def get_models_by_method(method, force_refresh: false)
  all_models = list_all_models(force_refresh: force_refresh)
  all_models.filter_by_method(method)
end

#list_all_models(force_refresh: false) ⇒ Geminize::Models::ModelList

Get all available models, handling pagination automatically

Raises:

  • If any request fails

Parameters:

  • (defaults to: false)

    Force a refresh from the API instead of using cache

Returns:

  • Complete list of all available models



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/geminize/model_info.rb', line 61

def list_all_models(force_refresh: false)
  cache_key = "all_models_list"

  # Check if we have a valid cached result
  if !force_refresh && @cache[cache_key] && @cache_expiry[cache_key] > Time.now
    return @cache[cache_key]
  end

  all_models = []
  page_token = nil

  # Fetch first page
  model_list = list_models(
    page_size: DEFAULT_PAGE_SIZE,
    page_token: page_token,
    force_refresh: force_refresh
  )
  all_models.concat(model_list.models)

  # Fetch additional pages if available
  while model_list.has_more_pages?
    page_token = model_list.next_page_token
    model_list = list_models(
      page_size: DEFAULT_PAGE_SIZE,
      page_token: page_token,
      force_refresh: force_refresh
    )
    all_models.concat(model_list.models)
  end

  # Create a consolidated model list
  result = Models::ModelList.new(all_models)

  # Cache the result
  @cache[cache_key] = result
  @cache_expiry[cache_key] = Time.now + @cache_ttl

  result
end

#list_models(page_size: nil, page_token: nil, force_refresh: false) ⇒ Geminize::Models::ModelList

List available models from the Gemini API

Raises:

  • If the request fails

Parameters:

  • (defaults to: nil)

    Number of models to return per page (max 1000)

  • (defaults to: nil)

    Token for retrieving a specific page

  • (defaults to: false)

    Force a refresh from the API instead of using cache

Returns:

  • List of available models



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/geminize/model_info.rb', line 31

def list_models(page_size: nil, page_token: nil, force_refresh: false)
  cache_key = "models_list_#{page_size}_#{page_token}"

  # Check if we have a valid cached result
  if !force_refresh && @cache[cache_key] && @cache_expiry[cache_key] > Time.now
    return @cache[cache_key]
  end

  # Prepare query parameters
  params = {}
  params[:pageSize] = page_size if page_size
  params[:pageToken] = page_token

  # Make the API request
  response = client.get("models", params)

  # Create a ModelList from the response
  model_list = Models::ModelList.from_api_data(response)

  # Cache the result
  @cache[cache_key] = model_list
  @cache_expiry[cache_key] = Time.now + @cache_ttl

  model_list
end