« blib/lib/Langertha/Engine/TranscriptionBase.pm
↑ Summary
blib/lib/Langertha/Engine/LMStudioOpenAI.pm »
| 1 | package Langertha::Engine::AKIAnthropic; | ||||||||||||||||||||
| 2 | # ABSTRACT: AKI.IO via Anthropic-compatible API | ||||||||||||||||||||
| 3 | 15 | our $VERSION = '0.503'; | |||||||||||||||||||
| 4 | 15 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | 15 | ▶ | use Carp qw( croak ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 15 | extends 'Langertha::Engine::AnthropicBase'; | |||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | 15 | with 'Langertha::Role::StaticModels'; | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | # AnthropicBase->chat_request appends '/v1/messages' to url; the default must | ||||||||||||||||||||
| 13 | # therefore stop at '/anthropic' so the composed endpoint is a single | ||||||||||||||||||||
| 14 | # '/anthropic/v1/messages' (a '/anthropic/v1' default double-stacks to | ||||||||||||||||||||
| 15 | # '/anthropic/v1/v1/messages' -> HTTP 404). AKI.IO documents the same rule for | ||||||||||||||||||||
| 16 | # the Anthropic Python SDK: "AKI.IO API internally appends /v1/messages, so we | ||||||||||||||||||||
| 17 | # recommend using https://aki.io/anthropic for the base_url instead." | ||||||||||||||||||||
| 18 | has '+url' => ( | ||||||||||||||||||||
| 19 | lazy => 1, | ||||||||||||||||||||
| 20 | 5 | ▶ | default => sub { 'https://aki.io/anthropic' }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 15 | ); | |||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 2 | ▶ | sub _build_api_key { | ||||||||||||||||||
|
Subroutine
_build_api_key: called CC=2 SCAR=7.1
| |||||||||||||||||||||
| 24 | 2 | my ( $self ) = @_; | |||||||||||||||||||
| 25 | return $ENV{LANGERTHA_AKI_API_KEY} | ||||||||||||||||||||
| 26 | 2 | ▶ | || croak "".(ref $self)." requires LANGERTHA_AKI_API_KEY or api_key set"; | ||||||||||||||||||
|
Condition66%2/3
$ENV{'LANGERTHA_AKI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_AKI_API_KEY or api_key set')
MC/DC0%0/2
$ENV{'LANGERTHA_AKI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_AKI_API_KEY or api_key set')
$ENV{'LANGERTHA_AKI_API_KEY'}
croak('' . ref($self) . ' requires LANGERTHA_AKI_API_KEY or api_key set')
Truth table33%1/3
A || B
A= $ENV{'LANGERTHA_AKI_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_AKI_API_KEY or api_key set')
| |||||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | # gpt-oss-120b replaces the EOL llama3-chat-8b (AKI.IO end-of-life 2026-09-30) | ||||||||||||||||||||
| 31 | # as the default. AKI.IO exposes MiniMax M3 only on its native endpoint, so a | ||||||||||||||||||||
| 32 | # current non-MiniMax model AKI's own table rates "Supported" is the shim | ||||||||||||||||||||
| 33 | # default. Verified live 2026-09-10: the /anthropic shim answers gpt-oss-120b | ||||||||||||||||||||
| 34 | # and echoes it back as $response->model. -- karr k132 | ||||||||||||||||||||
| 35 | 18 | ▶ | sub default_model { 'gpt-oss-120b' } | ||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 2 | ▶ | sub api_key_env { 'LANGERTHA_AKI_API_KEY' } | ||||||||||||||||||
|
Subroutine
api_key_env: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | # AKI.IO's /anthropic shim spells the cache read the Anthropic way but counts it | ||||||||||||||||||||
| 41 | # inside input_tokens: the captures t/data/akianthropic_chat_response.json and | ||||||||||||||||||||
| 42 | # akiopenai_chat_response.json answer the same request with input_tokens 65 + | ||||||||||||||||||||
| 43 | # cache_read_input_tokens 64 here and prompt_tokens 65 / cached_tokens 64 on the | ||||||||||||||||||||
| 44 | # OpenAI face. Without this, Pricing with a cache rate bills the 64 reads twice. | ||||||||||||||||||||
| 45 | # -- ADR 0031, k265 | ||||||||||||||||||||
| 46 | 12 | ▶ | sub _usage_input_includes_cache { 1 } | ||||||||||||||||||
|
Subroutine
_usage_input_includes_cache: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | # AKI.IO's machine-readable agent config states "default_max_output_tokens": | ||||||||||||||||||||
| 49 | # 8192, which is below the documented output limit of every listed model. The | ||||||||||||||||||||
| 50 | # Anthropic dialect requires max_tokens on every request, so this overrides the | ||||||||||||||||||||
| 51 | # conservative 1024 of Langertha::Role::AnthropicCompatible. | ||||||||||||||||||||
| 52 | 64 | ▶ | sub default_response_size { 8192 } | ||||||||||||||||||
|
Subroutine
default_response_size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | # Static list from the model table on | ||||||||||||||||||||
| 55 | # https://aki.io/docs/compatibility/anthropic-api-compatibility/ — AKI.IO's own | ||||||||||||||||||||
| 56 | # docs disagree about model discovery (the Anthropic page points at | ||||||||||||||||||||
| 57 | # /anthropic/v1/models, the agent integration guide says discovery runs through | ||||||||||||||||||||
| 58 | # the OpenAI-compatible /openai/v1/models), so this engine does not depend on | ||||||||||||||||||||
| 59 | # either endpoint. Use Langertha::Engine::AKIOpenAI->list_models for a live list. | ||||||||||||||||||||
| 60 | 1 | ▶ | sub _build_static_models {[ | ||||||||||||||||||
|
Subroutine
_build_static_models: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 61 | 1 | { id => 'apertus-chat-70b' }, | |||||||||||||||||||
| 62 | { id => 'gpt-oss-120b' }, | ||||||||||||||||||||
| 63 | { id => 'gemma4-26b' }, | ||||||||||||||||||||
| 64 | { id => 'kimi-k2.7-code-1100b' }, | ||||||||||||||||||||
| 65 | { id => 'llama3-chat-8b' }, | ||||||||||||||||||||
| 66 | { id => 'llama3-chat-70b' }, | ||||||||||||||||||||
| 67 | { id => 'minimax-m2.5-230b' }, | ||||||||||||||||||||
| 68 | { id => 'mistral4-119b' }, | ||||||||||||||||||||
| 69 | { id => 'qwen3.6-35b' }, | ||||||||||||||||||||
| 70 | ]} | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | 159 | ▶ | around engine_capabilities => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 73 | 159 | my ( $orig, $self, @rest ) = @_; | |||||||||||||||||||
| 74 | 159 | my $caps = $self->$orig(@rest); | |||||||||||||||||||
| 75 | # image_input (k266, ADR 0019): vision is undocumented / unverified on this face, so no claim. | ||||||||||||||||||||
| 76 | 159 | delete $caps->{image_input}; | |||||||||||||||||||
| 77 | 159 | return $caps; | |||||||||||||||||||
| 78 | 15 | }; | |||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | # Tool-result images (karr k359): the shim takes an image inside a tool_result | ||||||||||||||||||||
| 81 | # without error but the model does not see it. Live probe 2026-09-30 | ||||||||||||||||||||
| 82 | # (llm-advisor, aki.io/anthropic/v1/messages): qwen3.6-35b read a plain | ||||||||||||||||||||
| 83 | # user-message image correctly (16x16 red -> 'Red'), but the same kind of image | ||||||||||||||||||||
| 84 | # inside a tool_result came back as 'White' (16x16 red) and 'Black' (64x64 | ||||||||||||||||||||
| 85 | # green), with ~340 input tokens whatever the image size; gpt-oss-120b answered | ||||||||||||||||||||
| 86 | # 200 and saw only the text. The k336 placeholder at least says an image was | ||||||||||||||||||||
| 87 | # there, so it stays even if an image_input row is added for this face later. | ||||||||||||||||||||
| 88 | 9 | ▶ | sub _tool_result_images_on_wire { 0 } | ||||||||||||||||||
|
Subroutine
_tool_result_images_on_wire: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | # Source blocks in a tool_result (karr k364, k366): live probes 2026-09-30 | ||||||||||||||||||||
| 91 | # (llm-advisor, aki.io/anthropic/v1/messages) -- a text document answered | ||||||||||||||||||||
| 92 | # HTTP 529 {"type":"overloaded_error","message":"Unsupported content type: | ||||||||||||||||||||
| 93 | # document"} (qwen3.6-35b), and a search_result block {source, title, | ||||||||||||||||||||
| 94 | # content:[text]} answered 529 "Unsupported content type: search_result" | ||||||||||||||||||||
| 95 | # (qwen3.6-35b twice, gpt-oss-120b once). Deterministic on AKI, not a retry | ||||||||||||||||||||
| 96 | # signal: either breaks the tool-loop turn. An MCP text resource therefore goes | ||||||||||||||||||||
| 97 | # out as a text block, a PDF as the placeholder, a native document or | ||||||||||||||||||||
| 98 | # search_result as its text. (is_error: true in the same probes was accepted | ||||||||||||||||||||
| 99 | # and silently ignored -- 200, same answer and input tokens as without it -- | ||||||||||||||||||||
| 100 | # so it is still sent, like on every other shim; k366.) | ||||||||||||||||||||
| 101 | 9 | ▶ | sub _tool_result_source_blocks_on_wire { 0 } | ||||||||||||||||||
|
Subroutine
_tool_result_source_blocks_on_wire: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 15 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 15 | 1; | |||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | =pod | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | =encoding UTF-8 | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =head1 NAME | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | Langertha::Engine::AKIAnthropic - AKI.IO via Anthropic-compatible API | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =head1 VERSION | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | version 0.503 | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =head1 SYNOPSIS | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | use Langertha::Engine::AKIAnthropic; | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | my $aki = Langertha::Engine::AKIAnthropic->new( | ||||||||||||||||||||
| 127 | api_key => $ENV{LANGERTHA_AKI_API_KEY}, | ||||||||||||||||||||
| 128 | model => 'gemma4-26b', | ||||||||||||||||||||
| 129 | ); | ||||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | print $aki->simple_chat('Hello from Perl!'); | ||||||||||||||||||||
| 132 | |||||||||||||||||||||
| 133 | =head1 DESCRIPTION | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | Provides access to L<AKI.IO|https://aki.io/> via its Anthropic-compatible | ||||||||||||||||||||
| 136 | endpoint at C<https://aki.io/anthropic> (the shared | ||||||||||||||||||||
| 137 | L<Langertha::Engine::AnthropicBase> appends the C</v1/messages> path). AKI.IO | ||||||||||||||||||||
| 138 | is a European AI model hub based in Germany — all inference runs on EU | ||||||||||||||||||||
| 139 | infrastructure, fully GDPR-compliant. | ||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | This is the third face of the same provider: L<Langertha::Engine::AKI> speaks | ||||||||||||||||||||
| 142 | the native API, L<Langertha::Engine::AKIOpenAI> the OpenAI-compatible one, and | ||||||||||||||||||||
| 143 | this class the Anthropic Messages dialect. All three authenticate with the same | ||||||||||||||||||||
| 144 | AKI.IO key (C<LANGERTHA_AKI_API_KEY>); pick whichever wire format your code | ||||||||||||||||||||
| 145 | already speaks. | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | The key is sent as a raw C<x-api-key> header (B<no> C<Bearer> prefix). The | ||||||||||||||||||||
| 148 | C<anthropic-version> header is accepted but not required by AKI.IO; the base | ||||||||||||||||||||
| 149 | class sends it anyway. | ||||||||||||||||||||
| 150 | |||||||||||||||||||||
| 151 | B<Model fallback for unknown IDs (substitute unconfirmed):> AKI.IO's own | ||||||||||||||||||||
| 152 | documentation states it does B<not> error on unknown model IDs — "Requests to | ||||||||||||||||||||
| 153 | unknown model names will fall back onto the Minimax M2.5 model." The exact | ||||||||||||||||||||
| 154 | substitute is version-dependent and Langertha could not re-confirm it live | ||||||||||||||||||||
| 155 | (2026-09-14 the C</anthropic> shim answered every request, documented models | ||||||||||||||||||||
| 156 | included, with HTTP 529), so treat the specific fallback model as | ||||||||||||||||||||
| 157 | B<unconfirmed>. Either way, Claude model names and HuggingFace repository IDs | ||||||||||||||||||||
| 158 | are B<not> a usable shortcut — an unknown ID is not the model you asked for. | ||||||||||||||||||||
| 159 | Always pass an exact AKI.IO model ID (C<list_models> returns the documented | ||||||||||||||||||||
| 160 | set), and check C<< $response->model >> (and watch for errors) if it matters | ||||||||||||||||||||
| 161 | which model replied. | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | Models documented for this endpoint: C<apertus-chat-70b>, C<gpt-oss-120b>, | ||||||||||||||||||||
| 164 | C<gemma4-26b>, C<kimi-k2.7-code-1100b>, C<llama3-chat-8b>, C<llama3-chat-70b>, | ||||||||||||||||||||
| 165 | C<minimax-m2.5-230b>, C<mistral4-119b>, C<qwen3.6-35b>. | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | B<Tool calling works but is undocumented on this endpoint.> AKI.IO's Anthropic | ||||||||||||||||||||
| 168 | compatibility page lists only C<model>, C<messages>, C<max_tokens>, | ||||||||||||||||||||
| 169 | C<temperature>, C<top_p>, C<top_k>, C<stop_sequences>, C<stream> and C<system> | ||||||||||||||||||||
| 170 | as request parameters — no C<tools>. It nevertheless accepts the native | ||||||||||||||||||||
| 171 | Anthropic C<tools> array this class inherits from | ||||||||||||||||||||
| 172 | L<Langertha::Engine::AnthropicBase> and answers with a C<tool_use> block, | ||||||||||||||||||||
| 173 | verified live against C<llama3-chat-8b> (2026-08-24, re-verified 2026-09-01; | ||||||||||||||||||||
| 174 | fixture C<t/data/akianthropic_tool_call_response.json>). One deviation from | ||||||||||||||||||||
| 175 | real Anthropic: the C<input> of that block arrives as a B<JSON string>, not an | ||||||||||||||||||||
| 176 | object — L<Langertha::ToolCall> decodes it, so C<< $tc->arguments >> is a | ||||||||||||||||||||
| 177 | HashRef either way. Since the parameter is undocumented, AKI.IO owes it no | ||||||||||||||||||||
| 178 | stability; check C<< $response->has_tool_calls >> rather than assuming. | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | B<Client errors arrive as HTTP 529:> AKI.IO returns some B<caller-side> errors | ||||||||||||||||||||
| 181 | as C<529> C<overloaded_error> — notably a token budget too small to finish a | ||||||||||||||||||||
| 182 | tool call (C<"Response finished before tool_call was completed! Try to raise | ||||||||||||||||||||
| 183 | max_gen_tokens">). That condition is deterministic and fixed by raising | ||||||||||||||||||||
| 184 | C<response_size> / C<max_tokens>, B<not> transient server overload: do not treat | ||||||||||||||||||||
| 185 | an AKI C<529> as a wait-and-retry signal, and read | ||||||||||||||||||||
| 186 | C<< $error->{error}{message} >> for the real diagnostic. | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | B<Tool results carry text only.> In the tool loop, an image a tool returns | ||||||||||||||||||||
| 189 | goes out as a text placeholder (C<[image] image/png (N bytes)>) for every | ||||||||||||||||||||
| 190 | model: the shim accepts an image inside a C<tool_result> but the model does not | ||||||||||||||||||||
| 191 | see it (live probe 2026-09-30). An embedded text resource goes out as a | ||||||||||||||||||||
| 192 | C<text> block and a PDF as a placeholder, because the shim answers a | ||||||||||||||||||||
| 193 | C<document> block there with C<529> C<"Unsupported content type: document">; | ||||||||||||||||||||
| 194 | a native C<search_result> block (also a C<529>) or C<document> block becomes a | ||||||||||||||||||||
| 195 | C<text> block with its text, with a warning. | ||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | Get your API key at L<https://aki.io/> and set C<LANGERTHA_AKI_API_KEY>. | ||||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | B<THIS API IS WORK IN PROGRESS> | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | =head2 api_key | ||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | The AKI.IO API key — the same key the native and OpenAI-compatible AKI.IO | ||||||||||||||||||||
| 204 | engines use. If not provided, reads from the C<LANGERTHA_AKI_API_KEY> | ||||||||||||||||||||
| 205 | environment variable. Sent as a raw C<x-api-key> HTTP header (no C<Bearer> | ||||||||||||||||||||
| 206 | prefix). Required. | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | =head2 default_model | ||||||||||||||||||||
| 209 | |||||||||||||||||||||
| 210 | Returns C<gpt-oss-120b>, a current-generation non-MiniMax model AKI.IO rates | ||||||||||||||||||||
| 211 | "Supported", matching the default of the sibling | ||||||||||||||||||||
| 212 | L<Langertha::Engine::AKIOpenAI> and replacing C<llama3-chat-8b>, which AKI.IO | ||||||||||||||||||||
| 213 | marks end-of-life 2026-09-30. AKI.IO exposes MiniMax M3 only on its native | ||||||||||||||||||||
| 214 | endpoint, not this shim. AKI.IO documents no default of its own for this | ||||||||||||||||||||
| 215 | endpoint — an unset or unknown model resolves to a fallback AKI does not | ||||||||||||||||||||
| 216 | guarantee (its docs name Minimax M2.5; the live substitute is unconfirmed — | ||||||||||||||||||||
| 217 | see L</DESCRIPTION>), so this class always sends an explicit model. | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | =head1 SEE ALSO | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | =over | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | =item * L<Langertha::Engine::AKI> - Native AKI.IO API (with top_k, top_p, max_gen_tokens) | ||||||||||||||||||||
| 224 | |||||||||||||||||||||
| 225 | =item * L<Langertha::Engine::AKIOpenAI> - AKI.IO via the OpenAI-compatible endpoint | ||||||||||||||||||||
| 226 | |||||||||||||||||||||
| 227 | =item * L<Langertha::Engine::AnthropicBase> - Anthropic-compatible base class | ||||||||||||||||||||
| 228 | |||||||||||||||||||||
| 229 | =item * L<https://aki.io/docs/compatibility/anthropic-api-compatibility/> - AKI.IO Anthropic API compatibility docs | ||||||||||||||||||||
| 230 | |||||||||||||||||||||
| 231 | =back | ||||||||||||||||||||
| 232 | |||||||||||||||||||||
| 233 | =head1 SUPPORT | ||||||||||||||||||||
| 234 | |||||||||||||||||||||
| 235 | =head2 Issues | ||||||||||||||||||||
| 236 | |||||||||||||||||||||
| 237 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||
| 238 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||
| 239 | |||||||||||||||||||||
| 240 | =head2 IRC | ||||||||||||||||||||
| 241 | |||||||||||||||||||||
| 242 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||
| 243 | |||||||||||||||||||||
| 244 | =head1 CONTRIBUTING | ||||||||||||||||||||
| 245 | |||||||||||||||||||||
| 246 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||
| 247 | |||||||||||||||||||||
| 248 | =head1 AUTHOR | ||||||||||||||||||||
| 249 | |||||||||||||||||||||
| 250 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||
| 251 | |||||||||||||||||||||
| 252 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 253 | |||||||||||||||||||||
| 254 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||
| 255 | |||||||||||||||||||||
| 256 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 257 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | =cut | ||||||||||||||||||||