| 1 | package Langertha::Engine::VLLMHook; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: vLLM inference server with vLLM-Hook probe capture | ||||||||||||||||||||||||||||
| 3 | 11 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||
| 4 | 11 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use JSON::MaybeXS (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 11 | extends 'Langertha::Engine::vLLM'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | has vllm_xargs => ( | ||||||||||||||||||||||||||||
| 11 | is => 'ro', | ||||||||||||||||||||||||||||
| 12 | isa => 'HashRef', | ||||||||||||||||||||||||||||
| 13 | 31 | ▶ | default => sub { {} }, | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 11 | ); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 11 | has worker_name => ( | |||||||||||||||||||||||||||
| 18 | is => 'ro', | ||||||||||||||||||||||||||||
| 19 | isa => 'Str', | ||||||||||||||||||||||||||||
| 20 | predicate => 'has_worker_name', | ||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 20 | ▶ | sub resolved_xargs { | ||||||||||||||||||||||||||
|
Subroutine
resolved_xargs: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 20 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 27 | 20 | my $xargs = $self->vllm_xargs; | |||||||||||||||||||||||||||
| 28 | 20 | ▶ | return $xargs if keys %$xargs; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 16 | ▶ | return {} unless $self->has_worker_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 1 | ▶ | return { output_hidden_states => JSON::MaybeXS::true() } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | if $self->worker_name eq 'hidden_states'; | ||||||||||||||||||||||||||||
| 32 | 0 | return {}; | |||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 4 | ▶ | sub _encode_xargs { | ||||||||||||||||||||||||||
|
Subroutine
_encode_xargs: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 37 | 4 | my ( $self, $xargs ) = @_; | |||||||||||||||||||||||||||
| 38 | 4 | my %out; | |||||||||||||||||||||||||||
| 39 | 4 | for my $k ( keys %$xargs ) { | |||||||||||||||||||||||||||
| 40 | 5 | my $v = $xargs->{$k}; | |||||||||||||||||||||||||||
| 41 | 5 | ▶ | $out{$k} = ( ref $v eq 'HASH' || ref $v eq 'ARRAY' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $v eq "HASH" or ref $v eq "ARRAY"
MC/DC50%1/2
ref $v eq "HASH" or ref $v eq "ARRAY"
ref $v eq "HASH"
ref $v eq "ARRAY"
Truth table66%2/3
A or B
A= ref $v eq "HASH"B= ref $v eq "ARRAY"
| |||||||||||||||||||||||||||||
| 42 | ? $self->encode_json_text($v) | ||||||||||||||||||||||||||||
| 43 | : $v; | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 4 | return \%out; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 19 | ▶ | around 'chat_request' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 49 | 19 | my ( $orig, $self, $messages, %extra ) = @_; | |||||||||||||||||||||||||||
| 50 | 19 | my $xargs = $self->resolved_xargs; | |||||||||||||||||||||||||||
| 51 | 19 | ▶ | if ( keys %$xargs ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 4 | my $encoded = $self->_encode_xargs($xargs); | |||||||||||||||||||||||||||
| 53 | 4 | ▶ | my $per_call = $extra{vllm_xargs} // {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$extra{"vllm_xargs"} // {}
MC/DC0%0/1
$extra{"vllm_xargs"} // {}
$extra{"vllm_xargs"}
Truth table50%1/2
A
A= $extra{"vllm_xargs"}
| |||||||||||||||||||||||||||||
| 54 | 4 | $extra{vllm_xargs} = { %$encoded, %$per_call }; | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | 19 | return $self->$orig( $messages, %extra ); | |||||||||||||||||||||||||||
| 57 | 11 | }; | |||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 3 | ▶ | around 'chat_response' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 61 | 3 | my ( $orig, $self, $http_response ) = @_; | |||||||||||||||||||||||||||
| 62 | 3 | my $resp = $self->$orig($http_response); | |||||||||||||||||||||||||||
| 63 | 3 | ▶ | my $raw = $resp->raw // {}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$resp->raw // {}
MC/DC0%0/1
$resp->raw // {}
$resp->raw
Truth table50%1/2
A
A= $resp->raw
| |||||||||||||||||||||||||||||
| 64 | 3 | ▶ | if ( defined $raw->{probes} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 1 | $resp = $resp->clone_with( probes => $raw->{probes} ); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | 3 | return $resp; | |||||||||||||||||||||||||||
| 68 | 11 | }; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 11 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 11 | 1; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | __END__ | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =pod | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | =head1 NAME | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | Langertha::Engine::VLLMHook - vLLM inference server with vLLM-Hook probe capture | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head1 VERSION | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | version 0.503 | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | use Langertha::Engine::VLLMHook; | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | # Capture hidden states for every request | ||||||||||||||||||||||||||||
| 95 | my $engine = Langertha::Engine::VLLMHook->new( | ||||||||||||||||||||||||||||
| 96 | url => 'http://localhost:8770/v1', | ||||||||||||||||||||||||||||
| 97 | vllm_xargs => { output_hidden_states => JSON::MaybeXS::true() }, | ||||||||||||||||||||||||||||
| 98 | ); | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | my $response = $engine->simple_chat('Hello'); | ||||||||||||||||||||||||||||
| 101 | print $response->content, "\n"; | ||||||||||||||||||||||||||||
| 102 | print "probes: ", join(',', keys %{$response->probes}), "\n" | ||||||||||||||||||||||||||||
| 103 | if $response->has_probes; | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | # Drive xargs from a vLLM-Hook model config file | ||||||||||||||||||||||||||||
| 106 | use Langertha::VLLMHook::Config; | ||||||||||||||||||||||||||||
| 107 | my $cfg = Langertha::VLLMHook::Config->new( | ||||||||||||||||||||||||||||
| 108 | file => 'model_configs/hidden_states/Qwen2.5-3B-Instruct.json', | ||||||||||||||||||||||||||||
| 109 | ); | ||||||||||||||||||||||||||||
| 110 | my $engine = Langertha::Engine::VLLMHook->new( | ||||||||||||||||||||||||||||
| 111 | url => 'http://localhost:8770/v1', | ||||||||||||||||||||||||||||
| 112 | vllm_xargs => $cfg->xargs, | ||||||||||||||||||||||||||||
| 113 | ); | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Talks to a vLLM server running the vLLM-Hook plugin | ||||||||||||||||||||||||||||
| 118 | (L<https://github.com/IBM/vLLM-Hook>), which observes attention patterns, | ||||||||||||||||||||||||||||
| 119 | extracts hidden states and performs activation steering. It speaks ordinary | ||||||||||||||||||||||||||||
| 120 | OpenAI-compatible HTTP, so this engine extends L<Langertha::Engine::vLLM> and | ||||||||||||||||||||||||||||
| 121 | only adds two things: | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | =over 4 | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =item * On the way out, the C<vllm_xargs> HashRef is merged into the chat | ||||||||||||||||||||||||||||
| 126 | request body. vLLM maps the top-level C<vllm_xargs> field onto | ||||||||||||||||||||||||||||
| 127 | C<SamplingParams.extra_args>, which the plugin reads to install its hooks. | ||||||||||||||||||||||||||||
| 128 | Nested values (dicts, lists) are JSON-encoded as strings because C<vllm_xargs> | ||||||||||||||||||||||||||||
| 129 | only carries scalars — the plugin JSON-decodes them again before the worker | ||||||||||||||||||||||||||||
| 130 | reads them. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =item * On the way back, the serialized probe tensors that the plugin attaches | ||||||||||||||||||||||||||||
| 133 | as a top-level C<probes> field are lifted onto | ||||||||||||||||||||||||||||
| 134 | L<Langertha::Response/probes>. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =back | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | The server must be started with the matching worker, e.g. | ||||||||||||||||||||||||||||
| 139 | C<VLLM_HOOK_WORKER=hidden_states vllm serve E<lt>modelE<gt> --enforce-eager>. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | B<THIS API IS WORK IN PROGRESS> | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =head2 vllm_xargs | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | HashRef of extra arguments merged into the top-level C<vllm_xargs> field of | ||||||||||||||||||||||||||||
| 146 | every chat request. Activates the vLLM-Hook plugin's probes. Defaults to an | ||||||||||||||||||||||||||||
| 147 | empty HashRef. Nested HashRef/ArrayRef values are JSON-encoded as strings on | ||||||||||||||||||||||||||||
| 148 | the wire (see L</_encode_xargs>); plain scalars and JSON booleans pass through | ||||||||||||||||||||||||||||
| 149 | (a JSON boolean rides as a native JSON C<true>/C<false>). | ||||||||||||||||||||||||||||
| 150 | Recognised keys include C<output_hidden_states>, C<output_qk>, C<hookq_mode> | ||||||||||||||||||||||||||||
| 151 | and C<steer>. When non-empty this takes precedence over L</worker_name>. | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | =head2 worker_name | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | Optional convenience naming the vLLM-Hook worker the server was started with | ||||||||||||||||||||||||||||
| 156 | (C<VLLM_HOOK_WORKER>): C<qk>, C<hidden_states> or C<steer>. Used only to derive | ||||||||||||||||||||||||||||
| 157 | a default L</vllm_xargs> when none was given explicitly. C<hidden_states> | ||||||||||||||||||||||||||||
| 158 | yields C<{ output_hidden_states =E<gt> true }>; C<qk> and C<steer> need their | ||||||||||||||||||||||||||||
| 159 | layer/head map or steering dict supplied via C<vllm_xargs> and therefore | ||||||||||||||||||||||||||||
| 160 | contribute no default on their own. | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head2 resolved_xargs | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | my $xargs = $engine->resolved_xargs; | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Returns the effective C<vllm_xargs> HashRef. When L</vllm_xargs> is non-empty it | ||||||||||||||||||||||||||||
| 167 | is returned as-is; otherwise a default is derived from L</worker_name>. Returns | ||||||||||||||||||||||||||||
| 168 | an empty HashRef when neither yields anything. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head2 _encode_xargs | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | my $encoded = $engine->_encode_xargs(\%xargs); | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | Returns a copy of C<%xargs> in which every nested HashRef/ArrayRef value is | ||||||||||||||||||||||||||||
| 175 | JSON-encoded to a string, leaving plain scalars and JSON booleans untouched (a | ||||||||||||||||||||||||||||
| 176 | JSON boolean rides as a native JSON C<true>/C<false>). vLLM's C<vllm_xargs> | ||||||||||||||||||||||||||||
| 177 | only accepts scalar values, so structured values must travel as JSON strings; | ||||||||||||||||||||||||||||
| 178 | the vLLM-Hook plugin decodes them again. | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head2 chat_request | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | my $request = $engine->chat_request($messages, %extra); | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | Wraps L<Langertha::Role::OpenAICompatible/chat_request> to merge | ||||||||||||||||||||||||||||
| 185 | L</resolved_xargs> (encoded via L</_encode_xargs>) into the top-level | ||||||||||||||||||||||||||||
| 186 | C<vllm_xargs> field of the request body. A per-call C<vllm_xargs> in C<%extra> | ||||||||||||||||||||||||||||
| 187 | overrides the instance values key-by-key. | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | =head2 chat_response | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | my $response = $engine->chat_response($http_response); | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | Wraps L<Langertha::Role::OpenAICompatible/chat_response> to lift the | ||||||||||||||||||||||||||||
| 194 | serialized probe tensors from the response body's top-level C<probes> field | ||||||||||||||||||||||||||||
| 195 | onto L<Langertha::Response/probes>. Behaves exactly like the parent when the | ||||||||||||||||||||||||||||
| 196 | server returned no probes. | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =over | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | =item * L<Langertha::Engine::vLLM> - Parent engine (plain vLLM, no probes) | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | =item * L<Langertha::VLLMHook::Config> - Loads vLLM-Hook model config JSON into C<vllm_xargs> | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =item * L<Langertha::Response/probes> - Where captured probe tensors land | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | =item * L<https://github.com/IBM/vLLM-Hook> - The vLLM-Hook plugin | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | =back | ||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | =head2 Issues | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 217 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | =head2 IRC | ||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||
| 235 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 236 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =cut | ||||||||||||||||||||||||||||