Skip to content
Kward Search API index

Class: Kward::ModelCatalog

Inherits:
Object
  • Object
show all
Defined in:
lib/kward/model/catalog.rb

Overview

Fetches, normalizes, and caches provider model lists without storing secrets.

Constant Summary collapse

VERSION =
1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider_id:, api_key:, path: nil, requester: nil) ⇒ ModelCatalog

Returns a new instance of ModelCatalog.



17
18
19
20
21
22
# File 'lib/kward/model/catalog.rb', line 17

def initialize(provider_id:, api_key:, path: nil, requester: nil)
  @provider = ProviderCatalog.fetch(provider_id)
  @api_key = api_key.to_s
  @path = File.expand_path(path || ConfigFiles.model_catalog_cache_path(@provider.id))
  @requester = requester
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/kward/model/catalog.rb', line 15

def path
  @path
end

Instance Method Details

#modelsObject



33
34
35
# File 'lib/kward/model/catalog.rb', line 33

def models
  Array(read&.fetch("models", []))
end

#readObject



37
38
39
40
41
42
43
44
45
46
# File 'lib/kward/model/catalog.rb', line 37

def read
  return nil unless File.exist?(path)

  data = JSON.parse(File.read(path))
  return nil unless data.is_a?(Hash) && data["version"] == VERSION && data["provider"] == @provider.id

  data
rescue JSON::ParserError
  nil
end

#refreshObject



24
25
26
27
28
29
30
31
# File 'lib/kward/model/catalog.rb', line 24

def refresh
  raise "No #{@provider.name} API key found" if @api_key.empty?
  raise "#{@provider.name} does not support automatic model discovery" unless runtime.automatic_model_discovery?

  data = cache_data(normalize(response_body))
  PrivateFile.write_json(path, data)
  data.fetch("models")
end