Class: Kward::ModelCatalog
- Inherits:
-
Object
- Object
- Kward::ModelCatalog
- 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
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Instance Method Summary collapse
-
#initialize(provider_id:, api_key:, path: nil, requester: nil) ⇒ ModelCatalog
constructor
A new instance of ModelCatalog.
- #models ⇒ Object
- #read ⇒ Object
- #refresh ⇒ Object
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.(path || ConfigFiles.model_catalog_cache_path(@provider.id)) @requester = requester end |
Instance Attribute Details
#path ⇒ Object (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
#models ⇒ Object
33 34 35 |
# File 'lib/kward/model/catalog.rb', line 33 def models Array(read&.fetch("models", [])) end |
#read ⇒ Object
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 |
#refresh ⇒ Object
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 |