blib/lib/Langertha/Engine/vLLM.pm

filter: Statementstmt 100% 24 / 24 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 66.6% 2 / 3 totaltotal 97.2% 35 / 36 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
_build_knob_wire_format
1.0
CRAP 1.0
« blib/lib/Langertha/Request/HTTP.pm ↑ Summary blib/lib/Langertha/Engine/OpenAIBase.pm »
1 package Langertha::Engine::vLLM;
2 # ABSTRACT: vLLM inference server
3 31 our $VERSION = '0.503';
4 31 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 31 extends 'Langertha::Engine::OpenAIBase';
8  
9 31 with 'Langertha::Role::Tools',
10 'Langertha::Role::Embedding',
11 'Langertha::Role::Runtime::MetricsPoll',
12 'Langertha::Role::RuntimeKnobs';
13  
14 # vLLM's only per-request runtime knob is cache_salt (prefix-cache isolation,
15 # vLLM v1.x+). Speculative decoding is a server-launch flag
16 # (--speculative-config), not a request knob — see Langertha::Runtime::Knobs.
17 3 ▶sub _build_knob_wire_format { 'vllm' }
Subroutine _build_knob_wire_format: called CC=1 SCAR=0.0
18  
19 # prompt_cache_key (OpenAI's cache-routing hint) is not a field of vLLM's
20 # ChatCompletionRequest: the pydantic model accepts extra keys and ignores them
21 # with a debug log (vllm/entrypoints/serve/engine/protocol.py; only the
22 # /v1/responses protocol declares it; checked 2026-09-25, karr #200). The
23 # prefix-cache lever here is cache_salt via RuntimeKnobs, so clear the flag
24 # (ADR 0002 layer 2). VLLMHook inherits this.
25 328 ▶around engine_capabilities => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
26 328 my ( $orig, $self, @rest ) = @_;
27 328 my $caps = $self->$orig(@rest);
28 328 delete $caps->{prompt_cache_key};
29 # image_input (k266, ADR 0019): self-hosted: the served model is launch state the client cannot see, so no claim.
30 328 delete $caps->{image_input};
31 328 return $caps;
32 31 };
33  
34  
35 31 has '+url' => (
36 required => 1,
37 );
38  
39 64 ▶sub default_model { 'default' }
Subroutine default_model: called CC=1 SCAR=0.0
Pod documented
40 # No fixed embedding model: the caller's model, else no model field (k297).
41 14 ▶sub default_embedding_model { undef }
Subroutine default_embedding_model: called CC=1 SCAR=0.0
Pod undocumented
42  
43 # LANGERTHA_VLLM_API_KEY is derived from the class name; a local server needs
44 # no key, a --api-key-protected `vllm serve` does.
45 8 ▶sub api_key_required { 0 }
Subroutine api_key_required: called CC=1 SCAR=0.0
Pod documented
46  
47 36 ▶sub _build_api_key {
Subroutine _build_api_key: called CC=1 SCAR=0.0
48 36 return $ENV{LANGERTHA_VLLM_API_KEY};
49 }
50  
51 101 ▶sub _build_supported_operations {[qw(
Subroutine _build_supported_operations: called CC=1 SCAR=0.0
52 createChatCompletion
53 createCompletion
54 createEmbedding
55 )]}
56  
57 31 __PACKAGE__->meta->make_immutable;
58  
59  
60  
61 31 1;
62  
63 __END__
64  
65 =pod
66  
67 =encoding UTF-8
68  
69 =head1 NAME
70  
71 Langertha::Engine::vLLM - vLLM inference server
72  
73 =head1 VERSION
74  
75 version 0.503
76  
77 =head1 SYNOPSIS
78  
79 use Langertha::Engine::vLLM;
80  
81 # 1. Simple chat
82 my $vllm = Langertha::Engine::vLLM->new(
83 url => 'http://localhost:8000/v1',
84 system_prompt => 'You are a helpful assistant',
85 );
86  
87 print $vllm->simple_chat('Say something nice');
88  
89 # 2. Streaming
90 $vllm->simple_chat_stream(sub {
91 print shift->content;
92 }, 'Write a haiku about Perl');
93  
94 # 3. MCP tool calling (requires server started with tool-call-parser)
95 use Future::AsyncAwait;
96  
97 my $vllm = Langertha::Engine::vLLM->new(
98 url => 'http://localhost:8000/v1',
99 model => 'Qwen/Qwen2.5-3B-Instruct',
100 mcp_servers => [$mcp],
101 );
102  
103 my $response = await $vllm->chat_with_tools_f('Add 7 and 15');
104  
105 # 4. Multimodal input (vision-capable models: LLaVA, Qwen-VL, PaliGemma)
106 use Langertha::Content::Image;
107  
108 my $img = Langertha::Content::Image->from_url('https://example.com/cat.jpg');
109 my $resp = await $vllm->simple_chat_f({
110 role => 'user',
111 content => [ 'What is in this image?', $img ],
112 });
113  
114 # 5. Reasoning models (Qwen3, DeepSeek-R1, QwQ — server needs
115 # --reasoning-parser matching the model; reasoning_effort maps to
116 # enable_thinking via the chat template). simple_chat takes messages
117 # only — set the control on the engine, or hand it to chat_f.
118 my $thinker = Langertha::Engine::vLLM->new(
119 url => 'http://localhost:8000/v1',
120 reasoning_effort => 'high',
121 );
122  
123 print $thinker->simple_chat('Solve step by step: what is 7 factorial?');
124  
125 # … or as a per-request control:
126 my $reasoned = await $vllm->chat_f(
127 messages => ['Solve step by step: what is 7 factorial?'],
128 reasoning_effort => 'high',
129 );
130  
131 # 6. Embeddings (for embedding models: BAAI/bge-*, intfloat/e5-*, …)
132 my $vector = $vllm->simple_embedding('Some text to embed');
133  
134 # 7. Prometheus /metrics scraping (Runtime::MetricsPoll)
135 my $records = await $vllm->poll_metrics_f('vllm:');
136 # Returns ArrayRef of { name, type, value, labels } parsed from
137 # GET <url-stripped-of-/v1>/metrics.
138  
139 # 8. vLLM-Hook sub-engine (IBM vLLM-Hook plugin: hidden states, qk, steer)
140 use Langertha::Engine::VLLMHook;
141 my $hooked = Langertha::Engine::VLLMHook->new(
142 url => 'http://localhost:8770/v1',
143 vllm_xargs => { output_hidden_states => JSON::MaybeXS::true() },
144 );
145  
146 =head1 DESCRIPTION
147  
148 Provides access to vLLM, a high-throughput inference engine for large
149 language models. Extends L<Langertha::Engine::OpenAIBase> (which composes
150 L<Langertha::Role::OpenAICompatible>, L<Langertha::Role::OpenAPI>,
151 L<Langertha::Role::Models>, L<Langertha::Role::Temperature>,
152 L<Langertha::Role::ResponseSize>, L<Langertha::Role::SystemPrompt>,
153 L<Langertha::Role::ResponseFormat>, L<Langertha::Role::Streaming>,
154 L<Langertha::Role::Chat>, L<Langertha::Role::ReasoningEffort>, and
155 L<Langertha::Role::PromptCache>); vLLM itself additionally composes
156 L<Langertha::Role::Tools> (MCP tool calling), L<Langertha::Role::Embedding>
157 (OpenAI-compatible C</v1/embeddings> for embedding models), and
158 L<Langertha::Role::Runtime::MetricsPoll> (Prometheus C</metrics> scrape).
159  
160 Supports chat, streaming, tool calling, embeddings, multimodal input,
161 reasoning models, and Prometheus /metrics scraping.
162  
163 Only C<url> is required. The URL must include the C</v1> path prefix
164 (e.g., C<http://localhost:8000/v1>). Since vLLM serves exactly one model
165 (configured at server startup), no model name or API key is needed.
166  
167 =head1 TOOL CALLING
168  
169 MCP tool calling requires the vLLM server to be started with
170 C<--enable-auto-tool-choice> and C<--tool-call-parser> matching the model
171 (C<hermes> for Qwen2.5/Hermes, C<llama3> for Llama, C<mistral> for Mistral).
172  
173 =head1 EMBEDDINGS
174  
175 Composes L<Langertha::Role::Embedding>. vLLM exposes an OpenAI-compatible
176 C</v1/embeddings> endpoint when started with an embedding model
177 (BAAI/bge-*, intfloat/e5-*, …). The request carries C<embedding_model> if
178 you set it, else C<model> if you set it, else no C<model> field at all: the
179 server embeds with the model it serves. (vLLM 0.10/0.11 answer 404 to a
180 C<model> that is not a served name, such as the C<'default'> placeholder.)
181  
182 =head1 REASONING MODELS
183  
184 vLLM serves reasoning models through the OpenAI-compatible chat completions
185 endpoint. C<reasoning_effort> (L<Langertha::Role::ReasoningEffort>,
186 L<Langertha::Reasoning>, wire format C<openai>) is honored on the body —
187 vLLM translates the value into C<enable_thinking> via the chat template:
188  
189 reasoning_effort low|medium|high -> enable_thinking = true
190 reasoning_effort none -> enable_thinking = false
191 (unset) -> enable_thinking not injected
192  
193 The thought itself comes back in the C<reasoning> response field — mainline
194 vLLM renamed it from C<reasoning_content> to C<reasoning> in v0.16.0, and
195 both spellings are read — surfaced on C<< $response->thinking >>. It is only
196 populated when the server was started with the matching C<--reasoning-parser>.
197  
198 Supported reasoning-model families (vLLM's own table — verify against
199 L<https://docs.vllm.ai/en/latest/features/reasoning_outputs/> for the
200 exact list as new models land):
201  
202 =over 4
203  
204 =item * Qwen3 (C<Qwen3-Instruct> / C<Qwen3-Thinking>) — wire
205 C<reasoning_effort> honored.
206  
207 =item * DeepSeek-R1 (native) and DeepSeek-R1-Distill-* — wire
208 C<reasoning_effort> honored.
209  
210 =item * QwQ-32B — wire C<reasoning_effort> honored (served via the
211 C<deepseek_r1> reasoning parser).
212  
213 =item * Gemma 4, IBM Granite 3.2, DeepSeek-V3.1 / V4 — also honor the
214 parameter via the same chat-template auto-injection path.
215  
216 =back
217  
218 The server MUST be started with the matching C<--reasoning-parser> flag
219 for the loaded model (C<deepseek_r1> for DeepSeek-R1 / QwQ, C<qwen3>
220 for Qwen3 thinking, etc.) — without it, the model emits reasoning
221 content but Langertha cannot surface it on L<Langertha::Response>.
222 See vLLM's reasoning-outputs docs for the canonical flag list.
223  
224 Per ADR 0009 the wire format is C<openai> on vLLM; engines diverging
225 (Anthropic, Gemini) carry their own C<reasoning_wire_format> tag and
226 serialize independently.
227  
228 =head1 METRICS
229  
230 Composes L<Langertha::Role::Runtime::MetricsPoll>. vLLM serves Prometheus
231 text-format metrics at C<GET <url-stripped-of-/v1>/metrics>; pass a prefix
232 to L</poll_metrics_f> (e.g. C<'vllm:'> for the engine's own gauges) or
233 filter downstream via L<Langertha::Runtime::Metrics/filter_prefix>.
234  
235 =head1 SUB-ENGINE: VLLMHook
236  
237 L<Langertha::Engine::VLLMHook> extends this engine for servers running the
238 IBM vLLM-Hook plugin (hidden_states / qk / steer probes); it injects a
239 top-level C<vllm_xargs> field and lifts captured tensors onto
240 L<Langertha::Response/probes>. See ADR 0004 for the seam.
241  
242 See L<https://docs.vllm.ai/> for installation and configuration details.
243  
244 =head1 CAPABILITIES
245  
246 Advertised flags (derived from composed roles via L<Langertha::Role::Capabilities>):
247  
248 =over 4
249  
250 =item * C<chat> — L<Langertha::Role::Chat>
251  
252 =item * C<streaming> — L<Langertha::Role::Streaming>
253  
254 =item * C<tools_native> + C<tool_choice_{auto,any,none,named}> — L<Langertha::Role::Tools>
255  
256 =item * C<embedding> — L<Langertha::Role::Embedding>
257  
258 =item * C<runtime_metrics> — L<Langertha::Role::Runtime::MetricsPoll>
259  
260 =item * C<response_format_{json_object,json_schema}> — L<Langertha::Role::ResponseFormat>
261  
262 =item * C<temperature> — L<Langertha::Role::Temperature>
263  
264 =item * C<reasoning_effort> — L<Langertha::Role::ReasoningEffort>; honored by Qwen3 / DeepSeek-R1 / QwQ / Gemma 4 / Granite 3.2 when vLLM is started with C<--reasoning-parser>. See L</REASONING MODELS>.
265  
266 =item * C<response_size>, C<system_prompt>, C<parallel_tool_use>, C<context_size>, C<seed>
267 — generation-parameter knobs the engine will honour
268  
269 =back
270  
271 =head1 SEE ALSO
272  
273 =over
274  
275 =item * L<https://docs.vllm.ai/> - vLLM documentation
276  
277 =item * L<Langertha::Engine::OpenAIBase> - Base class for OpenAI-compatible engines
278  
279 =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format role
280  
281 =item * L<Langertha::Role::Tools> - MCP tool calling interface
282  
283 =item * L<Langertha::Role::Embedding> - Embedding request/response interface
284  
285 =item * L<Langertha::Role::Runtime::MetricsPoll> - Prometheus /metrics scraper
286  
287 =item * L<Langertha::Engine::VLLMHook> - Sub-engine for the IBM vLLM-Hook plugin
288  
289 =item * L<Langertha::Engine::LlamaCpp> - Sister self-hosted OpenAI-compatible engine (also Embedding + MetricsPoll)
290  
291 =item * L<Langertha::Engine::SGLang> - Sister self-hosted OpenAI-compatible engine (also MetricsPoll)
292  
293 =item * L<Langertha::Engine::OllamaOpenAI> - Ollama's OpenAI-compatible endpoint
294  
295 =back
296  
297 =head1 SUPPORT
298  
299 =head2 Issues
300  
301 Please report bugs and feature requests on GitHub at
302 L<https://github.com/Getty/langertha/issues>.
303  
304 =head2 IRC
305  
306 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
307  
308 =head1 CONTRIBUTING
309  
310 Contributions are welcome! Please fork the repository and submit a pull request.
311  
312 =head1 AUTHOR
313  
314 Torsten Raudssus <getty@cpan.org>
315  
316 =head1 COPYRIGHT AND LICENSE
317  
318 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
319  
320 This is free software; you can redistribute it and/or modify it under
321 the same terms as the Perl 5 programming language system itself.
322  
323 =cut
« blib/lib/Langertha/Request/HTTP.pm ↑ Summary blib/lib/Langertha/Engine/OpenAIBase.pm »