| 1 | package Langertha::Engine::SGLang; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: SGLang inference server | ||||||||||||||||||||||||||||
| 3 | 16 | our $VERSION = '0.505'; | |||||||||||||||||||||||||||
| 4 | 16 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 16 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 16 | extends 'Langertha::Engine::OpenAIBase'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | # StreamUsage: ChatCompletionRequest has stream_options.include_usage | ||||||||||||||||||||||||||||
| 10 | # (python/sglang/srt/entrypoints/openai/protocol.py, ADR 0038). | ||||||||||||||||||||||||||||
| 11 | 16 | with 'Langertha::Role::Tools', | |||||||||||||||||||||||||||
| 12 | 'Langertha::Role::Embedding', | ||||||||||||||||||||||||||||
| 13 | 'Langertha::Role::Runtime::MetricsPoll', | ||||||||||||||||||||||||||||
| 14 | 'Langertha::Role::RuntimeKnobs', | ||||||||||||||||||||||||||||
| 15 | 'Langertha::Role::StreamUsage'; | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # SGLang's per-request runtime knobs: cache_salt, extra_key, priority, | ||||||||||||||||||||||||||||
| 18 | # return_cached_tokens_details. Speculative decoding is a server-launch flag | ||||||||||||||||||||||||||||
| 19 | # (--speculative-*), not a request knob — see Langertha::Runtime::Knobs. | ||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub _build_knob_wire_format { 'sglang' } | ||||||||||||||||||||||||||
|
Subroutine
_build_knob_wire_format: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | # Thinking on/off goes through the chat template on this server: | ||||||||||||||||||||||||||||
| 23 | # chat_template_kwargs.enable_thinking (+ reasoning_effort when on), karr k382. | ||||||||||||||||||||||||||||
| 24 | 37 | ▶ | sub _build_reasoning_wire_format { 'chat_template_kwargs' } | ||||||||||||||||||||||||||
|
Subroutine
_build_reasoning_wire_format: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 16 | has '+url' => ( | |||||||||||||||||||||||||||
| 28 | required => 1, | ||||||||||||||||||||||||||||
| 29 | ); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 54 | ▶ | sub default_model { 'default' } | ||||||||||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 32 | # No fixed embedding model: the caller's model, else no model field (the vLLM | ||||||||||||||||||||||||||||
| 33 | # rule, k297); the server embeds with the model it was launched with (k309). | ||||||||||||||||||||||||||||
| 34 | 4 | ▶ | sub default_embedding_model { undef } | ||||||||||||||||||||||||||
|
Subroutine
default_embedding_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # LANGERTHA_SGLANG_API_KEY is derived from the class name; a local server | ||||||||||||||||||||||||||||
| 37 | # needs no key, a --api-key-protected `sglang.launch_server` does. | ||||||||||||||||||||||||||||
| 38 | 3 | ▶ | sub api_key_required { 0 } | ||||||||||||||||||||||||||
|
Subroutine
api_key_required: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 32 | ▶ | sub _build_api_key { | ||||||||||||||||||||||||||
|
Subroutine
_build_api_key: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 32 | return $ENV{LANGERTHA_SGLANG_API_KEY}; | |||||||||||||||||||||||||||
| 42 | } | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 63 | ▶ | sub _build_supported_operations {[qw( | ||||||||||||||||||||||||||
|
Subroutine
_build_supported_operations: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | createChatCompletion | ||||||||||||||||||||||||||||
| 46 | createCompletion | ||||||||||||||||||||||||||||
| 47 | createEmbedding | ||||||||||||||||||||||||||||
| 48 | )]} | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | # tool_choice: all four forms stay. ChatCompletionRequest.tool_choice is | ||||||||||||||||||||||||||||
| 51 | # auto|required|none or a named function, default auto | ||||||||||||||||||||||||||||
| 52 | # (python/sglang/srt/entrypoints/openai/protocol.py, source-checked 2026-09-25, | ||||||||||||||||||||||||||||
| 53 | # karr k244), and serving_chat honors none. The tool_parser docs list only | ||||||||||||||||||||||||||||
| 54 | # required and named because those two need the grammar backend (xgrammar, the | ||||||||||||||||||||||||||||
| 55 | # default); that omission, not a wire rejection, was why k138 cleared auto and | ||||||||||||||||||||||||||||
| 56 | # none. Every tool field needs the server started with --tool-call-parser. | ||||||||||||||||||||||||||||
| 57 | # prompt_cache_key (OpenAI's cache-routing hint) is not a field of SGLang's | ||||||||||||||||||||||||||||
| 58 | # ChatCompletionRequest, a pydantic model that silently drops unknown keys | ||||||||||||||||||||||||||||
| 59 | # (python/sglang/srt/entrypoints/openai/protocol.py, checked 2026-09-25, karr | ||||||||||||||||||||||||||||
| 60 | # #200). Prefix-cache control is cache_salt / extra_key via RuntimeKnobs. | ||||||||||||||||||||||||||||
| 61 | 301 | ▶ | around engine_capabilities => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 301 | my ( $orig, $self, @rest ) = @_; | |||||||||||||||||||||||||||
| 63 | 301 | my $caps = $self->$orig(@rest); | |||||||||||||||||||||||||||
| 64 | 301 | delete $caps->{prompt_cache_key}; | |||||||||||||||||||||||||||
| 65 | # image_input (k266, ADR 0019): self-hosted: the served model is launch state the client cannot see, so no claim. | ||||||||||||||||||||||||||||
| 66 | 301 | delete $caps->{image_input}; | |||||||||||||||||||||||||||
| 67 | 301 | return $caps; | |||||||||||||||||||||||||||
| 68 | 16 | }; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | # karr k245: SGLang rejects tool_choice 'required' or a named tool together | ||||||||||||||||||||||||||||
| 71 | # with an output constraint ("tool_choice 'required' or a named tool cannot be | ||||||||||||||||||||||||||||
| 72 | # combined with response_format, regex, or ebnf", ValueError in | ||||||||||||||||||||||||||||
| 73 | # python/sglang/srt/entrypoints/openai/protocol.py since 307a90f6d3 / | ||||||||||||||||||||||||||||
| 74 | # 17ba2c2e7c, source-checked 2026-09-25): json_schema, json_object (becomes | ||||||||||||||||||||||||||||
| 75 | # json_schema '{"type":"object"}') and structural_tag constrain, text does not. | ||||||||||||||||||||||||||||
| 76 | # tool_choice auto + response_format is accepted. The server exempts parsers | ||||||||||||||||||||||||||||
| 77 | # whose tool constraint is full_assistant_ebnf; that is a launch flag Langertha | ||||||||||||||||||||||||||||
| 78 | # cannot see, so the rule holds for every model (qr//), like Groq/Cerebras. | ||||||||||||||||||||||||||||
| 79 | 16 | my %SGLANG_CONSTRAINING_RF = map { $_ => 1 } qw( json_schema json_object structural_tag ); | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 16 | ▶ | sub model_capability_exclusions { | ||||||||||||||||||||||||||
|
Subroutine
model_capability_exclusions: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 82 | return ( | ||||||||||||||||||||||||||||
| 83 | 16 | qr// => \&_exclude_forced_tool_choice_with_response_format, | |||||||||||||||||||||||||||
| 84 | ); | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 16 | ▶ | sub _exclude_forced_tool_choice_with_response_format { | ||||||||||||||||||||||||||
|
Subroutine
_exclude_forced_tool_choice_with_response_format: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 88 | 16 | my ( $self, %request ) = @_; | |||||||||||||||||||||||||||
| 89 | 16 | ▶ | return unless $request{has_tools} && $request{tool_choice_forced}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$request{"has_tools"} and $request{"tool_choice_forced"}
MC/DC100%2/2
$request{"has_tools"} and $request{"tool_choice_forced"}
$request{"has_tools"}
$request{"tool_choice_forced"}
Truth table100%3/3
A and B
A= $request{"has_tools"}B= $request{"tool_choice_forced"}
| |||||||||||||||||||||||||||||
| 90 | 13 | my $rf = $request{response_format}; | |||||||||||||||||||||||||||
| 91 | 13 | ▶ | my $type = ( ref $rf eq 'HASH' ) ? ( $rf->{type} // '' ) : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$$rf{"type"} // ""
MC/DC0%0/1
$$rf{"type"} // ""
$$rf{"type"}
Truth table50%1/2
A
A= $$rf{"type"}
| |||||||||||||||||||||||||||||
| 92 | 13 | ▶ | return unless $SGLANG_CONSTRAINING_RF{$type}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 10 | croak "".(ref $self)." cannot combine a forced tool_choice (required or a " | |||||||||||||||||||||||||||
| 94 | ."named tool) with response_format $type in one request: the SGLang server " | ||||||||||||||||||||||||||||
| 95 | ."rejects it (the tool-call constraint and the output constraint cannot both " | ||||||||||||||||||||||||||||
| 96 | ."be honored) with HTTP 400. Use tool_choice auto, or drop the response_format."; | ||||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 16 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 16 | 1; | |||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | __END__ | ||||||||||||||||||||||||||||