Class: Legion::LLM::Inference::Request
- Inherits:
-
Data
- Object
- Data
- Legion::LLM::Inference::Request
- Defined in:
- lib/legion/llm/inference/request.rb
Instance Attribute Summary collapse
-
#agent ⇒ Object
readonly
Returns the value of attribute agent.
-
#billing ⇒ Object
readonly
Returns the value of attribute billing.
-
#body_model_hint_decision ⇒ Object
readonly
Returns the value of attribute body_model_hint_decision.
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#caller ⇒ Object
readonly
Returns the value of attribute caller.
-
#classification ⇒ Object
readonly
Returns the value of attribute classification.
-
#context_strategy ⇒ Object
readonly
Returns the value of attribute context_strategy.
-
#conversation_id ⇒ Object
readonly
Returns the value of attribute conversation_id.
-
#enrichments ⇒ Object
readonly
Returns the value of attribute enrichments.
-
#extra ⇒ Object
readonly
Returns the value of attribute extra.
-
#fork ⇒ Object
readonly
Returns the value of attribute fork.
-
#generation ⇒ Object
readonly
Returns the value of attribute generation.
-
#hooks ⇒ Object
readonly
Returns the value of attribute hooks.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#idempotency_key ⇒ Object
readonly
Returns the value of attribute idempotency_key.
-
#messages ⇒ Object
readonly
Returns the value of attribute messages.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#modality ⇒ Object
readonly
Returns the value of attribute modality.
-
#predictions ⇒ Object
readonly
Returns the value of attribute predictions.
-
#priority ⇒ Object
readonly
Returns the value of attribute priority.
-
#response_format ⇒ Object
readonly
Returns the value of attribute response_format.
-
#routing ⇒ Object
readonly
Returns the value of attribute routing.
-
#routing_context ⇒ Object
readonly
Returns the value of attribute routing_context.
-
#routing_settings_snapshot ⇒ Object
readonly
Returns the value of attribute routing_settings_snapshot.
-
#schema_version ⇒ Object
readonly
Returns the value of attribute schema_version.
-
#stop ⇒ Object
readonly
Returns the value of attribute stop.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
-
#system ⇒ Object
readonly
Returns the value of attribute system.
-
#test ⇒ Object
readonly
Returns the value of attribute test.
-
#thinking ⇒ Object
readonly
Returns the value of attribute thinking.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
-
#tool_choice ⇒ Object
readonly
Returns the value of attribute tool_choice.
-
#tools ⇒ Object
readonly
Returns the value of attribute tools.
-
#tracing ⇒ Object
readonly
Returns the value of attribute tracing.
-
#trusted_constraints ⇒ Object
readonly
Returns the value of attribute trusted_constraints.
-
#ttl ⇒ Object
readonly
Returns the value of attribute ttl.
Class Method Summary collapse
- .auto_routing_model?(model) ⇒ Boolean
-
.build(routing_context: nil, **kwargs) ⇒ Object
SSOT v3 §7.2 additive build order.
-
.build_for_test(routing_seed:, **keywords) ⇒ Object
SSOT v3 §7.2 test helper: delegates to the production normalization path, substituting only a deterministic RoutingContext.
- .default_auto_routing_intent ⇒ Object
- .from_chat_args(**kwargs) ⇒ Object
- .normalize_auto_routing(routing, extra) ⇒ Object
- .normalize_hash(value) ⇒ Object
-
.trusted_from_routing(routing, settings_snapshot) ⇒ Object
Derive trusted constraints from a legacy
routinghash for callers not yet migrated to pass an explicit trusted_constraints value.
Instance Attribute Details
#agent ⇒ Object (readonly)
Returns the value of attribute agent
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def agent @agent end |
#billing ⇒ Object (readonly)
Returns the value of attribute billing
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def billing @billing end |
#body_model_hint_decision ⇒ Object (readonly)
Returns the value of attribute body_model_hint_decision
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def body_model_hint_decision @body_model_hint_decision end |
#cache ⇒ Object (readonly)
Returns the value of attribute cache
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def cache @cache end |
#caller ⇒ Object (readonly)
Returns the value of attribute caller
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def caller @caller end |
#classification ⇒ Object (readonly)
Returns the value of attribute classification
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def classification @classification end |
#context_strategy ⇒ Object (readonly)
Returns the value of attribute context_strategy
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def context_strategy @context_strategy end |
#conversation_id ⇒ Object (readonly)
Returns the value of attribute conversation_id
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def conversation_id @conversation_id end |
#enrichments ⇒ Object (readonly)
Returns the value of attribute enrichments
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def enrichments @enrichments end |
#extra ⇒ Object (readonly)
Returns the value of attribute extra
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def extra @extra end |
#fork ⇒ Object (readonly)
Returns the value of attribute fork
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def fork @fork end |
#generation ⇒ Object (readonly)
Returns the value of attribute generation
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def generation @generation end |
#hooks ⇒ Object (readonly)
Returns the value of attribute hooks
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def hooks @hooks end |
#id ⇒ Object (readonly)
Returns the value of attribute id
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def id @id end |
#idempotency_key ⇒ Object (readonly)
Returns the value of attribute idempotency_key
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def idempotency_key @idempotency_key end |
#messages ⇒ Object (readonly)
Returns the value of attribute messages
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def @messages end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def @metadata end |
#modality ⇒ Object (readonly)
Returns the value of attribute modality
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def modality @modality end |
#predictions ⇒ Object (readonly)
Returns the value of attribute predictions
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def predictions @predictions end |
#priority ⇒ Object (readonly)
Returns the value of attribute priority
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def priority @priority end |
#response_format ⇒ Object (readonly)
Returns the value of attribute response_format
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def response_format @response_format end |
#routing ⇒ Object (readonly)
Returns the value of attribute routing
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def routing @routing end |
#routing_context ⇒ Object (readonly)
Returns the value of attribute routing_context
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def routing_context @routing_context end |
#routing_settings_snapshot ⇒ Object (readonly)
Returns the value of attribute routing_settings_snapshot
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def routing_settings_snapshot @routing_settings_snapshot end |
#schema_version ⇒ Object (readonly)
Returns the value of attribute schema_version
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def schema_version @schema_version end |
#stop ⇒ Object (readonly)
Returns the value of attribute stop
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def stop @stop end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def stream @stream end |
#system ⇒ Object (readonly)
Returns the value of attribute system
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def system @system end |
#test ⇒ Object (readonly)
Returns the value of attribute test
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def test @test end |
#thinking ⇒ Object (readonly)
Returns the value of attribute thinking
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def thinking @thinking end |
#tokens ⇒ Object (readonly)
Returns the value of attribute tokens
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def tokens @tokens end |
#tool_choice ⇒ Object (readonly)
Returns the value of attribute tool_choice
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def tool_choice @tool_choice end |
#tools ⇒ Object (readonly)
Returns the value of attribute tools
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def tools @tools end |
#tracing ⇒ Object (readonly)
Returns the value of attribute tracing
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def tracing @tracing end |
#trusted_constraints ⇒ Object (readonly)
Returns the value of attribute trusted_constraints
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def trusted_constraints @trusted_constraints end |
#ttl ⇒ Object (readonly)
Returns the value of attribute ttl
14 15 16 |
# File 'lib/legion/llm/inference/request.rb', line 14 def ttl @ttl end |
Class Method Details
.auto_routing_model?(model) ⇒ Boolean
166 167 168 169 170 171 172 |
# File 'lib/legion/llm/inference/request.rb', line 166 def self.auto_routing_model?(model) routing_settings = Legion::Settings.dig(:llm, :routing) || {} configured = routing_settings[:auto_routing_model_aliases] aliases = Array(configured).map { |entry| entry.to_s.strip.downcase }.reject(&:empty?) aliases = [AUTO_ROUTING_MODEL_KEY] if aliases.empty? aliases.include?(model.to_s.strip.downcase) end |
.build(routing_context: nil, **kwargs) ⇒ Object
SSOT v3 §7.2 additive build order. routing_context is injected only by
build_for_test; otherwise a fresh server seed is created here. The new
trusted fields are always populated (derived from existing routing kwargs
when a caller has not yet migrated), so every Request carries them.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/legion/llm/inference/request.rb', line 33 def self.build(routing_context: nil, **kwargs) routing, extra = normalize_auto_routing( kwargs.fetch(:routing, { provider: nil, model: nil }), kwargs.fetch(:extra, {}) ) ctx = routing_context || Legion::LLM::RoutingContext.build settings_snapshot = kwargs[:routing_settings_snapshot] || Legion::LLM::Router::SettingsState.current trusted = kwargs[:trusted_constraints] || trusted_from_routing(routing, settings_snapshot) body_decision = Legion::LLM::Router::BodyModelHintPolicy.call( body_model: kwargs[:client_model], trusted_model: trusted.model, settings_snapshot: settings_snapshot ) new( routing_context: ctx, routing_settings_snapshot: settings_snapshot, trusted_constraints: trusted, body_model_hint_decision: body_decision, id: kwargs[:id] || "req_#{SecureRandom.hex(12)}", conversation_id: kwargs[:conversation_id], idempotency_key: kwargs[:idempotency_key], schema_version: kwargs.fetch(:schema_version, '1.0.0'), system: kwargs[:system], messages: kwargs.fetch(:messages, []), tools: kwargs.key?(:tools) ? kwargs[:tools] : nil, tool_choice: kwargs.fetch(:tool_choice, { mode: :auto }), routing: routing, tokens: kwargs.fetch(:tokens, { max: 4096 }), stop: kwargs.fetch(:stop, { sequences: [] }), generation: kwargs.fetch(:generation, {}), thinking: kwargs[:thinking], response_format: kwargs.fetch(:response_format, { type: :text }), stream: kwargs.fetch(:stream, false), fork: kwargs[:fork], context_strategy: kwargs.fetch(:context_strategy, :auto), cache: kwargs.fetch(:cache, { strategy: :default, cacheable: true }), priority: kwargs.fetch(:priority, :normal), ttl: kwargs[:ttl], extra: extra, metadata: kwargs.fetch(:metadata, {}), enrichments: kwargs.fetch(:enrichments, {}), predictions: kwargs.fetch(:predictions, {}), tracing: kwargs[:tracing], classification: kwargs[:classification], caller: kwargs[:caller], agent: kwargs[:agent], billing: kwargs[:billing], test: kwargs[:test], modality: kwargs[:modality], hooks: kwargs[:hooks] ) end |
.build_for_test(routing_seed:, **keywords) ⇒ Object
SSOT v3 §7.2 test helper: delegates to the production normalization path, substituting only a deterministic RoutingContext. It cannot bypass body policy, constraint derivation, or request freezing.
150 151 152 |
# File 'lib/legion/llm/inference/request.rb', line 150 def self.build_for_test(routing_seed:, **keywords) build(routing_context: Legion::LLM::RoutingContext.for_test(routing_seed: routing_seed), **keywords) end |
.default_auto_routing_intent ⇒ Object
174 175 176 177 178 |
# File 'lib/legion/llm/inference/request.rb', line 174 def self.default_auto_routing_intent intent = Legion::Settings[:llm][:routing][:default_intent] intent = intent.is_a?(Hash) ? normalize_hash(intent) : {} intent.merge(operation: :chat, effort: :moderate) end |
.from_chat_args(**kwargs) ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/legion/llm/inference/request.rb', line 86 def self.from_chat_args(**kwargs) request_id = kwargs[:request_id] || kwargs[:id] = [] if kwargs[:messages] = kwargs[:messages] elsif kwargs[:message] msg = kwargs[:message] = msg.is_a?(Array) ? msg : [{ role: :user, content: msg }] end routing = { provider: kwargs[:provider], model: kwargs[:model] } extra = kwargs.except( :message, :messages, :model, :provider, :system, :tools, :tool_choice, :stream, :caller, :classification, :billing, :agent, :test, :tracing, :priority, :conversation_id, :request_id, :id, :generation, :thinking, :response_format, :context_strategy, :cache, :fork, :tokens, :stop, :modality, :hooks, :idempotency_key, :ttl, :metadata, :enrichments, :predictions ) build_args = { messages: , system: kwargs[:system], routing: routing, tools: kwargs.key?(:tools) ? kwargs[:tools] : nil, tool_choice: kwargs[:tool_choice] || { mode: :auto }, stream: kwargs.fetch(:stream, false), generation: kwargs[:generation] || {}, thinking: kwargs[:thinking], response_format: kwargs[:response_format] || { type: :text }, context_strategy: kwargs.fetch(:context_strategy, :auto), cache: kwargs[:cache] || { strategy: :default, cacheable: true }, fork: kwargs[:fork], tokens: kwargs[:tokens] || { max: 4096 }, stop: kwargs[:stop] || { sequences: [] }, modality: kwargs[:modality], hooks: kwargs[:hooks], caller: kwargs[:caller], classification: kwargs[:classification], billing: kwargs[:billing], agent: kwargs[:agent], test: kwargs[:test], tracing: kwargs[:tracing], priority: kwargs.fetch(:priority, :normal), conversation_id: kwargs[:conversation_id], idempotency_key: kwargs[:idempotency_key], ttl: kwargs[:ttl], metadata: kwargs[:metadata] || {}, enrichments: kwargs[:enrichments] || {}, predictions: kwargs[:predictions] || {}, extra: extra } build_args[:id] = request_id if request_id build(**build_args) end |
.normalize_auto_routing(routing, extra) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/legion/llm/inference/request.rb', line 180 def self.normalize_auto_routing(routing, extra) normalized_routing = normalize_hash(routing) normalized_extra = normalize_hash(extra) return [normalized_routing, normalized_extra] unless auto_routing_model?(normalized_routing[:model]) normalized_routing = normalized_routing.dup normalized_routing[:model] = nil normalized_extra = normalized_extra.dup normalized_extra[:requested_model_alias] = Legion::LLM::Inference::AUTO_ROUTING_MODEL_KEY if normalized_routing.values_at(:provider, :instance, :instance_id, :provider_instance).compact.any? || normalized_extra[:tier] return [normalized_routing, normalized_extra] end normalized_extra[:intent] ||= default_auto_routing_intent normalized_extra[:auto_route] = true [normalized_routing, normalized_extra] end |
.normalize_hash(value) ⇒ Object
199 200 201 202 203 204 205 |
# File 'lib/legion/llm/inference/request.rb', line 199 def self.normalize_hash(value) return {} unless value.is_a?(Hash) value.each_with_object({}) do |(key, hash_value), normalized| normalized[key.respond_to?(:to_sym) ? key.to_sym : key] = hash_value end end |
.trusted_from_routing(routing, settings_snapshot) ⇒ Object
Derive trusted constraints from a legacy routing hash for callers not
yet migrated to pass an explicit trusted_constraints value. The routing
hash is trusted internal input (not an untrusted client body field).
157 158 159 160 161 162 163 164 |
# File 'lib/legion/llm/inference/request.rb', line 157 def self.trusted_from_routing(routing, settings_snapshot) routing ||= {} instance = routing[:instance] || routing[:instance_id] || routing[:provider_instance] Legion::LLM::Router::HeaderConstraints.from_internal( provider: routing[:provider], instance_id: instance, model: routing[:model], tier: routing[:tier], maximum_attempts: nil, settings_snapshot: settings_snapshot ) end |