« blib/lib/Langertha/Engine/MoonshotAnthropic.pm
↑ Summary
blib/lib/Langertha/Role/Runtime/MetricsPoll.pm »
| 1 | package Langertha::Engine::Remote; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Base class for all remote engines | ||||||||||||||||||||||||||||
| 3 | 178 | our $VERSION = '0.503'; | |||||||||||||||||||||||||||
| 4 | 178 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 178 | ▶ | use Langertha::RateLimit; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 178 | with map { 'Langertha::Role::'.$_ } qw( | |||||||||||||||||||||||||||
| 9 | JSON | ||||||||||||||||||||||||||||
| 10 | HTTP | ||||||||||||||||||||||||||||
| 11 | PluginHost | ||||||||||||||||||||||||||||
| 12 | ); | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 178 | has '+url' => ( | |||||||||||||||||||||||||||
| 16 | required => 1, | ||||||||||||||||||||||||||||
| 17 | ); | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 50 | ▶ | sub api_key_env { | ||||||||||||||||||||||||||
|
Subroutine
api_key_env: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 20 | 50 | my ( $class ) = @_; | |||||||||||||||||||||||||||
| 21 | 50 | (my $name = $class) =~ s/^Langertha::Engine:://; | |||||||||||||||||||||||||||
| 22 | 50 | return 'LANGERTHA_'.uc($name).'_API_KEY'; | |||||||||||||||||||||||||||
| 23 | } | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 29 | ▶ | sub api_key_required { | ||||||||||||||||||||||||||
|
Subroutine
api_key_required: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 27 | 29 | my ( $class ) = @_; | |||||||||||||||||||||||||||
| 28 | 29 | ▶ | return defined $class->api_key_env ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 178 | has _last_rate_limit => ( | |||||||||||||||||||||||||||
| 33 | is => 'rw', | ||||||||||||||||||||||||||||
| 34 | isa => 'Maybe[Langertha::RateLimit]', | ||||||||||||||||||||||||||||
| 35 | predicate => '_has_last_rate_limit', | ||||||||||||||||||||||||||||
| 36 | clearer => '_clear_last_rate_limit', | ||||||||||||||||||||||||||||
| 37 | init_arg => undef, | ||||||||||||||||||||||||||||
| 38 | ); | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 83 | ▶ | sub rate_limit { | ||||||||||||||||||||||||||
|
Subroutine
rate_limit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 41 | 83 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 42 | 83 | return $self->_last_rate_limit; | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 287 | ▶ | sub has_rate_limit { | ||||||||||||||||||||||||||
|
Subroutine
has_rate_limit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 287 | my ( $self ) = @_; | |||||||||||||||||||||||||||
| 48 | 287 | return defined $self->_last_rate_limit; | |||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 1347 | ▶ | sub _update_rate_limit { | ||||||||||||||||||||||||||
|
Subroutine
_update_rate_limit: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 53 | 1347 | my ( $self, $http_response ) = @_; | |||||||||||||||||||||||||||
| 54 | # No headers clears: rate_limit describes the latest response, never an | ||||||||||||||||||||||||||||
| 55 | # older one (karr k300). | ||||||||||||||||||||||||||||
| 56 | 1347 | my $rl = $self->_parse_rate_limit_headers($http_response); | |||||||||||||||||||||||||||
| 57 | 1347 | $self->_last_rate_limit($rl); | |||||||||||||||||||||||||||
| 58 | 1347 | return $rl; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # The fallback for engines without a dialect parser (Gemini, Ollama native, | ||||||||||||||||||||||||||||
| 62 | # AKI native, LM Studio native): no buckets to normalize, but a Retry-After or | ||||||||||||||||||||||||||||
| 63 | # retry-after-ms on a 429/503 is still what a caller backs off from, so it | ||||||||||||||||||||||||||||
| 64 | # gets a RateLimit carrying retry_after and raw. No per-engine parsers (k312). | ||||||||||||||||||||||||||||
| 65 | 303 | ▶ | sub _parse_rate_limit_headers { | ||||||||||||||||||||||||||
|
Subroutine
_parse_rate_limit_headers: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 66 | 303 | my ( $self, $http_response ) = @_; | |||||||||||||||||||||||||||
| 67 | 303 | require Langertha::RateLimit; | |||||||||||||||||||||||||||
| 68 | 303 | require Langertha::Moment; | |||||||||||||||||||||||||||
| 69 | 303 | my %raw = Langertha::RateLimit::_collect_headers($http_response); | |||||||||||||||||||||||||||
| 70 | 303 | ▶ | return undef unless defined $raw{'retry-after'} || defined $raw{'retry-after-ms'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $raw{"retry-after"} or defined $raw{"retry-after-ms"}
MC/DC100%2/2
defined $raw{"retry-after"} or defined $raw{"retry-after-ms"}
defined $raw{"retry-after"}
defined $raw{"retry-after-ms"}
Truth table100%3/3
A or B
A= defined $raw{"retry-after"}B= defined $raw{"retry-after-ms"}
| |||||||||||||||||||||||||||||
| 71 | 10 | return Langertha::RateLimit->new( received => Langertha::Moment->now_utc, raw => \%raw ); | |||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | # Wire-agnostic generation-parameter block shared by every chat dialect | ||||||||||||||||||||||||||||
| 75 | # (OpenAI, Anthropic, ...). Extracts the can-guarded sibling calls for | ||||||||||||||||||||||||||||
| 76 | # reasoning_kwargs_for / prompt_cache_kwargs_for; the dialect-specific | ||||||||||||||||||||||||||||
| 77 | # fields (response_format on OpenAI, inference_geo on Anthropic, ...) stay | ||||||||||||||||||||||||||||
| 78 | # inline because their placement in the request body depends on dialect | ||||||||||||||||||||||||||||
| 79 | # conventions. Temperature also stays inline: OpenAI emits seed between | ||||||||||||||||||||||||||||
| 80 | # temperature and reasoning, so folding temperature in here would shift | ||||||||||||||||||||||||||||
| 81 | # seed and break the byte order of the OpenAI body (karr #98, ADR 0015 | ||||||||||||||||||||||||||||
| 82 | # Decision 3). The helper name is deliberately not knobs_kwargs_for - | ||||||||||||||||||||||||||||
| 83 | # that name belongs to Langertha::Role::RuntimeKnobs for the self-hosted | ||||||||||||||||||||||||||||
| 84 | # prefix-cache knobs (ADR 0012). | ||||||||||||||||||||||||||||
| 85 | 2386 | ▶ | sub generation_kwargs_for { | ||||||||||||||||||||||||||
|
Subroutine
generation_kwargs_for: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | 2386 | my ( $self, %controls ) = @_; | |||||||||||||||||||||||||||
| 87 | return ( | ||||||||||||||||||||||||||||
| 88 | 2386 | ▶ | ( $self->can('reasoning_kwargs_for') ? $self->reasoning_kwargs_for(%controls) : () ), | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 89 | ( $self->can('prompt_cache_kwargs_for') ? $self->prompt_cache_kwargs_for(%controls) : () ), | ||||||||||||||||||||||||||||
| 90 | ); | ||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 178 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | 178 | 1; | |||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | __END__ | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | =pod | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =encoding UTF-8 | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =head1 NAME | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Langertha::Engine::Remote - Base class for all remote engines | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head1 VERSION | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | version 0.503 | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | package My::Engine; | ||||||||||||||||||||||||||||
| 116 | use Moose; | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | extends 'Langertha::Engine::Remote'; | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | has '+url' => ( default => 'https://api.example.com' ); | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | sub default_model { 'my-model' } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | Root base class for all HTTP-based LLM engines in Langertha. Composes | ||||||||||||||||||||||||||||
| 127 | L<Langertha::Role::JSON>, L<Langertha::Role::HTTP>, and | ||||||||||||||||||||||||||||
| 128 | L<Langertha::Role::PluginHost>, and makes the C<url> attribute required. | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | All engines in the distribution extend this class, either directly | ||||||||||||||||||||||||||||
| 131 | (L<Langertha::Engine::Anthropic>, L<Langertha::Engine::Gemini>, | ||||||||||||||||||||||||||||
| 132 | L<Langertha::Engine::Ollama>, L<Langertha::Engine::AKI>) or via the | ||||||||||||||||||||||||||||
| 133 | OpenAI-compatible intermediate class L<Langertha::Engine::OpenAIBase>. | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head2 api_key_env | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | my $env = $class->api_key_env; | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | Class method returning the name of the environment variable this engine | ||||||||||||||||||||||||||||
| 140 | reads its API key from (C<LANGERTHA_*_API_KEY>), or C<undef> if the engine | ||||||||||||||||||||||||||||
| 141 | reads none at all. Derived from the class name by default; engines that | ||||||||||||||||||||||||||||
| 142 | share a vendor key override it (e.g. L<Langertha::Engine::AKIOpenAI> reads | ||||||||||||||||||||||||||||
| 143 | C<LANGERTHA_AKI_API_KEY>). | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | The name alone does not say whether the key is mandatory - pair it with | ||||||||||||||||||||||||||||
| 146 | L</api_key_required>. | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 api_key_required | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | my $needs_key = $class->api_key_required; | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Class method returning true if the engine is unusable without credentials. | ||||||||||||||||||||||||||||
| 153 | Together with L</api_key_env> it covers the three states an engine can be | ||||||||||||||||||||||||||||
| 154 | in: | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =over | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | =item * B<required> - C<api_key_env> names a variable and C<api_key_required> | ||||||||||||||||||||||||||||
| 159 | is true. Cloud providers; building the engine without the key croaks. | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =item * B<optional> - C<api_key_env> names a variable and C<api_key_required> | ||||||||||||||||||||||||||||
| 162 | is false. Local-first engines that also serve an authenticated deployment | ||||||||||||||||||||||||||||
| 163 | (L<Langertha::Engine::Ollama> reaching Ollama Cloud, | ||||||||||||||||||||||||||||
| 164 | L<Langertha::Engine::LMStudio> reaching a secured LM Studio): the variable | ||||||||||||||||||||||||||||
| 165 | unlocks that deployment, the engine works without it. | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =item * B<none> - C<api_key_env> is C<undef> and C<api_key_required> is | ||||||||||||||||||||||||||||
| 168 | false. The engine reads no environment variable at all | ||||||||||||||||||||||||||||
| 169 | (L<Langertha::Engine::vLLM>, L<Langertha::Engine::SGLang>, | ||||||||||||||||||||||||||||
| 170 | L<Langertha::Engine::LlamaCpp>, L<Langertha::Engine::Whisper>); pass | ||||||||||||||||||||||||||||
| 171 | C<api_key> explicitly when the server was started with one. | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | =back | ||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | Derived from L</api_key_env> by default, so only engines with an optional | ||||||||||||||||||||||||||||
| 176 | key override it. | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | =head2 rate_limit | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | my $rl = $engine->rate_limit; | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Returns the L<Langertha::RateLimit> from the most recent API response, | ||||||||||||||||||||||||||||
| 183 | or C<undef> if that response carried no rate limit headers — a response | ||||||||||||||||||||||||||||
| 184 | without them clears the previous one, so the value always describes the | ||||||||||||||||||||||||||||
| 185 | latest response. Error responses count: a C<429> (or any other non-2xx) is | ||||||||||||||||||||||||||||
| 186 | recorded B<before> the request croaks or its future fails, on the synchronous | ||||||||||||||||||||||||||||
| 187 | and every asynchronous path, so a caller can read C<requests_remaining>, the | ||||||||||||||||||||||||||||
| 188 | resets and L<Langertha::RateLimit/retry_after> after catching the error. | ||||||||||||||||||||||||||||
| 189 | Engines whose wire has no rate-limit headers (Gemini, Ollama native, AKI | ||||||||||||||||||||||||||||
| 190 | native, LM Studio native) still get a rate limit when a response sends | ||||||||||||||||||||||||||||
| 191 | C<Retry-After> or C<retry-after-ms>: it carries only | ||||||||||||||||||||||||||||
| 192 | L<Langertha::RateLimit/retry_after> and L<Langertha::RateLimit/raw>. | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | =head2 has_rate_limit | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | if ($engine->has_rate_limit) { ... } | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | Returns true if the engine has rate limit data from the most recent response. | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =head2 generation_kwargs_for | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | my @kwargs = $engine->generation_kwargs_for(%$controls); | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Emits the wire-agnostic generation-parameter kwargs that every chat dialect | ||||||||||||||||||||||||||||
| 205 | shares - the C<reasoning_kwargs_for> and C<prompt_cache_kwargs_for> outputs, | ||||||||||||||||||||||||||||
| 206 | each gated by C<can()> so engines without those roles stay quiet. C<%controls> | ||||||||||||||||||||||||||||
| 207 | is the per-request controls hash from C<chat_f> (karr #46), passed wholesale; | ||||||||||||||||||||||||||||
| 208 | keys it does not carry fall back to the engine attributes inside the | ||||||||||||||||||||||||||||
| 209 | underlying L<Langertha::Role::ReasoningEffort/reasoning_kwargs_for> and | ||||||||||||||||||||||||||||
| 210 | L<Langertha::Role::PromptCache/prompt_cache_kwargs_for>. Returned as a list | ||||||||||||||||||||||||||||
| 211 | suitable for spreading into a request body. Dialect-specific emission | ||||||||||||||||||||||||||||
| 212 | (C<temperature>, C<response_format>, C<seed>, C<knobs_kwargs_for>, | ||||||||||||||||||||||||||||
| 213 | C<inference_geo>, ...) stays at the call site because each dialect positions | ||||||||||||||||||||||||||||
| 214 | those fields at a specific place in the body. | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =over | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | =item * L<Langertha::Engine::OpenAIBase> - Intermediate base for all OpenAI-compatible engines | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =item * L<Langertha::Engine::Anthropic> - Anthropic Claude (extends this directly) | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | =item * L<Langertha::Engine::Gemini> - Google Gemini (extends this directly) | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | =item * L<Langertha::Engine::Ollama> - Ollama native API (extends this directly) | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =item * L<Langertha::Engine::AKI> - AKI EU engine (extends this directly) | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | =item * L<Langertha::Role::HTTP> - HTTP transport with C<url>, C<user_agent>, request builders | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =item * L<Langertha::Role::JSON> - Shared JSON encoder/decoder | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =item * L<Langertha::Role::PluginHost> - Plugin system with lifecycle events | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =item * L<Langertha::RateLimit> - Rate limit data stored per-engine | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | =back | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | =head2 Issues | ||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||
| 244 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||
| 245 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||
| 247 | =head2 IRC | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 264 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =cut | ||||||||||||||||||||||||||||