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 TranslatorChunkHelpers

Legion::Extensions::Llm::Vllm::TranslatorChunkHelpers::FALLBACK_STOP_REASON

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.



762
763
764
# File 'lib/legion/extensions/llm/vllm/translator.rb', line 762

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

Instance Method Details

#capabilitiesObject



803
804
805
806
807
808
809
810
811
812
813
# File 'lib/legion/extensions/llm/vllm/translator.rb', line 803

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,
    streaming_token_usage: true
  }.freeze
end

#parse_chunk(raw) ⇒ Object



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

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 Legion::JSON::ParseError => e
  handle_exception(e, level: :warn, handled: true, operation: 'vllm.translator.parse_chunk')
  nil
rescue StandardError => e
  handle_exception(e, level: :error, handled: false, operation: 'vllm.translator.parse_chunk')
  raise
end

#parse_response(wire) ⇒ Object



775
776
777
778
779
780
781
782
783
784
785
786
# File 'lib/legion/extensions/llm/vllm/translator.rb', line 775

def parse_response(wire)
  return canonical_error_response(wire) unless wire.is_a?(Hash)
  return Canonical::Response.from_hash(wire) if canonical_response?(wire)

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

#render_request(request) ⇒ Object



766
767
768
769
770
771
772
773
# File 'lib/legion/extensions/llm/vllm/translator.rb', line 766

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