Module: LlmCostTracker::PriceRegistry
- Defined in:
- lib/llm_cost_tracker/price_registry.rb
Constant Summary collapse
- DEFAULT_PRICES_PATH =
File.("prices.json", __dir__)
- EMPTY_PRICES =
{}.freeze
- PRICE_KEYS =
%w[input cached_input output cache_read_input cache_creation_input].freeze
- METADATA_KEYS =
%w[_source _updated _notes].freeze
- NORMALIZE_PRICE_ENTRY =
lambda do |price| (price || {}).each_with_object({}) do |(key, value), normalized| key = key.to_s normalized[key.to_sym] = Float(value) if PRICE_KEYS.include?(key) end end
- NORMALIZE_PRICE_TABLE =
lambda do |table| (table || {}).each_with_object({}) do |(model, price), normalized| normalized[model.to_s] = NORMALIZE_PRICE_ENTRY.call(price) end end
- RAW_REGISTRY =
JSON.parse(File.read(DEFAULT_PRICES_PATH)).freeze
- PRICE_METADATA =
RAW_REGISTRY.fetch("metadata", {}).freeze
- BUILTIN_PRICES =
NORMALIZE_PRICE_TABLE.call(RAW_REGISTRY.fetch("models", {})).freeze
Class Method Summary collapse
- .builtin_prices ⇒ Object
- .file_prices(path) ⇒ Object
- .metadata ⇒ Object
- .normalize_price_table(table) ⇒ Object
Class Method Details
.builtin_prices ⇒ Object
33 34 35 |
# File 'lib/llm_cost_tracker/price_registry.rb', line 33 def builtin_prices BUILTIN_PRICES end |
.file_prices(path) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/llm_cost_tracker/price_registry.rb', line 45 def file_prices(path) return EMPTY_PRICES unless path path = path.to_s FILE_PRICES_MUTEX.synchronize do cache_key = [path, File.mtime(path).to_f] return @file_prices if @file_prices_cache_key == cache_key @file_prices_cache_key = cache_key @file_prices = normalize_file_prices(price_file_models(load_price_file(path)), path: path).freeze end rescue Errno::ENOENT, JSON::ParserError, Psych::Exception, ArgumentError, TypeError, NoMethodError => e raise Error, "Unable to load prices_file #{path.inspect}: #{e.}" end |
.metadata ⇒ Object
37 38 39 |
# File 'lib/llm_cost_tracker/price_registry.rb', line 37 def PRICE_METADATA end |
.normalize_price_table(table) ⇒ Object
41 42 43 |
# File 'lib/llm_cost_tracker/price_registry.rb', line 41 def normalize_price_table(table) NORMALIZE_PRICE_TABLE.call(table) end |