ask-llm-providers 0.6.0 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 82485a61da8048017c554844b7b31218778eca6c20a95a237331b62c2eb31da4
4
- data.tar.gz: 66818f024528ecf1dde358b5a6e0882687b2e75c023d9c02659fa9c97116babb
3
+ metadata.gz: 48397005f7c0a96ae277cc4542f716c389000bb958ef060e474efa763f3cb2e7
4
+ data.tar.gz: ad14c0674e82ff5ee6572ee16c2c2c2731222b846ac1ea10e3ed51b59eb453cd
5
5
  SHA512:
6
- metadata.gz: ac4f1392b495866a46232a8d3b7b5ae00adf4db9e14cc05c774e3478a15f31f8d79313eebf110749d3972ea6f5b02ec33d90a536ec4dade0a8498c58b265ca45
7
- data.tar.gz: 423dadd52ef3653cf8552b7fde1f9a551c793f0c9c1650af139ada68f415d22e90f9d76a1e6788fd2633f074af09439dbf8304168e3f017f7161a46627330327
6
+ metadata.gz: 05db1db4749217a513b85be42e0b7492466d657ea1c90f50b5fe3bacec83745555d95440c77ffe6df90a356584b99f4f3619645a99d1eb8bf73103aed4c39e94
7
+ data.tar.gz: adbb4b2e782d488a87aae1b7c1ba9478dfc9e083c013f701f78984a28fe49c081f42f254197ed071c03a6f31820ecc10de35ef101d97d26e806a5b21d9765074
data/CHANGELOG.md CHANGED
@@ -1,3 +1,28 @@
1
+ ## [0.7.0] — 2026-07-17
2
+
3
+ ### Added
4
+
5
+ - **`Ask::LLM::Sources::ModelsDev`** — fetches model data from `models.dev` API and writes enriched per-provider JSON files with pricing, capabilities, and modalities. Run `rake models:update` before each release to keep bundled model data current.
6
+ - **`Ask::LLM::CostCalculator`** — calculates LLM API costs from model pricing data. Supports input, output, cache read/write, and reasoning tokens.
7
+
8
+ ### Changed
9
+
10
+ - **Model coverage expanded** — from 62 to 289 models across 10 providers, with 284 (98%) having full pricing data. Generated from models.dev API instead of hand-written.
11
+ - **`build_model_info` now deep-symbolizes pricing keys** — pricing hashes loaded from JSON now use symbol keys (`:text_tokens`, `:standard`, `:input_per_million`) matching the format produced by `ModelsDevParser` in ask-core.
12
+ - **`build_model_info` handles date parsing safely** — `Date.parse` failures no longer silently destroy the entire model entry via a broad `rescue Date::Error`. Invalid dates are gracefully set to `nil` via `safe_parse_date`.
13
+
14
+ ### Fixed
15
+
16
+ - **Pricing data loss bug** — `rescue Date::Error` in `build_model_info` was catching exceptions from the entire method body, including date parsing and pricing construction. When any model had an unparseable date, its ModelInfo was created with only `id` and `provider`, silently discarding pricing, capabilities, modalities, and all other fields.
17
+ - **Pricing key inconsistency** — pricing loaded from JSON had string keys while pricing from `ModelsDevParser` (ask-core) had symbol keys. Both formats now consistently use symbol keys.
18
+
19
+ ## [0.6.1] — 2026-07-17
20
+
21
+ ### Added
22
+
23
+ - **`Ask::LLM::CostCalculator`** — calculate LLM API costs from model pricing data. Supports input, output, cache read/write, and reasoning tokens. Returns cost in USD or nil if no pricing data available. Works with any object responding to `#pricing` (Ask::ModelInfo, raw hash, etc.).
24
+ - **`CostCalculator.breakdown`** — returns a component-by-component cost breakdown hash.
25
+
1
26
  ## [0.6.0] — 2026-07-17
2
27
 
3
28
  ### Added
@@ -35,6 +35,8 @@ module Ask
35
35
  true
36
36
  end
37
37
 
38
+
39
+
38
40
  # Like load! but also fetches model lists from configured providers'
39
41
  # list_models() APIs. Unknown models are added with minimal metadata.
40
42
  def refresh!
@@ -112,9 +114,12 @@ module Ask
112
114
  # Register all accumulated entries into Ask::ModelCatalog.
113
115
  # Also registers alias entries so models can be found by alias name.
114
116
  def register_all
117
+ catalog = Ask::ModelCatalog.instance
118
+ catalog.instance_variable_set(:@models, [])
119
+
115
120
  @entries.each do |entry|
116
121
  info = build_model_info(entry)
117
- Ask::ModelCatalog.instance.register(info)
122
+ catalog.register(info)
118
123
  end
119
124
 
120
125
  register_alias_entries
@@ -142,6 +147,13 @@ module Ask
142
147
  hash.transform_keys { |k| k.respond_to?(:to_sym) ? k.to_sym : k }
143
148
  end
144
149
 
150
+ def deep_symbolize_keys(hash)
151
+ hash.each_with_object({}) { |(k, v), h|
152
+ hk = k.respond_to?(:to_sym) ? k.to_sym : k
153
+ h[hk] = v.is_a?(Hash) ? deep_symbolize_keys(v) : v
154
+ }
155
+ end
156
+
145
157
  def add_entry(entry)
146
158
  key = entry_key(entry)
147
159
  return if @model_keys.include?(key)
@@ -170,8 +182,16 @@ module Ask
170
182
 
171
183
  def build_model_info(entry)
172
184
  e = entry.transform_keys(&:to_sym)
