llm_gateway 0.9.0 → 0.10.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 +4 -4
- data/CHANGELOG.md +26 -0
- data/README.md +149 -64
- data/Rakefile +5 -0
- data/docs/migration_guide_0.10.0.md +53 -0
- data/docs/structured-output.md +28 -0
- data/lib/llm_gateway/adapter_definition.rb +64 -0
- data/lib/llm_gateway/adapters/adapter.rb +105 -21
- data/lib/llm_gateway/adapters/anthropic/messages_adapter.rb +8 -0
- data/lib/llm_gateway/adapters/anthropic_option_mapper.rb +29 -19
- data/lib/llm_gateway/adapters/groq/chat_completions_adapter.rb +8 -0
- data/lib/llm_gateway/adapters/groq/option_mapper.rb +7 -9
- data/lib/llm_gateway/adapters/normalized_stream_accumulator.rb +7 -3
- data/lib/llm_gateway/adapters/openai/chat_completions/option_mapper.rb +7 -9
- data/lib/llm_gateway/adapters/openai/chat_completions/stream_mapper.rb +1 -1
- data/lib/llm_gateway/adapters/openai/chat_completions_adapter.rb +6 -0
- data/lib/llm_gateway/adapters/openai/responses/option_mapper.rb +11 -9
- data/lib/llm_gateway/adapters/openai/responses/stream_mapper.rb +1 -1
- data/lib/llm_gateway/adapters/openai/responses_adapter.rb +6 -0
- data/lib/llm_gateway/adapters/openai_codex/responses_adapter.rb +8 -0
- data/lib/llm_gateway/adapters/stream_mapper.rb +4 -3
- data/lib/llm_gateway/agents/harness.rb +63 -29
- data/lib/llm_gateway/agents/in_memory_session_manager.rb +35 -4
- data/lib/llm_gateway/agents/tools/tool_utils.rb +2 -2
- data/lib/llm_gateway/errors.rb +3 -0
- data/lib/llm_gateway/models/catalog.rb +68 -0
- data/lib/llm_gateway/models/cost_calculator.rb +47 -0
- data/lib/llm_gateway/models/definition.rb +325 -0
- data/lib/llm_gateway/models/explicit.rb +46 -0
- data/lib/llm_gateway/models/generated.json +3055 -0
- data/lib/llm_gateway/models.rb +78 -0
- data/lib/llm_gateway/prompt.rb +13 -13
- data/lib/llm_gateway/proxy/adapter.rb +22 -12
- data/lib/llm_gateway/proxy/client.rb +4 -4
- data/lib/llm_gateway/proxy/protocol.rb +50 -0
- data/lib/llm_gateway/proxy/server.rb +12 -9
- data/lib/llm_gateway/utils.rb +12 -6
- data/lib/llm_gateway/version.rb +1 -1
- data/lib/llm_gateway.rb +4 -67
- data/scripts/generate_model_catalog.rb +195 -0
- metadata +13 -4
- data/lib/llm_gateway/client.rb +0 -35
- data/lib/llm_gateway/provider_registry.rb +0 -37
|
@@ -8,15 +8,13 @@ module LlmGateway
|
|
|
8
8
|
class Harness < LlmGateway::Prompt
|
|
9
9
|
COMPACTION_TOKEN_THRESHOLD = 180_000
|
|
10
10
|
COMPACTION_IDLE_THRESHOLD_SECONDS = 60 * 60
|
|
11
|
-
attr_accessor :provider
|
|
12
|
-
attr_reader :session_manager, :default_queue_mode, :queue_drain_mode,
|
|
13
|
-
:model, :reasoning
|
|
14
11
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
12
|
+
attr_reader :session_manager, :adapter, :default_queue_mode, :queue_drain_mode
|
|
13
|
+
|
|
14
|
+
def initialize(session_manager, adapter:, cache_key: nil, cache_retention: nil)
|
|
15
|
+
super(adapter:, cache_key:, cache_retention:)
|
|
18
16
|
@session_manager = session_manager
|
|
19
|
-
|
|
17
|
+
validate_adapter_model!(adapter, model)
|
|
20
18
|
self.default_queue_mode = :follow_up
|
|
21
19
|
self.queue_drain_mode = :all
|
|
22
20
|
end
|
|
@@ -26,6 +24,14 @@ module LlmGateway
|
|
|
26
24
|
end
|
|
27
25
|
alias :prompt :transcript
|
|
28
26
|
|
|
27
|
+
def model
|
|
28
|
+
runtime_configuration.model
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def reasoning
|
|
32
|
+
runtime_configuration.reasoning
|
|
33
|
+
end
|
|
34
|
+
|
|
29
35
|
def prompt_message(message, &block)
|
|
30
36
|
enqueue_and_continue_if_idle(message, default_queue_mode, &block)
|
|
31
37
|
end
|
|
@@ -46,24 +52,51 @@ module LlmGateway
|
|
|
46
52
|
@queue_drain_mode = session_manager.validate_drain_mode!(mode)
|
|
47
53
|
end
|
|
48
54
|
|
|
49
|
-
def model=(
|
|
50
|
-
return
|
|
55
|
+
def model=(definition)
|
|
56
|
+
return model if model.equal?(definition)
|
|
51
57
|
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
@model
|
|
58
|
+
validate_adapter_model!(adapter, definition)
|
|
59
|
+
session_manager.change_model(definition)
|
|
55
60
|
end
|
|
56
61
|
|
|
57
62
|
def reasoning=(level)
|
|
58
|
-
return
|
|
63
|
+
return reasoning if reasoning == level
|
|
64
|
+
|
|
65
|
+
session_manager.change_reasoning(level)
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def change_adapter(new_adapter, model: nil)
|
|
69
|
+
current_model = self.model
|
|
70
|
+
if model.nil? && new_adapter.provider != "proxy" && new_adapter.provider != current_model.provider
|
|
71
|
+
raise LlmGateway::Errors::ModelProviderMismatch,
|
|
72
|
+
"Changing providers requires a model definition for the new provider"
|
|
73
|
+
end
|
|
59
74
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
75
|
+
candidate_model = model || current_model
|
|
76
|
+
validate_adapter_model!(new_adapter, candidate_model)
|
|
77
|
+
session_manager.change_model(candidate_model) unless candidate_model.equal?(current_model)
|
|
78
|
+
@adapter = new_adapter
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def adapter=(new_adapter)
|
|
82
|
+
change_adapter(new_adapter)
|
|
63
83
|
end
|
|
64
84
|
|
|
65
85
|
def compact
|
|
66
|
-
session_manager.compaction(
|
|
86
|
+
session_manager.compaction(adapter)
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def stream(input = transcript, **options, &block)
|
|
90
|
+
stream_options = {
|
|
91
|
+
cache_key: cache_key,
|
|
92
|
+
cache_retention: cache_retention
|
|
93
|
+
}.compact.merge(options).merge(
|
|
94
|
+
model: model,
|
|
95
|
+
tools: tools,
|
|
96
|
+
system: system_prompt,
|
|
97
|
+
reasoning: reasoning
|
|
98
|
+
)
|
|
99
|
+
adapter.stream(input, **stream_options, &block)
|
|
67
100
|
end
|
|
68
101
|
|
|
69
102
|
def run(&block)
|
|
@@ -94,9 +127,7 @@ module LlmGateway
|
|
|
94
127
|
turn_end_event = Event::TurnEnd.new(message: assistant_message, tool_results: tool_results)
|
|
95
128
|
emit(turn_end_event, &block)
|
|
96
129
|
|
|
97
|
-
if tool_results.length.positive?
|
|
98
|
-
return run(&block)
|
|
99
|
-
end
|
|
130
|
+
return run(&block) if tool_results.length.positive?
|
|
100
131
|
|
|
101
132
|
if session_manager.queued_messages?(:follow_up)
|
|
102
133
|
compact_if_needed
|
|
@@ -122,15 +153,20 @@ module LlmGateway
|
|
|
122
153
|
|
|
123
154
|
private
|
|
124
155
|
|
|
125
|
-
def
|
|
126
|
-
session_manager.
|
|
156
|
+
def runtime_configuration
|
|
157
|
+
session_manager.current_configuration
|
|
127
158
|
end
|
|
128
159
|
|
|
129
|
-
def
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
160
|
+
def validate_adapter_model!(candidate_adapter, candidate_model)
|
|
161
|
+
unless candidate_adapter.respond_to?(:resolve_model!)
|
|
162
|
+
raise ArgumentError, "adapter must implement #resolve_model!"
|
|
163
|
+
end
|
|
164
|
+
unless candidate_model
|
|
165
|
+
raise LlmGateway::Errors::InvalidModelDefinition,
|
|
166
|
+
"The session must have a model before constructing a harness"
|
|
133
167
|
end
|
|
168
|
+
|
|
169
|
+
candidate_adapter.resolve_model!(candidate_model)
|
|
134
170
|
end
|
|
135
171
|
|
|
136
172
|
def enqueue_and_continue_if_idle(message, queue, &block)
|
|
@@ -173,9 +209,7 @@ module LlmGateway
|
|
|
173
209
|
end
|
|
174
210
|
|
|
175
211
|
def emit(event, &block)
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
block.call(event)
|
|
212
|
+
block&.call(event)
|
|
179
213
|
end
|
|
180
214
|
end
|
|
181
215
|
end
|
|
@@ -11,6 +11,13 @@ module LlmGateway
|
|
|
11
11
|
QUEUES = [ :steer, :follow_up ].freeze
|
|
12
12
|
DRAIN_MODES = [ :one_at_a_time, :all ].freeze
|
|
13
13
|
|
|
14
|
+
RuntimeConfiguration = Struct.new(:model, :reasoning, keyword_init: true) do
|
|
15
|
+
def initialize(model: nil, reasoning: nil)
|
|
16
|
+
super
|
|
17
|
+
freeze
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
|
|
14
21
|
attr_reader :session_id, :session_start
|
|
15
22
|
|
|
16
23
|
def initialize(session_id = nil)
|
|
@@ -106,12 +113,35 @@ module LlmGateway
|
|
|
106
113
|
message_events.last&.dig(:id)
|
|
107
114
|
end
|
|
108
115
|
|
|
109
|
-
def
|
|
110
|
-
|
|
116
|
+
def change_model(model)
|
|
117
|
+
canonical = LlmGateway.models.fetch(provider: model.provider, id: model.id)
|
|
118
|
+
push_entry(type: "model_change", provider: canonical.provider, model_id: canonical.id)
|
|
119
|
+
canonical
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def change_reasoning(reasoning)
|
|
123
|
+
push_entry(type: "reasoning_change", reasoning: reasoning)
|
|
124
|
+
reasoning
|
|
111
125
|
end
|
|
112
126
|
|
|
113
|
-
def
|
|
114
|
-
|
|
127
|
+
def current_configuration
|
|
128
|
+
model_event = nil
|
|
129
|
+
reasoning = nil
|
|
130
|
+
|
|
131
|
+
events.reverse_each do |event|
|
|
132
|
+
model_event ||= event if event[:type] == "model_change"
|
|
133
|
+
reasoning ||= event[:reasoning] if event[:type] == "reasoning_change"
|
|
134
|
+
break if model_event && reasoning
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
model = if model_event
|
|
138
|
+
LlmGateway.models.fetch(
|
|
139
|
+
provider: model_event.fetch(:provider),
|
|
140
|
+
id: model_event.fetch(:model_id)
|
|
141
|
+
)
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
RuntimeConfiguration.new(model: model, reasoning: reasoning || "high")
|
|
115
145
|
end
|
|
116
146
|
|
|
117
147
|
def events_until(event_id)
|
|
@@ -144,6 +174,7 @@ module LlmGateway
|
|
|
144
174
|
def compaction(adapter)
|
|
145
175
|
response = adapter.stream(
|
|
146
176
|
active_messages,
|
|
177
|
+
model: current_configuration.model,
|
|
147
178
|
system: "Summarize the conversation so far for future context.",
|
|
148
179
|
tools: []
|
|
149
180
|
)
|
|
@@ -155,9 +155,9 @@ module ToolUtils
|
|
|
155
155
|
bytes = str.dup.force_encoding("UTF-8").bytes
|
|
156
156
|
return str if bytes.length <= max_bytes
|
|
157
157
|
|
|
158
|
-
tail = bytes.last(max_bytes).pack("C*")
|
|
158
|
+
tail = bytes.last(max_bytes).pack("C*").force_encoding(Encoding::UTF_8)
|
|
159
159
|
until tail.valid_encoding?
|
|
160
|
-
tail = tail.bytes.drop(1).pack("C*")
|
|
160
|
+
tail = tail.bytes.drop(1).pack("C*").force_encoding(Encoding::UTF_8)
|
|
161
161
|
end
|
|
162
162
|
tail
|
|
163
163
|
end
|
data/lib/llm_gateway/errors.rb
CHANGED
|
@@ -39,6 +39,9 @@ module LlmGateway
|
|
|
39
39
|
class UnsupportedModel < ClientError; end
|
|
40
40
|
class UnsupportedProvider < ClientError; end
|
|
41
41
|
class MissingMapperForProvider < ClientError; end
|
|
42
|
+
class InvalidModelDefinition < BaseError; end
|
|
43
|
+
class ModelProviderMismatch < BaseError; end
|
|
44
|
+
class UnsupportedModelForAdapter < BaseError; end
|
|
42
45
|
|
|
43
46
|
OVERFLOW_PATTERNS = [
|
|
44
47
|
/prompt is too long/i, # Anthropic
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module LlmGateway
|
|
4
|
+
module Models
|
|
5
|
+
class Catalog
|
|
6
|
+
def initialize(definitions = [])
|
|
7
|
+
@definitions = {}
|
|
8
|
+
definitions.each { |definition| register(definition) }
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def register(definition, replace: false)
|
|
12
|
+
unless definition.is_a?(Definition)
|
|
13
|
+
raise ArgumentError, "Expected a model definition, got #{definition.inspect}"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
key = [ definition.provider, definition.id ]
|
|
17
|
+
if @definitions.key?(key) && !replace
|
|
18
|
+
raise ArgumentError, "Duplicate model catalog entry: #{key.join("/")}"
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
@definitions[key] = definition
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def find(reference = nil, provider: nil, id: nil)
|
|
25
|
+
provider, id = lookup_key(reference, provider:, id:)
|
|
26
|
+
@definitions[[ provider, id ]]
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def fetch(reference = nil, provider: nil, id: nil)
|
|
30
|
+
provider, id = lookup_key(reference, provider:, id:)
|
|
31
|
+
@definitions.fetch([ provider, id ]) do
|
|
32
|
+
raise KeyError, "Unknown model catalog entry: #{provider}/#{id}"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def all(provider: nil)
|
|
37
|
+
return @definitions.values.freeze unless provider
|
|
38
|
+
|
|
39
|
+
@definitions.select { |(definition_provider, _), _| definition_provider == provider.to_s }.values.freeze
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
private
|
|
43
|
+
|
|
44
|
+
def lookup_key(reference, provider:, id:)
|
|
45
|
+
if reference
|
|
46
|
+
if provider || id
|
|
47
|
+
raise ArgumentError, "Pass either a model reference or provider:/id:, not both"
|
|
48
|
+
end
|
|
49
|
+
parse(reference)
|
|
50
|
+
else
|
|
51
|
+
if provider.nil? || provider.to_s.empty? || id.nil? || id.to_s.empty?
|
|
52
|
+
raise ArgumentError, "Both provider: and id: are required"
|
|
53
|
+
end
|
|
54
|
+
[ provider.to_s, id.to_s ]
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def parse(reference)
|
|
59
|
+
provider, id = reference.to_s.split("/", 2)
|
|
60
|
+
if provider.nil? || provider.empty? || id.nil? || id.empty?
|
|
61
|
+
raise ArgumentError, "Model reference must be in provider/model form: #{reference.inspect}"
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
[ provider, id ]
|
|
65
|
+
end
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bigdecimal"
|
|
4
|
+
|
|
5
|
+
module LlmGateway
|
|
6
|
+
module Models
|
|
7
|
+
class CostCalculator
|
|
8
|
+
TOKENS_PER_MILLION = BigDecimal("1000000")
|
|
9
|
+
|
|
10
|
+
def self.calculate(definition, usage)
|
|
11
|
+
new(definition, usage).calculate
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def initialize(definition, usage)
|
|
15
|
+
@definition = definition
|
|
16
|
+
@usage = usage.to_h.symbolize_keys
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def calculate
|
|
20
|
+
return nil unless definition&.pricing
|
|
21
|
+
|
|
22
|
+
rates = definition.pricing.rates_for(input + cache_read + cache_write)
|
|
23
|
+
components = {
|
|
24
|
+
input: (rates.input * input) / TOKENS_PER_MILLION,
|
|
25
|
+
output: (rates.output * output) / TOKENS_PER_MILLION,
|
|
26
|
+
cache_read: (rates.cache_read * cache_read) / TOKENS_PER_MILLION,
|
|
27
|
+
cache_write: (rates.cache_write * cache_write) / TOKENS_PER_MILLION
|
|
28
|
+
}
|
|
29
|
+
components[:total] = components.values.sum
|
|
30
|
+
components.freeze
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
attr_reader :definition
|
|
36
|
+
|
|
37
|
+
def input = count(:input)
|
|
38
|
+
def output = count(:output)
|
|
39
|
+
def cache_read = count(:cache_read)
|
|
40
|
+
def cache_write = count(:cache_write)
|
|
41
|
+
|
|
42
|
+
def count(key)
|
|
43
|
+
Integer(@usage.fetch(key, 0))
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,325 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "bigdecimal"
|
|
4
|
+
require "date"
|
|
5
|
+
|
|
6
|
+
module LlmGateway
|
|
7
|
+
module Models
|
|
8
|
+
class Definition
|
|
9
|
+
REASONING_LEVELS = %i[minimal low medium high max].freeze
|
|
10
|
+
REASONING_BLOCK_SIZE = 1_024
|
|
11
|
+
DEFAULT_REASONING_BUDGET_CEILING = 20_480
|
|
12
|
+
|
|
13
|
+
attr_reader :provider, :id, :name, :source, :release_date, :last_updated,
|
|
14
|
+
:context_window, :max_output_tokens, :input_modalities, :output_modalities,
|
|
15
|
+
:capabilities, :reasoning_controls, :pricing
|
|
16
|
+
|
|
17
|
+
def initialize(provider:, id:, name: nil, source: nil, release_date: nil, last_updated: nil,
|
|
18
|
+
context_window: nil, max_output_tokens: nil, input_modalities: [], output_modalities: [],
|
|
19
|
+
capabilities: {}, reasoning_controls: nil, pricing: nil)
|
|
20
|
+
@provider = non_empty_string(provider, :provider)
|
|
21
|
+
@id = non_empty_string(id, :id)
|
|
22
|
+
@name = (name || id).to_s.freeze
|
|
23
|
+
@source = source&.to_sym
|
|
24
|
+
@release_date = normalize_date(release_date, :release_date)
|
|
25
|
+
@last_updated = normalize_date(last_updated, :last_updated)
|
|
26
|
+
@context_window = non_negative_integer(context_window, :context_window)
|
|
27
|
+
@max_output_tokens = non_negative_integer(max_output_tokens, :max_output_tokens)
|
|
28
|
+
@input_modalities = normalize_modalities(input_modalities)
|
|
29
|
+
@output_modalities = normalize_modalities(output_modalities)
|
|
30
|
+
@capabilities = normalize_capabilities(capabilities)
|
|
31
|
+
@reasoning_controls = normalize_reasoning_controls(reasoning_controls)
|
|
32
|
+
@pricing = Pricing.new(pricing) if pricing
|
|
33
|
+
freeze
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# Returns true, false, or nil when support for the capability is unknown.
|
|
37
|
+
def supports?(capability)
|
|
38
|
+
capabilities[capability.to_sym]
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def supports_reasoning?
|
|
42
|
+
supports?(:reasoning)
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
def supports_input?(modality)
|
|
46
|
+
input_modalities.include?(modality.to_sym)
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def supports_output?(modality)
|
|
50
|
+
output_modalities.include?(modality.to_sym)
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Canonical, UI-safe reasoning values supported by this definition.
|
|
54
|
+
# Provider-native controls remain available through #reasoning_controls.
|
|
55
|
+
def reasoning_options
|
|
56
|
+
return [].freeze unless supports?(:reasoning) == true
|
|
57
|
+
|
|
58
|
+
options = [ :default ]
|
|
59
|
+
control = preferred_reasoning_control
|
|
60
|
+
return options.freeze unless control
|
|
61
|
+
|
|
62
|
+
case control[:type]
|
|
63
|
+
when "effort"
|
|
64
|
+
options << :none if control[:values].include?("none")
|
|
65
|
+
options.concat(control[:values].filter_map { |value| canonical_positive_reasoning_value(value) })
|
|
66
|
+
when "budget_tokens"
|
|
67
|
+
options << :none
|
|
68
|
+
options.concat(REASONING_LEVELS)
|
|
69
|
+
when "toggle"
|
|
70
|
+
options.concat(%i[none high])
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
options.uniq.freeze
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
# Resolves a canonical reasoning option into a model control. Budget controls
|
|
77
|
+
# use the catalog model's output limit, or a conservative ceiling when absent.
|
|
78
|
+
def reasoning_control_for(option)
|
|
79
|
+
canonical = normalize_reasoning_option_name(option)
|
|
80
|
+
return { type: :none }.freeze if canonical == :none
|
|
81
|
+
|
|
82
|
+
raise ArgumentError, "Unsupported reasoning option #{option.inspect} for #{provider}/#{id}" unless reasoning_options.include?(canonical)
|
|
83
|
+
|
|
84
|
+
return nil if canonical == :default
|
|
85
|
+
|
|
86
|
+
control = preferred_reasoning_control
|
|
87
|
+
case control[:type]
|
|
88
|
+
when "effort"
|
|
89
|
+
{ type: :effort, value: effort_for(control, canonical) }.freeze
|
|
90
|
+
when "budget_tokens"
|
|
91
|
+
{ type: :budget_tokens, value: budget_for(control, canonical) }.freeze
|
|
92
|
+
when "toggle"
|
|
93
|
+
{ type: :toggle, value: true }.freeze
|
|
94
|
+
end
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
private
|
|
98
|
+
|
|
99
|
+
def non_empty_string(value, name)
|
|
100
|
+
normalized = value.to_s
|
|
101
|
+
raise ArgumentError, "#{name} must not be empty" if normalized.strip.empty?
|
|
102
|
+
|
|
103
|
+
normalized.freeze
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def non_negative_integer(value, name)
|
|
107
|
+
return nil if value.nil?
|
|
108
|
+
|
|
109
|
+
normalized = Integer(value)
|
|
110
|
+
if normalized.negative? || (value.is_a?(Numeric) && value != normalized)
|
|
111
|
+
raise ArgumentError
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
normalized
|
|
115
|
+
rescue TypeError, ArgumentError, RangeError
|
|
116
|
+
raise ArgumentError, "#{name} must be a non-negative integer"
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def normalize_modalities(modalities)
|
|
120
|
+
Array(modalities).map(&:to_sym).uniq.freeze
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def normalize_date(value, name)
|
|
124
|
+
return nil if value.nil?
|
|
125
|
+
return value if value.is_a?(Date)
|
|
126
|
+
raise ArgumentError, "#{name} must be an ISO-8601 date" unless value.is_a?(String)
|
|
127
|
+
|
|
128
|
+
Date.iso8601(value)
|
|
129
|
+
rescue Date::Error
|
|
130
|
+
raise ArgumentError, "#{name} must be an ISO-8601 date"
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def normalize_reasoning_option_name(option)
|
|
134
|
+
normalized = option.to_sym
|
|
135
|
+
normalized == :xhigh ? :max : normalized
|
|
136
|
+
rescue NoMethodError
|
|
137
|
+
raise ArgumentError, "Invalid reasoning option #{option.inspect}"
|
|
138
|
+
end
|
|
139
|
+
|
|
140
|
+
def canonical_positive_reasoning_value(value)
|
|
141
|
+
return :max if value == "xhigh"
|
|
142
|
+
|
|
143
|
+
normalized = value.to_sym
|
|
144
|
+
normalized if REASONING_LEVELS.include?(normalized)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def positive_reasoning_value?(value)
|
|
148
|
+
!canonical_positive_reasoning_value(value).nil?
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def preferred_reasoning_control
|
|
152
|
+
controls = reasoning_controls || []
|
|
153
|
+
controls.find { |control| control[:type] == "effort" && control[:values].any? { |value| positive_reasoning_value?(value) } } ||
|
|
154
|
+
controls.find { |control| control[:type] == "budget_tokens" } ||
|
|
155
|
+
controls.find { |control| control[:type] == "toggle" } ||
|
|
156
|
+
controls.find { |control| control[:type] == "effort" }
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
def effort_for(control, canonical)
|
|
160
|
+
control[:values].find { |value| canonical_positive_reasoning_value(value) == canonical }
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def budget_for(control, canonical)
|
|
164
|
+
minimum = round_up_to_reasoning_block(control[:min] || REASONING_BLOCK_SIZE)
|
|
165
|
+
maximum = [ control[:max] || DEFAULT_REASONING_BUDGET_CEILING, max_output_tokens || DEFAULT_REASONING_BUDGET_CEILING ].min
|
|
166
|
+
maximum = round_down_to_reasoning_block(maximum)
|
|
167
|
+
raise ArgumentError, "No valid reasoning budget for #{provider}/#{id}" if maximum < minimum
|
|
168
|
+
|
|
169
|
+
minimum_blocks = minimum / REASONING_BLOCK_SIZE
|
|
170
|
+
maximum_blocks = maximum / REASONING_BLOCK_SIZE
|
|
171
|
+
level_index = REASONING_LEVELS.index(canonical)
|
|
172
|
+
blocks = minimum_blocks + ((maximum_blocks - minimum_blocks) * level_index.to_f / (REASONING_LEVELS.length - 1)).round
|
|
173
|
+
blocks * REASONING_BLOCK_SIZE
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def round_up_to_reasoning_block(value)
|
|
177
|
+
((value + REASONING_BLOCK_SIZE - 1) / REASONING_BLOCK_SIZE) * REASONING_BLOCK_SIZE
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def round_down_to_reasoning_block(value)
|
|
181
|
+
(value / REASONING_BLOCK_SIZE) * REASONING_BLOCK_SIZE
|
|
182
|
+
end
|
|
183
|
+
|
|
184
|
+
def normalize_reasoning_controls(controls)
|
|
185
|
+
return nil if controls.nil?
|
|
186
|
+
raise ArgumentError, "reasoning_controls must be an array" unless controls.is_a?(Array)
|
|
187
|
+
|
|
188
|
+
controls.map.with_index do |option, index|
|
|
189
|
+
raise ArgumentError, "reasoning_controls[#{index}] must be an object" unless option.is_a?(Hash)
|
|
190
|
+
|
|
191
|
+
normalize_reasoning_option(option, index)
|
|
192
|
+
end.freeze
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def normalize_reasoning_option(option, index)
|
|
196
|
+
normalized = normalize_source_value(option)
|
|
197
|
+
type = normalized[:type]
|
|
198
|
+
unless type.is_a?(String) && !type.empty?
|
|
199
|
+
raise ArgumentError, "reasoning_controls[#{index}].type must be a non-empty string"
|
|
200
|
+
end
|
|
201
|
+
|
|
202
|
+
case type
|
|
203
|
+
when "effort"
|
|
204
|
+
values = normalized[:values]
|
|
205
|
+
unless values.is_a?(Array) && values.all? { |value| value.is_a?(String) }
|
|
206
|
+
raise ArgumentError, "reasoning_controls[#{index}].values must contain strings"
|
|
207
|
+
end
|
|
208
|
+
normalized[:values] = values.uniq.freeze
|
|
209
|
+
when "budget_tokens"
|
|
210
|
+
%i[min max].each do |boundary|
|
|
211
|
+
next unless normalized.key?(boundary) && !normalized[boundary].nil?
|
|
212
|
+
|
|
213
|
+
normalized[boundary] = non_negative_integer(normalized[boundary], "reasoning_controls[#{index}].#{boundary}")
|
|
214
|
+
end
|
|
215
|
+
if normalized[:min] && normalized[:max] && normalized[:max] < normalized[:min]
|
|
216
|
+
raise ArgumentError, "reasoning_controls[#{index}].max must not be below min"
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
deep_freeze(normalized)
|
|
221
|
+
end
|
|
222
|
+
|
|
223
|
+
def normalize_source_value(value)
|
|
224
|
+
case value
|
|
225
|
+
when String, Numeric, TrueClass, FalseClass, NilClass then value
|
|
226
|
+
when Array then value.map { |item| normalize_source_value(item) }
|
|
227
|
+
when Hash
|
|
228
|
+
value.each_with_object({}) do |(key, item), normalized|
|
|
229
|
+
unless key.is_a?(String) || key.is_a?(Symbol)
|
|
230
|
+
raise ArgumentError, "reasoning option keys must be strings"
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
normalized[key.to_sym] = normalize_source_value(item)
|
|
234
|
+
end
|
|
235
|
+
else
|
|
236
|
+
raise ArgumentError, "reasoning option attributes must contain JSON values"
|
|
237
|
+
end
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
def deep_freeze(value)
|
|
241
|
+
case value
|
|
242
|
+
when Hash then value.each { |key, item| deep_freeze(key); deep_freeze(item) }
|
|
243
|
+
when Array then value.each { |item| deep_freeze(item) }
|
|
244
|
+
end
|
|
245
|
+
value.freeze
|
|
246
|
+
end
|
|
247
|
+
|
|
248
|
+
def normalize_capabilities(capabilities)
|
|
249
|
+
capabilities.to_h.each_with_object({}) do |(name, supported), normalized|
|
|
250
|
+
unless supported.nil? || supported == true || supported == false
|
|
251
|
+
raise ArgumentError, "Capability #{name.inspect} must be true, false, or nil"
|
|
252
|
+
end
|
|
253
|
+
|
|
254
|
+
normalized[name.to_sym] = supported
|
|
255
|
+
end.freeze
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
public
|
|
259
|
+
|
|
260
|
+
class Pricing
|
|
261
|
+
attr_reader :input, :output, :cache_read, :cache_write, :tiers
|
|
262
|
+
|
|
263
|
+
def initialize(attributes)
|
|
264
|
+
attributes = attributes.to_h.transform_keys(&:to_sym)
|
|
265
|
+
@input = decimal(attributes.fetch(:input))
|
|
266
|
+
@output = decimal(attributes.fetch(:output))
|
|
267
|
+
@cache_read = decimal(attributes.fetch(:cache_read, 0))
|
|
268
|
+
@cache_write = decimal(attributes.fetch(:cache_write, 0))
|
|
269
|
+
@tiers = Array(attributes[:tiers]).map { |tier| Tier.new(tier) }.freeze
|
|
270
|
+
freeze
|
|
271
|
+
end
|
|
272
|
+
|
|
273
|
+
def rates_for(input_tokens)
|
|
274
|
+
tiers.reduce(self) do |rates, tier|
|
|
275
|
+
tier.input_tokens_above < input_tokens && tier.input_tokens_above > rates.threshold ? tier : rates
|
|
276
|
+
end
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
def threshold
|
|
280
|
+
-1
|
|
281
|
+
end
|
|
282
|
+
|
|
283
|
+
private
|
|
284
|
+
|
|
285
|
+
def decimal(value)
|
|
286
|
+
decimal = BigDecimal(value.to_s)
|
|
287
|
+
raise ArgumentError, "Pricing rates must be finite and non-negative" unless decimal.finite? && !decimal.negative?
|
|
288
|
+
|
|
289
|
+
decimal
|
|
290
|
+
rescue TypeError, ArgumentError
|
|
291
|
+
raise ArgumentError, "Pricing rates must be finite and non-negative"
|
|
292
|
+
end
|
|
293
|
+
|
|
294
|
+
def non_negative_integer(value, name)
|
|
295
|
+
normalized = Integer(value)
|
|
296
|
+
if normalized.negative? || (value.is_a?(Numeric) && value != normalized)
|
|
297
|
+
raise ArgumentError
|
|
298
|
+
end
|
|
299
|
+
|
|
300
|
+
normalized
|
|
301
|
+
rescue TypeError, ArgumentError, RangeError
|
|
302
|
+
raise ArgumentError, "#{name} must be a non-negative integer"
|
|
303
|
+
end
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
class Tier < Pricing
|
|
307
|
+
attr_reader :input_tokens_above
|
|
308
|
+
|
|
309
|
+
def initialize(attributes)
|
|
310
|
+
attributes = attributes.to_h.transform_keys(&:to_sym)
|
|
311
|
+
@input_tokens_above = non_negative_integer(
|
|
312
|
+
attributes.fetch(:input_tokens_above),
|
|
313
|
+
"Tier input_tokens_above"
|
|
314
|
+
)
|
|
315
|
+
|
|
316
|
+
super
|
|
317
|
+
end
|
|
318
|
+
|
|
319
|
+
def threshold
|
|
320
|
+
input_tokens_above
|
|
321
|
+
end
|
|
322
|
+
end
|
|
323
|
+
end
|
|
324
|
+
end
|
|
325
|
+
end
|