Langertha/Engine/AKI.pm

filter: Statementstmt 70.7% 46 / 65 Branchbran 61.5% 16 / 26 Conditioncond 20.0% 2 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 73.6% 14 / 19 Podpod 40.0% 4 / 10 totaltotal 60.2% 82 / 136 CC 18 SCAR 19.8
CC
18
Cov
63%
list_models
30.0
chat_response
6.7
_build_api_key
6.0
CRAP 7.2
« Langertha/Role/HTTP.pm ↑ Summary Langertha/Role/PluginHost.pm »
1 package Langertha::Engine::AKI;
2 # ABSTRACT: AKI.IO native API
3 5 our $VERSION = '0.502';
4 5 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use Carp qw( croak carp );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use JSON::MaybeXS;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 extends 'Langertha::Engine::Remote';
9  
10 5 with map { 'Langertha::Role::'.$_ } qw(
11 Models
12 Temperature
13 SystemPrompt
14 Chat
15 Tools
16 HermesTools
17 );
18  
19  
20 5 has api_key => (
21 is => 'ro',
22 lazy_build => 1,
23 );
24 sub _build_api_key {
25 0 ▶ my ( $self ) = @_;
Subroutine _build_api_key: not called CC=2 SCAR=17.9
26 return $ENV{LANGERTHA_AKI_API_KEY}
27 0 ▶ || croak "".(ref $self)." requires LANGERTHA_AKI_API_KEY or api_key set";
Condition0%0/3
$ENV{'LANGERTHA_AKI_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_AKI_API_KEY or api_key set')
l!l&&r!l&&!r
000
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 table0%0/3
A || B
A= $ENV{'LANGERTHA_AKI_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_AKI_API_KEY or api_key set')
ABresult
1X1
011
000
28 }
29  
30  
31 has '+url' => (
32 lazy => 1,
33 4 ▶ default => sub { 'https://aki.io' },
Subroutine __ANON__: called CC=1 SCAR=0.0
34 5 );
35  
36 1 ▶sub default_model { 'llama3_8b_chat' }
Subroutine default_model: called CC=1 SCAR=0.0
Pod undocumented
37  
38 sub hermes_extract_content {
39 0 ▶ my ( $self, $data ) = @_;
Subroutine hermes_extract_content: not called CC=1 SCAR=6.9
Pod undocumented
40 0 return $data->{text};
41 }
42  
43 5 has top_k => (
44 is => 'ro',
45 isa => 'Num',
46 predicate => 'has_top_k',
47 );
48  
49  
50 5 has top_p => (
51 is => 'ro',
52 isa => 'Num',
53 predicate => 'has_top_p',
54 );
55  
56  
57 5 has max_gen_tokens => (
58 is => 'ro',
59 isa => 'Int',
60 predicate => 'has_max_gen_tokens',
61 );
62  
63  
64 # Dynamic model listing
65  
66 sub list_models_request {
67 1 ▶ my ($self) = @_;
Subroutine list_models_request: called CC=1 SCAR=0.0
Pod undocumented
68 return $self->generate_http_request(
69 GET => $self->url.'/api/endpoints?key='.$self->api_key,
70 1 ▶ sub { $self->list_models_response(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
71 1 );
72 }
73  
74 sub list_models_response {
75 1 ▶ my ($self, $response) = @_;
Subroutine list_models_response: called CC=1 SCAR=0.0
Pod undocumented
76 1 my $data = $self->parse_response($response);
77 1 return $data->{endpoints};
78 }
79  
80 sub list_models {
81 0 ▶ my ($self, %opts) = @_;
Subroutine list_models: not called CC=5 SCAR=34.0
Pod documented
82  
83 # Check cache unless force_refresh requested
84 0 ▶ unless ($opts{force_refresh}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"force_refresh"}) 0 0 0
85 0 my $cache = $self->_models_cache;
86 0 ▶ if ($cache->{timestamp} && time - $cache->{timestamp} < $self->models_cache_ttl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$cache{"timestamp"} and time - $$cache{"timestamp"} < $self->models_cache_ttl) 0 0 0
Condition0%0/3
$$cache{"timestamp"} and time - $$cache{"timestamp"} < $self->models_cache_ttl
!ll&&!rl&&r
000
MC/DC0%0/2
$$cache{"timestamp"} and time - $$cache{"timestamp"} < $self->models_cache_ttl
$$cache{"timestamp"} time - $$cache{"timestamp"} < $self->models_cache_ttl
Truth table0%0/3
A and B
A= $$cache{"timestamp"}B= time - $$cache{"timestamp"} < $self->models_cache_ttl
ABresult
0X0
100
111
87 0 ▶ return $opts{full} ? $cache->{models} : $cache->{model_ids};
Branch: 1 branch
BranchTrueFalseTotal
$opts{'full'} ? : 0 0 0
88 }
89 }
90  
91 # Fetch from API
92 0 my $request = $self->list_models_request;
93 0 my $response = $self->user_agent->request($request);
94 0 my $endpoints = $request->response_call->($response);
95  
96 # Update cache
97 0 $self->_models_cache({
98 timestamp => time,
99 models => $endpoints,
100 model_ids => $endpoints,
101 });
102  
103 0 return $endpoints;
104 }
105  
106  
107 sub endpoint_details_request {
108 1 ▶ my ($self, $endpoint_name) = @_;
Subroutine endpoint_details_request: called CC=1 SCAR=0.0
Pod undocumented
109 return $self->generate_http_request(
110 GET => $self->url.'/api/endpoints/'.$endpoint_name.'?key='.$self->api_key,
111 1 ▶ sub { $self->endpoint_details_response(shift) },
Subroutine __ANON__: called CC=1 SCAR=0.0
112 1 );
113 }
114  
115 sub endpoint_details_response {
116 1 ▶ my ($self, $response) = @_;
Subroutine endpoint_details_response: called CC=1 SCAR=0.0
Pod undocumented
117 1 return $self->parse_response($response);
118 }
119  
120 sub endpoint_details {
121 0 ▶ my ($self, $endpoint_name) = @_;
Subroutine endpoint_details: not called CC=1 SCAR=6.9
Pod documented
122 0 my $request = $self->endpoint_details_request($endpoint_name);
123 0 my $response = $self->user_agent->request($request);
124 0 return $request->response_call->($response);
125 }
126  
127  
128 # Chat
129  
130 sub chat_request {
131 3 ▶ my ( $self, $messages, %extra ) = @_;
Subroutine chat_request: called CC=5 SCAR=16.1
Pod documented
132 3 my $model = $self->chat_model;
133 return $self->generate_http_request(
134 POST => $self->url.'/api/call/'.$model,
135 0 ▶ sub { $self->chat_response(shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
136 3 ▶ key => $self->api_key,
Branch: 4 branches
BranchTrueFalseTotal
$self->has_temperature ? : 2 1 3
$self->has_top_k ? : 1 2 3
$self->has_top_p ? : 1 2 3
$self->has_max_gen_tokens ? : 1 2 3
137 chat_context => $self->json->encode($messages),
138 $self->has_temperature ? ( temperature => $self->temperature ) : (),
139 $self->has_top_k ? ( top_k => $self->top_k ) : (),
140 $self->has_top_p ? ( top_p => $self->top_p ) : (),
141 $self->has_max_gen_tokens ? ( max_gen_tokens => $self->max_gen_tokens ) : (),
142 wait_for_result => JSON->true,
143 %extra,
144 );
145 }
146  
147  
148 sub chat_response {
149 3 ▶ my ( $self, $response ) = @_;
Subroutine chat_response: called CC=6 SCAR=19.0
150 3 my $data = $self->parse_response($response);
151 croak "".(ref $self)." API error: ".($data->{error} || 'unknown')
152 3 ▶ unless $data->{success};
Branch: 1 branch
BranchTrueFalseTotal
unless $$data{"success"} 1 2 3
Condition50%1/2
$$data{'error'} || 'unknown'
l!l
10
MC/DC0%0/1
$$data{'error'} || 'unknown'
$$data{'error'}
Truth table50%1/2
A
A= $$data{'error'}
Aresult
11
00
153 2 require Langertha::Response;
154 return Langertha::Response->new(
155 content => $data->{text} // '',
156 raw => $data,
157 $data->{model_name} ? ( model => $data->{model_name} ) : (),
158 2 ▶ $data->{total_duration} ? ( timing => { total_duration => $data->{total_duration} } ) : (),
Branch: 2 branches
BranchTrueFalseTotal
$$data{'model_name'} ? : 2 0 2
$$data{'total_duration'} ? : 2 0 2
Condition50%1/2
$$data{"text"} // ""
l!l
20
MC/DC0%0/1
$$data{"text"} // ""
$$data{"text"}
Truth table50%1/2
A
A= $$data{"text"}
Aresult
11
00
159 );
160 }
161  
162  
163 sub openai {
164 2 ▶ my ( $self, %args ) = @_;
Subroutine openai: called CC=4 SCAR=14.1
Pod documented
165 2 require Langertha::Engine::AKIOpenAI;
166 2 ▶ unless (exists $args{model}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $args{"model"}) 1 1 2
167 1 carp "".(ref $self)."->openai: native model name cannot be mapped to /v1 model name automatically, using AKIOpenAI default model";
168 }
169 2 ▶ return Langertha::Engine::AKIOpenAI->new(
Branch: 2 branches
BranchTrueFalseTotal
$self->has_system_prompt ? : 2 0 2
$self->has_temperature ? : 2 0 2
170 api_key => $self->api_key,
171 $self->has_system_prompt ? ( system_prompt => $self->system_prompt ) : (),
172 $self->has_temperature ? ( temperature => $self->temperature ) : (),
173 %args,
174 );
175 }
176  
177  
178 5 __PACKAGE__->meta->make_immutable;
179  
180  
181 5 1;
182  
183 __END__
184  
185 =pod
186  
187 =encoding UTF-8
188  
189 =head1 NAME
190  
191 Langertha::Engine::AKI - AKI.IO native API
192  
193 =head1 VERSION
194  
195 version 0.502
196  
197 =head1 SYNOPSIS
198  
199 use Langertha::Engine::AKI;
200  
201 my $aki = Langertha::Engine::AKI->new(
202 api_key => $ENV{AKI_API_KEY},
203 model => 'llama3_8b_chat',
204 );
205  
206 print $aki->simple_chat('Hello from Perl!');
207  
208 # Get OpenAI-compatible API access
209 my $aki_openai = $aki->openai;
210 print $aki_openai->simple_chat('Hello via OpenAI format!');
211  
212 =head1 DESCRIPTION
213  
214 Provides access to AKI.IO's native API for running LLM inference. AKI.IO is
215 a European AI model hub based in Germany; all inference runs on EU infrastructure,
216 fully GDPR-compliant with no data leaving the EU.
217  
218 The native API sends the API key as a C<key> field in the JSON request body
219 (not as an HTTP header). Supports synchronous chat, temperature and sampling
220 controls, dynamic endpoint listing, MCP tool calling via
221 L<Langertha::Role::HermesTools>, and OpenAI-compatible access via L</openai>.
222  
223 Streaming is not yet supported in the native API. For streaming, use the
224 OpenAI-compatible endpoint via C<< $aki->openai >>.
225  
226 Get your API key at L<https://aki.io/> and set C<LANGERTHA_AKI_API_KEY>.
227  
228 B<THIS API IS WORK IN PROGRESS>
229  
230 =head2 api_key
231  
232 The AKI.IO API key. If not provided, reads from C<LANGERTHA_AKI_API_KEY>
233 environment variable. Sent as a C<key> field in the JSON request body
234 (not as an HTTP header). Required.
235  
236 =head2 top_k
237  
238 top_k => 40
239  
240 Top-K sampling parameter. Controls the number of highest-probability tokens
241 to consider at each generation step.
242  
243 =head2 top_p
244  
245 top_p => 0.9
246  
247 Top-P (nucleus) sampling parameter. Controls the cumulative probability
248 threshold for token selection.
249  
250 =head2 max_gen_tokens
251  
252 max_gen_tokens => 1000
253  
254 Maximum number of tokens to generate in the response.
255  
256 =head2 list_models
257  
258 my $endpoints = $aki->list_models;
259 my $endpoints = $aki->list_models(force_refresh => 1);
260  
261 Fetches available endpoint names from the AKI.IO C<GET /api/endpoints> API.
262 Returns an ArrayRef of endpoint names. Results are cached for C<models_cache_ttl>
263 seconds (default: 3600).
264  
265 =head2 endpoint_details
266  
267 my $details = $aki->endpoint_details('llama3_8b_chat');
268 # Returns hashref with name, title, description, workers, parameter_description, etc.
269  
270 Fetches detailed information about a specific endpoint from the AKI.IO
271 C<GET /api/endpoints/{name}> API. Returns worker info, model metadata,
272 and parameter descriptions.
273  
274 =head2 chat_request
275  
276 my $request = $aki->chat_request($messages, %extra);
277  
278 Generates a native AKI.IO chat request. Posts to C</api/call/{model}> with
279 messages encoded as JSON in the C<chat_context> field. Includes C<key>,
280 C<temperature>, C<top_k>, C<top_p>, C<max_gen_tokens>, and
281 C<wait_for_result> parameters as configured. Returns an HTTP request object.
282  
283 =head2 chat_response
284  
285 my $response = $aki->chat_response($http_response);
286  
287 Parses a native AKI.IO chat response. Dies with an API error message if
288 C<success> is false. Returns a L<Langertha::Response> with C<content>,
289 C<model>, C<timing>, and C<raw>.
290  
291 =head2 openai
292  
293 my $oai = $aki->openai;
294 my $oai = $aki->openai(model => 'llama3-chat-8b');
295  
296 Returns a L<Langertha::Engine::AKIOpenAI> instance configured with the same
297 API key, system prompt, and temperature. Supports streaming and MCP tool
298 calling.
299  
300 B<Note:> The native AKI model name is B<not> carried over automatically
301 because the C</v1> endpoint uses different model identifiers. If no C<model>
302 is passed, the AKIOpenAI default model is used and a warning is emitted.
303 Pass C<< model => '...' >> explicitly with a valid C</v1> model name to
304 suppress the warning.
305  
306 =head1 SEE ALSO
307  
308 =over
309  
310 =item * L<Langertha::Engine::AKIOpenAI> - OpenAI-compatible AKI.IO access via L</openai>
311  
312 =item * L<https://aki.io/docs> - AKI.IO API documentation
313  
314 =item * L<Langertha::Role::Chat> - Chat interface methods
315  
316 =back
317  
318 =head1 SUPPORT
319  
320 =head2 Issues
321  
322 Please report bugs and feature requests on GitHub at
323 L<https://github.com/Getty/langertha/issues>.
324  
325 =head2 IRC
326  
327 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
328  
329 =head1 CONTRIBUTING
330  
331 Contributions are welcome! Please fork the repository and submit a pull request.
332  
333 =head1 AUTHOR
334  
335 Torsten Raudssus <getty@cpan.org>
336  
337 =head1 COPYRIGHT AND LICENSE
338  
339 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
340  
341 This is free software; you can redistribute it and/or modify it under
342 the same terms as the Perl 5 programming language system itself.
343  
344 =cut
« Langertha/Role/HTTP.pm ↑ Summary Langertha/Role/PluginHost.pm »