173
- modalities = e[:modalities]
174
- modalities = symbolize_keys(modalities) if modalities
185
+
186
+ modalities = symbolize_keys(e[:modalities]) if e[:modalities]
187
+
188
+ pricing = {}
189
+ if e[:pricing] && e[:pricing].any?
190
+ deep_symbolize_keys(e[:pricing]).each { |k, v| pricing[k] = v }
191
+ end
192
+
193
+ knowledge_cutoff = safe_parse_date(e[:knowledge_cutoff])
194
+ created_at = safe_parse_date(e[:created_at])
175
195
 
176
196
  Ask::ModelInfo.new(
177
197
  id: e[:id],
@@ -182,13 +202,19 @@ module Ask
182
202
  context_window: e[:context_window],
183
203
  max_output_tokens: e[:max_output_tokens],
184
204
  modalities: modalities || { input: %w[text], output: %w[text] },
185
- pricing: e[:pricing] || {},
186
- knowledge_cutoff: e[:knowledge_cutoff] ? Date.parse(e[:knowledge_cutoff].to_s) : nil,
187
- created_at: e[:created_at] ? Date.parse(e[:created_at].to_s) : nil,
205
+ pricing: pricing,
206
+ knowledge_cutoff: knowledge_cutoff,
207
+ created_at: created_at,
188
208
  metadata: (e[:metadata] || {}).merge(source: e[:metadata]&.dig("source") || "bundled")
189
209
  )
190
- rescue Date::Error
191
- Ask::ModelInfo.new(id: e[:id], provider: e[:provider])
210
+ end
211
+
212
+ def safe_parse_date(value)
213
+ return nil if value.nil?
214
+ return value if value.is_a?(Date)
215
+ Date.parse(value.to_s)
216
+ rescue ArgumentError
217
+ nil
192
218
  end
193
219
  end
194
220
  end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Ask
4
+ module LLM
5
+ # Calculate LLM API costs from model pricing data.
6
+ #
7
+ # Works with any object responding to +#pricing+ that returns a hash
8
+ # in the standard ask-rb pricing format:
9
+ #
10
+ # {
11
+ # text_tokens: {
12
+ # standard: {
13
+ # input_per_million: 2.5,
14
+ # output_per_million: 10.0,
15
+ # cache_read_input_per_million: 1.25,
16
+ # cache_write_input_per_million: 5.0,
17
+ # reasoning_output_per_million: 15.0
18
+ # }
19
+ # },
20
+ # audio_tokens: { standard: { input_per_million: 100.0, output_per_million: 200.0 } }
21
+ # }
22
+ #
23
+ module CostCalculator
24
+ MILLION = 1_000_000
25
+
26
+ class << self
27
+ # Calculate the total cost in USD for a model invocation.
28
+ #
29
+ # @param model [Ask::ModelInfo, #pricing] the model
30
+ # @param input_tokens [Integer]
31
+ # @param output_tokens [Integer]
32
+ # @param cache_read_tokens [Integer]
33
+ # @param cache_write_tokens [Integer]
34
+ # @param reasoning_tokens [Integer] tokens billed at reasoning rate
35
+ # @return [Float, nil] cost in USD, or nil if no pricing data
36
+ def calculate(model, input_tokens: 0, output_tokens: 0,
37
+ cache_read_tokens: 0, cache_write_tokens: 0,
38
+ reasoning_tokens: 0)
39
+ pricing = model.respond_to?(:pricing) ? model.pricing : model
40
+ rates = pricing.dig(:text_tokens, :standard) or return nil
41
+
42
+ sum = cost(input_tokens, rates[:input_per_million])
43
+ sum += cost(output_tokens, rates[:output_per_million])
44
+
45
+ if cache_read_tokens > 0
46
+ sum += cost(cache_read_tokens, rates[:cache_read_input_per_million])
47
+ end
48
+ if cache_write_tokens > 0
49
+ sum += cost(cache_write_tokens, rates[:cache_write_input_per_million])
50
+ end
51
+ if reasoning_tokens > 0
52
+ rate = rates[:reasoning_output_per_million] || rates[:output_per_million]
53
+ sum += cost(reasoning_tokens, rate)
54
+ end
55
+
56
+ sum
57
+ end
58
+
59
+ # Cost breakdown with individual components.
60
+ #
61
+ # @return [Hash, nil]
62
+ def breakdown(model, input_tokens: 0, output_tokens: 0,
63
+ cache_read_tokens: 0, cache_write_tokens: 0,
64
+ reasoning_tokens: 0)
65
+ pricing = model.respond_to?(:pricing) ? model.pricing : model
66
+ rates = pricing.dig(:text_tokens, :standard) or return nil
67
+
68
+ {
69
+ input: cost(input_tokens, rates[:input_per_million]),
70
+ output: cost(output_tokens, rates[:output_per_million]),
71
+ cache_read: cache_read_tokens > 0 ? cost(cache_read_tokens, rates[:cache_read_input_per_million]) : 0,
72
+ cache_write: cache_write_tokens > 0 ? cost(cache_write_tokens, rates[:cache_write_input_per_million]) : 0,
73
+ reasoning: reasoning_tokens > 0 ? cost(reasoning_tokens, rates[:reasoning_output_per_million] || rates[:output_per_million]) : 0
74
+ }.compact
75
+ end
76
+
77
+ private
78
+
79
+ def cost(tokens, rate)
80
+ return 0.0 unless rate && tokens > 0
81
+ (tokens * rate) / MILLION.to_f
82
+ end
83
+ end
84
+ end
85
+ end
86
+ end