Class: Mistri::Models::Model

Inherits:
Data
  • Object
show all
Defined in:
lib/mistri/models.rb

Overview

thinking is how the model accepts a reasoning request: :adaptive (the model decides), :budget (a token budget), or :effort. It is what keeps a provider from sending an unsupported thinking shape that 400s.

context_window is the provider's published context or input limit. Anthropic and OpenAI share it with the next output; Gemini publishes a separate inputTokenLimit and outputTokenLimit.

Pricing is selected per request from its prompt size. Rates are paid standard direct-API list prices. cache_write is the provider's baseline write rate; Usage prices a reported longer-retention slice separately.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, provider:, max_output:, context_window:, thinking:, pricing: []) ⇒ Model

Returns a new instance of Model.



43
44
45
46
# File 'lib/mistri/models.rb', line 43

def initialize(id:, provider:, max_output:, context_window:, thinking:, pricing: [])
  @pricing = pricing.sort_by(&:effective_at).freeze
  super(id:, provider:, max_output:, context_window:, thinking:)
end

Instance Attribute Details

#context_windowObject (readonly)

Returns the value of attribute context_window

Returns:

  • (Object)

    the current value of context_window



42
43
44
# File 'lib/mistri/models.rb', line 42

def context_window
  @context_window
end

#idObject (readonly)

Returns the value of attribute id

Returns:

  • (Object)

    the current value of id



42
43
44
# File 'lib/mistri/models.rb', line 42

def id
  @id
end

#max_outputObject (readonly)

Returns the value of attribute max_output

Returns:

  • (Object)

    the current value of max_output



42
43
44
# File 'lib/mistri/models.rb', line 42

def max_output
  @max_output
end

#providerObject (readonly)

Returns the value of attribute provider

Returns:

  • (Object)

    the current value of provider



42
43
44
# File 'lib/mistri/models.rb', line 42

def provider
  @provider
end

#thinkingObject (readonly)

Returns the value of attribute thinking

Returns:

  • (Object)

    the current value of thinking



42
43
44
# File 'lib/mistri/models.rb', line 42

def thinking
  @thinking
end

Class Method Details

._load(payload) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/mistri/models.rb', line 73

def self._load(payload)
  attributes = JSON.parse(payload, symbolize_names: true)
  serialized = attributes.delete(:pricing)
  pricing = serialized.map do |entry|
    card = RateCard.new(rates: entry[:rates], above: entry[:above],
                        higher: entry[:higher])
    Schedule.new(effective_at: Time.at(entry[:effective_at]).utc, card:)
  end
  attributes[:provider] = attributes[:provider].to_sym
  attributes[:thinking] = attributes[:thinking].to_sym
  new(**attributes, pricing:)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



59
# File 'lib/mistri/models.rb', line 59

def ==(other) = super && pricing == other.send(:pricing)

#_dump(_level) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/mistri/models.rb', line 65

def _dump(_level)
  serialized = pricing.map do |entry|
    { effective_at: entry.effective_at.to_i, rates: entry.card.rates,
      above: entry.card.above, higher: entry.card.higher }
  end
  JSON.generate(to_h.merge(pricing: serialized))
end

#hashObject



63
# File 'lib/mistri/models.rb', line 63

def hash = [super, pricing].hash

#priced?Boolean

Returns:

  • (Boolean)


52
# File 'lib/mistri/models.rb', line 52

def priced? = !pricing.empty?

#rates(usage: nil, at: Time.now) ⇒ Object



48
49
50
# File 'lib/mistri/models.rb', line 48

def rates(usage: nil, at: Time.now)
  pricing.reverse_each.find { |entry| entry.effective_at <= at }&.card&.rates_for(usage)
end

#with(**changes) ⇒ Object



54
55
56
57
# File 'lib/mistri/models.rb', line 54

def with(**changes)
  updated_pricing = changes.delete(:pricing) { pricing }
  self.class.new(**to_h, **changes, pricing: updated_pricing)
end