Class: Riffer::Config::Pricing
- Inherits:
-
Object
- Object
- Riffer::Config::Pricing
- Defined in:
- lib/riffer/config.rb,
sig/generated/riffer/config.rbs
Overview
Consumer-configured token pricing, keyed by provider/model id. Riffer
ships no price table, so an unconfigured model carries no cost.
Defined Under Namespace
Classes: Rates
Instance Method Summary collapse
-
#coerce_optional_rate(value, attribute) ⇒ Float?
-- : (untyped, String) -> Float?.
-
#coerce_rate(value, attribute) ⇒ Float
-- : (untyped, String) -> Float.
-
#empty? ⇒ Boolean
Returns true when no rates are registered.
-
#initialize ⇒ Pricing
constructor
-- : () -> void.
-
#rates_for(model) ⇒ Riffer::Config::Pricing::Rates?
Returns the rates registered for a
provider/modelid. -
#set(models, input:, output:, cache_read: nil, cache_write: nil) ⇒ void
Registers per-million-token rates for a
provider/modelid/s. -
#validate_model!(model) ⇒ void
-- : (String) -> void.
Constructor Details
#initialize ⇒ Pricing
-- : () -> void
157 158 159 |
# File 'lib/riffer/config.rb', line 157 def initialize @rates = {} end |
Instance Method Details
#coerce_optional_rate(value, attribute) ⇒ Float?
-- : (untyped, String) -> Float?
214 215 216 217 |
# File 'lib/riffer/config.rb', line 214 def coerce_optional_rate(value, attribute) return nil if value.nil? coerce_rate(value, attribute) end |
#coerce_rate(value, attribute) ⇒ Float
-- : (untyped, String) -> Float
205 206 207 208 209 210 |
# File 'lib/riffer/config.rb', line 205 def coerce_rate(value, attribute) number = value float = value.is_a?(Numeric) ? number.to_f : nil #: Float? raise Riffer::ArgumentError, "#{attribute} rate must be a non-negative number, got #{value.inspect}" unless float&.finite? && float >= 0 float end |
#empty? ⇒ Boolean
Returns true when no rates are registered.
: () -> bool
189 190 191 |
# File 'lib/riffer/config.rb', line 189 def empty? @rates.empty? end |
#rates_for(model) ⇒ Riffer::Config::Pricing::Rates?
Returns the rates registered for a provider/model id.
: (String) -> Riffer::Config::Pricing::Rates?
182 183 184 |
# File 'lib/riffer/config.rb', line 182 def rates_for(model) @rates[model] end |
#set(models, input:, output:, cache_read: nil, cache_write: nil) ⇒ void
This method returns an undefined value.
Registers per-million-token rates for a provider/model id/s. Raises
Riffer::ArgumentError on a malformed id or a negative/non-numeric rate.
: ((String | Array), input: Numeric, output: Numeric, ?cache_read: Numeric?, ?cache_write: Numeric?) -> void
165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/riffer/config.rb', line 165 def set(models, input:, output:, cache_read: nil, cache_write: nil) ids = models.is_a?(Array) ? models : [models] raise Riffer::ArgumentError, "at least one model id is required" if ids.empty? ids.each { |id| validate_model!(id) } rates = Rates.new( input: coerce_rate(input, "input"), output: coerce_rate(output, "output"), cache_read: coerce_optional_rate(cache_read, "cache_read"), cache_write: coerce_optional_rate(cache_write, "cache_write") ) ids.each { |id| @rates[id] = rates } end |
#validate_model!(model) ⇒ void
This method returns an undefined value.
-- : (String) -> void
197 198 199 200 201 |
# File 'lib/riffer/config.rb', line 197 def validate_model!(model) segments = model.to_s.split("/", 2) valid = segments.length == 2 && segments.none? { |segment| segment.strip.empty? } raise Riffer::ArgumentError, "pricing model id must be in \"provider/model\" form, got #{model.inspect}" unless valid end |