Class: Legion::Extensions::Llm::Vllm::Translator

Inherits:
Object
  • Object
show all
Includes:
StopReasonMapping, TranslatorChunkBuilderHelpers, TranslatorChunkHelpers, TranslatorMessageHelpers, TranslatorParamHelpers, TranslatorRenderHelpers, TranslatorResponseHelpers, TranslatorThinkingHelpers, TranslatorToolCallHelpers, TranslatorToolCallParseHelpers, TranslatorToolHelpers, Logging::Helper
Defined in:
lib/legion/extensions/llm/vllm/translator.rb

Overview

Canonical provider translator for vLLM (OpenAI-compatible wire format).

Implements render_request, parse_response, parse_chunk, and capabilities.

vLLM quirks declared in capabilities:

  • tool_calls_as_text: true — some models output tool calls as JSON text.
  • forced_tool_choice: true — named tool choices require explicit references.
  • thinking_tags: %w[think thinking] — Qwen-style reasoning in tags.

Constant Summary

Constants included from TranslatorParamHelpers

Legion::Extensions::Llm::Vllm::TranslatorParamHelpers::PARAM_WIRE_KEYS, Legion::Extensions::Llm::Vllm::TranslatorParamHelpers::SUPPORTED_PARAMS

Instance Method Summary collapse

Constructor Details

#initialize(config: nil) ⇒ Translator

Returns a new instance of Translator.



749
750
751
# File 'lib/legion/extensions/llm/vllm/translator.rb', line 749

def initialize(config: nil)
  @config = config
end

Instance Method Details

#capabilitiesObject



799
800
801
802
803
804
805
806
807
808
809
# File 'lib/legion/extensions/llm/vllm/translator.rb', line 799

def capabilities
  {
    provider: 'vllm',
    wire_format: 'openai_compatible',
    tool_calls_as_text: true,
    forced_tool_choice: true,
    thinking_tags: %w[think thinking],
    stop_reason_map: stop_reason_map.merge(stop_reason_map_additions),
    streaming_token_usage: true
  }.freeze
end

#parse_chunk(raw) ⇒ Object

V10: the base streaming layer hands this boundary parsed frames only (Hash, or a canonical-shaped chunk on the explicit conformance edge) — an unparseable frame is a classified failure upstream (M1), never a silent nil here. The dead ParseError → nil fail-open is deleted.



787
788
789
790
791
792
793
794
795
796
797
# File 'lib/legion/extensions/llm/vllm/translator.rb', line 787

def parse_chunk(raw)
  data = coerce_chunk_input(raw)
  return nil if data.nil?
  return handle_canonical_chunk(data) if data['type']
  return error_chunk_from_hash(data) if data['error']

  parse_openai_chunk(data)
rescue StandardError => e
  handle_exception(e, level: :error, handled: false, operation: 'vllm.translator.parse_chunk')
  raise
end

#parse_response(wire) ⇒ Object

V16: a non-completion body is a transport/contract fault, not a provider-returned error response — it fails loud (raises) instead of completing the call with a successful :error response the ledger would record as a success. The canonical_error_response fail-open is deleted.



767
768
769
770
771
772
773
774
775
776
777
778
779
780
# File 'lib/legion/extensions/llm/vllm/translator.rb', line 767

def parse_response(wire)
  unless wire.is_a?(Hash)
    raise ArgumentError,
          "vllm.parse_response: expected a Hash completion body, got #{wire.class}"
  end
  return Canonical::Response.from_hash(wire) if canonical_response?(wire)
  raise ArgumentError, 'vllm.parse_response: completion body carries no choices' \
    unless wire['choices'].is_a?(Array)

  build_canonical_response(wire)
rescue StandardError => e
  handle_exception(e, level: :error, handled: false, operation: 'vllm.translator.parse_response')
  raise
end

#render_request(request) ⇒ Object



753
754
755
756
757
758
759
760
# File 'lib/legion/extensions/llm/vllm/translator.rb', line 753

def render_request(request)
  model = extract_wire_model(request)
  messages = format_messages(request)
  payload = { model: model, messages: messages, stream: request.stream }
  apply_payload_options(payload, request)
  log_rendered_request(model, messages, request, payload)
  payload
end