Module: OllamaAgent::OllamaCloudCatalog
- Defined in:
- lib/ollama_agent/ollama_cloud_catalog.rb
Overview
Fetches the public Ollama cloud model catalog (/api/tags on ollama.com).
See docs.ollama.com/cloud — list models with curl ollama.com/api/tags. Optional OLLAMA_API_KEY is sent as Authorization: Bearer when set.
Constant Summary collapse
- DEFAULT_TAGS_URL =
"https://ollama.com/api/tags"
Class Method Summary collapse
- .api_key_string(api_key) ⇒ Object
- .catalog_uri(base_url) ⇒ Object
- .http_get(uri, api_key:, open_timeout:, read_timeout:) ⇒ Object
-
.list_model_names(base_url: nil, api_key: nil, open_timeout: 5, read_timeout: 20) ⇒ Array<String>
Sorted unique model names, or empty on failure.
- .names_from_tags_json(body) ⇒ Array<String>
Class Method Details
.api_key_string(api_key) ⇒ Object
68 69 70 71 |
# File 'lib/ollama_agent/ollama_cloud_catalog.rb', line 68 def api_key_string(api_key) k = api_key || ENV.fetch("OLLAMA_API_KEY", nil) k.to_s.strip end |
.catalog_uri(base_url) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/ollama_agent/ollama_cloud_catalog.rb', line 60 def catalog_uri(base_url) raw = base_url || ENV.fetch("OLLAMA_AGENT_CLOUD_CATALOG_URL", nil) raw = DEFAULT_TAGS_URL if raw.nil? || raw.to_s.strip.empty? URI(raw.to_s.strip) rescue URI::InvalidURIError nil end |
.http_get(uri, api_key:, open_timeout:, read_timeout:) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ollama_agent/ollama_cloud_catalog.rb', line 73 def http_get(uri, api_key:, open_timeout:, read_timeout:) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == "https" http.open_timeout = open_timeout http.read_timeout = read_timeout req = Net::HTTP::Get.new(uri) req["Authorization"] = "Bearer #{api_key}" unless api_key.empty? http.request(req) end |
.list_model_names(base_url: nil, api_key: nil, open_timeout: 5, read_timeout: 20) ⇒ Array<String>
Returns sorted unique model names, or empty on failure.
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/ollama_agent/ollama_cloud_catalog.rb', line 20 def list_model_names(base_url: nil, api_key: nil, open_timeout: 5, read_timeout: 20) uri = catalog_uri(base_url) return [] unless uri key = api_key_string(api_key) all_names = [] page = 1 max_pages = 10 # Safety limit loop do page_uri = uri.dup query = URI.decode_www_form(page_uri.query || "") << ["page", page] page_uri.query = URI.encode_www_form(query) res = http_get(page_uri, api_key: key, open_timeout: open_timeout, read_timeout: read_timeout) break unless res.is_a?(Net::HTTPSuccess) names = (res.body) break if names.empty? || (names - all_names).empty? # Stop if no new names all_names.concat(names) break if names.size < 10 # Heuristic: if less than full page (assuming page size > 10) page += 1 break if page > max_pages end all_names.uniq.sort rescue StandardError all_names.uniq.sort end |
.names_from_tags_json(body) ⇒ Array<String>
54 55 56 57 58 |
# File 'lib/ollama_agent/ollama_cloud_catalog.rb', line 54 def (body) parsed = JSON.parse(body.to_s) models = parsed["models"] || [] models.filter_map { |m| m["name"] }.uniq.sort end |