Module: Legion::LLM::API::OpenAI::Responses
- Extended by:
- Legion::Logging::Helper
- Defined in:
- lib/legion/llm/api/openai/responses.rb
Class Method Summary collapse
-
.build_handler ⇒ Object
rubocop:disable Metrics/MethodLength.
- .build_output_tool_calls(pipeline_response) ⇒ Object
- .build_tool_declarations(tools) ⇒ Object
- .build_usage(tokens) ⇒ Object
- .call_streaming_executor(executor, upstream_body: nil) ⇒ Object
- .extract_token(tokens, key) ⇒ Object
- .flush_pending_tool_calls(messages, pending) ⇒ Object
- .format_response(pipeline_response, request_id:, model:) ⇒ Object
- .normalize_input_array(input) ⇒ Object
- .registered(app) ⇒ Object
- .sse_event(name, payload) ⇒ Object
-
.stream_response(out, executor, request_id:, model:, upstream_body: nil) ⇒ Object
rubocop:disable Metrics/MethodLength.
- .token_aliases(key) ⇒ Object
- .token_method(key) ⇒ Object
Class Method Details
.build_handler ⇒ Object
rubocop:disable Metrics/MethodLength
25 26 27 28 29 30 31 32 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 85 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 |
# File 'lib/legion/llm/api/openai/responses.rb', line 25 def self.build_handler # rubocop:disable Metrics/MethodLength proc do # rubocop:disable Metrics/BlockLength require_llm! body = parse_request_body request_id = "resp_#{SecureRandom.hex(16)}" input = body[:input] = case input when Array Responses.normalize_input_array(input) when String [{ role: 'user', content: input }] else halt 400, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: 'input is required (string or array)', type: 'invalid_request_error', code: nil } }) end = [{ role: 'system', content: body[:instructions].to_s }] + if body[:instructions] model = body[:model] || Legion::Settings[:llm][:default_model] || 'default' streaming = body[:stream] == true tool_declarations = Responses.build_tool_declarations(body[:tools]) log.info( "[llm][api][openai][responses] action=accepted request_id=#{request_id} " \ "model=#{model} stream=#{streaming} tools=#{tool_declarations.size}" ) effective_caller = build_server_caller(source: 'openai_responses', path: request.path, env: env) require 'legion/llm/inference/request' unless defined?(Legion::LLM::Inference::Request) require 'legion/llm/inference/executor' unless defined?(Legion::LLM::Inference::Executor) inference_request = Legion::LLM::Inference::Request.build( id: request_id, messages: , routing: { model: model }, tools: tool_declarations, caller: effective_caller, stream: streaming, cache: { strategy: :default, cacheable: true } ) executor = Legion::LLM::Inference::Executor.new(inference_request) if streaming content_type 'text/event-stream' headers 'Cache-Control' => 'no-cache', 'Connection' => 'keep-alive', 'X-Accel-Buffering' => 'no' stream do |out| Responses.stream_response(out, executor, request_id: request_id, model: model, upstream_body: body) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.responses.stream', request_id: request_id) out << "event: error\ndata: #{Legion::JSON.dump({ type: 'server_error', message: e. })}\n\n" end else pipeline_response = executor.call_responses(body: body, stream: false) response_body = Responses.format_response(pipeline_response, request_id: request_id, model: model) log.info("[llm][api][openai][responses] action=complete request_id=#{request_id} model=#{response_body[:model]}") content_type :json status 200 Legion::JSON.dump(response_body) end rescue Legion::LLM::AuthError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.openai.responses.auth') halt 401, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: e., type: 'authentication_error' } }) rescue Legion::LLM::RateLimitError => e handle_exception(e, level: :warn, handled: true, operation: 'llm.api.openai.responses.rate_limit') halt 429, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: e., type: 'rate_limit_error' } }) rescue Legion::LLM::ProviderDown, Legion::LLM::ProviderError => e handle_exception(e, level: :error, handled: true, operation: 'llm.api.openai.responses.provider') halt 502, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: e., type: 'server_error' } }) rescue StandardError => e handle_exception(e, level: :error, handled: false, operation: 'llm.api.openai.responses') halt 500, { 'Content-Type' => 'application/json' }, Legion::JSON.dump({ error: { message: e., type: 'server_error' } }) end end |
.build_output_tool_calls(pipeline_response) ⇒ Object
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 |
# File 'lib/legion/llm/api/openai/responses.rb', line 360 def self.build_output_tool_calls(pipeline_response) tools_data = pipeline_response.respond_to?(:tools) ? pipeline_response.tools : nil return [] unless tools_data.is_a?(Array) && !tools_data.empty? tools_data.filter_map do |tc| name = tc.respond_to?(:name) ? tc.name : (tc[:name] || tc['name']) args = tc.respond_to?(:arguments) ? tc.arguments : (tc[:arguments] || tc['arguments'] || {}) tc_id = tc.respond_to?(:id) ? tc.id : (tc[:id] || tc['id'] || "call_#{SecureRandom.hex(8)}") next unless name { type: 'function_call', id: "fc_#{SecureRandom.hex(12)}", call_id: tc_id, name: name.to_s, arguments: args.is_a?(String) ? args : Legion::JSON.dump(args), status: 'completed' } end end |
.build_tool_declarations(tools) ⇒ Object
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/legion/llm/api/openai/responses.rb', line 157 def self.build_tool_declarations(tools) return [] if tools.nil? || !tools.is_a?(Array) || tools.empty? tools.filter_map do |tool| t = tool.respond_to?(:transform_keys) ? tool.transform_keys(&:to_sym) : tool fn = t[:function] || t fn = fn.transform_keys(&:to_sym) if fn.respond_to?(:transform_keys) next unless fn[:name].to_s.length.positive? Legion::LLM::Types::ToolDefinition.build( name: fn[:name].to_s, description: fn[:description].to_s, parameters: fn[:parameters] || {}, source: { type: :client, executable: true } ) rescue StandardError => e handle_exception(e, level: :warn, handled: true, operation: 'llm.api.openai.responses.build_tool') nil end end |
.build_usage(tokens) ⇒ Object
404 405 406 407 408 409 410 411 412 413 |
# File 'lib/legion/llm/api/openai/responses.rb', line 404 def self.build_usage(tokens) input_tokens = extract_token(tokens, :input_tokens) output_tokens = extract_token(tokens, :output_tokens) { input_tokens: input_tokens, output_tokens: output_tokens, total_tokens: input_tokens + output_tokens } end |
.call_streaming_executor(executor, upstream_body: nil) ⇒ Object
348 349 350 351 352 353 354 |
# File 'lib/legion/llm/api/openai/responses.rb', line 348 def self.call_streaming_executor(executor, upstream_body: nil, &) if upstream_body && executor.respond_to?(:call_responses) executor.call_responses(body: upstream_body, stream: true, &) else executor.call_stream(&) end end |
.extract_token(tokens, key) ⇒ Object
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 |
# File 'lib/legion/llm/api/openai/responses.rb', line 381 def self.extract_token(tokens, key) return 0 if tokens.nil? aliases = token_aliases(key) if tokens.is_a?(Hash) aliases.each do |candidate| value = tokens[candidate] value = tokens[candidate.to_s] if value.nil? return value.to_i unless value.nil? end return 0 end aliases.each do |candidate| method_name = token_method(candidate) return tokens.public_send(method_name).to_i if method_name && tokens.respond_to?(method_name) end 0 end |
.flush_pending_tool_calls(messages, pending) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/legion/llm/api/openai/responses.rb', line 144 def self.flush_pending_tool_calls(, pending) return if pending.empty? << { role: 'assistant', content: '', tool_calls: pending.map do |tc| { id: tc[:id], type: 'function', function: { name: tc[:name], arguments: tc[:arguments] } } end } pending.clear end |
.format_response(pipeline_response, request_id:, model:) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/legion/llm/api/openai/responses.rb', line 178 def self.format_response(pipeline_response, request_id:, model:) routing = pipeline_response.routing || {} tokens = pipeline_response.tokens || {} raw_msg = pipeline_response. content = raw_msg.is_a?(Hash) ? (raw_msg[:content] || raw_msg['content']).to_s : raw_msg.to_s resolved_model = (routing[:model] || routing['model'] || model).to_s output = [] tool_calls = build_output_tool_calls(pipeline_response) output.concat(tool_calls) output << { type: 'message', id: "msg_#{SecureRandom.hex(12)}", role: 'assistant', content: [{ type: 'output_text', text: content }], status: 'completed' } { id: request_id, object: 'response', created_at: Time.now.to_i, model: resolved_model, output: output, usage: build_usage(tokens), status: 'completed' } end |
.normalize_input_array(input) ⇒ Object
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 |
# File 'lib/legion/llm/api/openai/responses.rb', line 112 def self.normalize_input_array(input) = [] pending_tool_calls = [] input.each do |item| item = item.transform_keys(&:to_sym) if item.respond_to?(:transform_keys) case item[:type]&.to_s when 'function_call' pending_tool_calls << { id: item[:call_id] || item[:id], name: item[:name].to_s, arguments: item[:arguments].is_a?(String) ? item[:arguments] : Legion::JSON.dump(item[:arguments] || {}) } when 'function_call_output' flush_pending_tool_calls(, pending_tool_calls) << { role: 'tool', tool_call_id: item[:call_id], content: item[:output].to_s } else flush_pending_tool_calls(, pending_tool_calls) role = item[:role]&.to_s next unless role role = 'system' if role == 'developer' content = item[:content] content = content.to_s if content && !content.is_a?(Array) << { role: role, content: content }.compact end end flush_pending_tool_calls(, pending_tool_calls) end |
.registered(app) ⇒ Object
14 15 16 17 18 19 20 21 22 23 |
# File 'lib/legion/llm/api/openai/responses.rb', line 14 def self.registered(app) log.debug('[llm][api][openai][responses] registering POST /v1/responses + /api/llm/inference/v1/responses') handler = build_handler app.post('/v1/responses') { instance_exec(&handler) } app.post('/api/llm/inference/v1/responses') { instance_exec(&handler) } log.debug('[llm][api][openai][responses] routes registered') end |
.sse_event(name, payload) ⇒ Object
356 357 358 |
# File 'lib/legion/llm/api/openai/responses.rb', line 356 def self.sse_event(name, payload) "event: #{name}\ndata: #{Legion::JSON.dump(payload)}\n\n" end |
.stream_response(out, executor, request_id:, model:, upstream_body: nil) ⇒ Object
rubocop:disable Metrics/MethodLength
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 |
# File 'lib/legion/llm/api/openai/responses.rb', line 209 def self.stream_response(out, executor, request_id:, model:, upstream_body: nil) # rubocop:disable Metrics/MethodLength created_at = Time.now.to_i seq = 0 in_progress_response = { id: request_id, object: 'response', created_at: created_at, status: 'in_progress', model: model, output: [], usage: nil } # response.created — envelope matches gateway format: { type:, response:, sequence_number: } out << sse_event('response.created', { type: 'response.created', sequence_number: seq += 1, response: in_progress_response }) out << sse_event('response.in_progress', { type: 'response.in_progress', sequence_number: seq += 1, response: in_progress_response }) msg_id = "msg_#{SecureRandom.hex(12)}" out << sse_event('response.output_item.added', { type: 'response.output_item.added', sequence_number: seq += 1, output_index: 0, item: { id: msg_id, type: 'message', role: 'assistant', content: [], status: 'in_progress' } }) out << sse_event('response.content_part.added', { type: 'response.content_part.added', sequence_number: seq += 1, output_index: 0, content_index: 0, item_id: msg_id, part: { type: 'output_text', text: '', annotations: [] } }) full_text = +'' pipeline_response = call_streaming_executor(executor, upstream_body: upstream_body) do |chunk| text = chunk.respond_to?(:content) ? chunk.content.to_s : chunk.to_s next if text.empty? full_text << text out << sse_event('response.output_text.delta', { type: 'response.output_text.delta', sequence_number: seq += 1, output_index: 0, content_index: 0, item_id: msg_id, delta: text }) end routing = pipeline_response.routing || {} tokens = pipeline_response.tokens || {} resolved_model = (routing[:model] || routing['model'] || model).to_s usage = build_usage(tokens) function_calls = build_output_tool_calls(pipeline_response) out << sse_event('response.output_text.done', { type: 'response.output_text.done', sequence_number: seq += 1, output_index: 0, content_index: 0, item_id: msg_id, text: full_text }) out << sse_event('response.content_part.done', { type: 'response.content_part.done', sequence_number: seq += 1, output_index: 0, content_index: 0, item_id: msg_id, part: { type: 'output_text', text: full_text, annotations: [] } }) completed_item = { id: msg_id, type: 'message', role: 'assistant', status: 'completed', content: [{ type: 'output_text', text: full_text, annotations: [] }] } out << sse_event('response.output_item.done', { type: 'response.output_item.done', sequence_number: seq += 1, output_index: 0, item: completed_item }) function_calls.each_with_index do |function_call, index| output_index = index + 1 in_progress_item = function_call.merge(status: 'in_progress', arguments: '') out << sse_event('response.output_item.added', { type: 'response.output_item.added', sequence_number: seq += 1, output_index: output_index, item: in_progress_item }) out << sse_event('response.function_call_arguments.delta', { type: 'response.function_call_arguments.delta', sequence_number: seq += 1, output_index: output_index, item_id: function_call[:id], delta: function_call[:arguments] }) out << sse_event('response.function_call_arguments.done', { type: 'response.function_call_arguments.done', sequence_number: seq += 1, output_index: output_index, item_id: function_call[:id], arguments: function_call[:arguments] }) out << sse_event('response.output_item.done', { type: 'response.output_item.done', sequence_number: seq += 1, output_index: output_index, item: function_call }) end out << sse_event('response.completed', { type: 'response.completed', sequence_number: seq + 1, response: { id: request_id, object: 'response', created_at: created_at, status: 'completed', model: resolved_model, output: [completed_item, *function_calls], usage: usage } }) log.info("[llm][api][openai][responses] action=stream_complete request_id=#{request_id} model=#{resolved_model}") end |
.token_aliases(key) ⇒ Object
415 416 417 418 419 420 421 422 423 424 |
# File 'lib/legion/llm/api/openai/responses.rb', line 415 def self.token_aliases(key) case key.to_sym when :input, :input_tokens %i[input_tokens input] when :output, :output_tokens %i[output_tokens output] else [key.to_sym] end end |
.token_method(key) ⇒ Object
426 427 428 429 430 431 432 433 |
# File 'lib/legion/llm/api/openai/responses.rb', line 426 def self.token_method(key) { input: :input_tokens, input_tokens: :input_tokens, output: :output_tokens, output_tokens: :output_tokens }[key.to_sym] end |