blib/lib/Langertha/Engine/HuggingFace.pm

filter: Statementstmt 100% 50 / 50 Branchbran 66.6% 8 / 12 Conditioncond 46.6% 7 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 100% 13 / 13 Podpod 100% 4 / 4 totaltotal 80.5% 83 / 103 CC 13 SCAR 15.0
CC
13
Cov
84%
list_models
8.9
list_models_request
5.2
_build_api_key
2.3
CRAP 4.5
« blib/lib/Langertha.pm ↑ Summary blib/lib/Langertha/Role/ContextSize.pm »
1 package Langertha::Engine::HuggingFace;
2 # ABSTRACT: HuggingFace Inference Providers API
3 8 our $VERSION = '0.503';
4 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Carp qw( croak );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 8 extends 'Langertha::Engine::OpenAIBase';
9  
10 8 with 'Langertha::Role::Tools';
11  
12  
13 has '+url' => (
14 lazy => 1,
15 3 ▶ default => sub { 'https://router.huggingface.co/v1' },
Subroutine __ANON__: called CC=1 SCAR=0.0
16 8 );
17  
18 1 ▶sub _build_api_key {
Subroutine _build_api_key: called CC=2 SCAR=8.1
19 1 my ( $self ) = @_;
20 return $ENV{LANGERTHA_HUGGINGFACE_API_KEY}
21 1 ▶ || croak "".(ref $self)." requires LANGERTHA_HUGGINGFACE_API_KEY or api_key set";
Condition33%1/3
$ENV{'LANGERTHA_HUGGINGFACE_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_HUGGINGFACE_API_KEY or api_key set')
l!l&&r!l&&!r
100
MC/DC0%0/2
$ENV{'LANGERTHA_HUGGINGFACE_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_HUGGINGFACE_API_KEY or api_key set')
$ENV{'LANGERTHA_HUGGINGFACE_API_KEY'} croak('' . ref($self) . ' requires LANGERTHA_HUGGINGFACE_API_KEY or api_key set')
Truth table33%1/3
A || B
A= $ENV{'LANGERTHA_HUGGINGFACE_API_KEY'}B= croak('' . ref($self) . ' requires LANGERTHA_HUGGINGFACE_API_KEY or api_key set')
ABresult
1X1
011
000
22 }
23  
24 1 ▶sub default_model { croak "".(ref $_[0])." requires model to be set" }
Subroutine default_model: called CC=1 SCAR=0.0
Pod documented
25  
26 14 ▶sub _build_supported_operations {[qw(
Subroutine _build_supported_operations: called CC=1 SCAR=0.0
27 createChatCompletion
28 )]}
29  
30 # The router's chat-completion spec lists tools / tool_choice / tool_prompt but
31 # not parallel_tool_calls; whether a backend provider honors it varies. Clear
32 # parallel_tool_use (the flag means the wire documents the field); the raw
33 # parallel_tool_calls kwarg still reaches a backend that honors it
34 # (karr k242, docs only).
35 60 ▶around engine_capabilities => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
36 60 my ( $orig, $self, @rest ) = @_;
37 60 my $caps = $self->$orig(@rest);
38 60 delete $caps->{parallel_tool_use};
39 # image_input (k266, ADR 0019): a gateway: the model behind it is unknown to the client, so no claim.
40 60 delete $caps->{image_input};
41 60 return $caps;
42 8 };
43  
44 has hub_url => (
45 is => 'ro',
46 isa => 'Str',
47 23 ▶ default => sub { 'https://huggingface.co' },
Subroutine __ANON__: called CC=1 SCAR=0.0
48 8 );
49  
50  
51 3 ▶sub list_models_request {
Subroutine list_models_request: called CC=5 SCAR=16.6
Pod documented
52 3 my ($self, %opts) = @_;
53 3 my $url = URI->new($self->hub_url.'/api/models');
54 my %params = (
55 inference_provider => $opts{inference_provider} // 'all',
56 pipeline_tag => $opts{pipeline_tag} // 'text-generation',
57 limit => $opts{limit} // 50,
58 3 ▶ $opts{search} ? (search => $opts{search}) : (),
Branch: 1 branch
BranchTrueFalseTotal
$opts{'search'} ? : 1 2 3
Condition50%3/6
$opts{"inference_provider"} // "all"
l!l
03
$opts{"pipeline_tag"} // "text-generation"
l!l
03
$opts{"limit"} // 50
l!l
03
MC/DC0%0/1
$opts{"inference_provider"} // "all"
$opts{"inference_provider"}
MC/DC0%0/1
$opts{"pipeline_tag"} // "text-generation"
$opts{"pipeline_tag"}
MC/DC0%0/1
$opts{"limit"} // 50
$opts{"limit"}
Truth table50%1/2
A
A= $opts{"inference_provider"}
Aresult
11
00
Truth table50%1/2
A
A= $opts{"pipeline_tag"}
Aresult
11
00
Truth table50%1/2
A
A= $opts{"limit"}
Aresult
11
00
59 'expand[]' => 'inferenceProviderMapping',
60 );
61 3 $url->query_form(%params);
62 return $self->generate_http_request(
63 GET => $url->as_string,
64 1 ▶ sub { $self->list_models_response(shift, %opts) },
Subroutine __ANON__: called CC=1 SCAR=0.0
65 3 );
66 }
67  
68  
69 1 ▶sub list_models_response {
Subroutine list_models_response: called CC=1 SCAR=0.0
Pod documented
70 1 my ($self, $response, %opts) = @_;
71 1 my $data = $self->parse_response($response);
72 1 return $data;
73 }
74  
75  
76 2 ▶sub list_models {
Subroutine list_models: called CC=8 SCAR=21.9
Pod documented
77 2 my ($self, %opts) = @_;
78  
79 # Check cache unless force_refresh or search (searches are not cached)
80 2 ▶ unless ($opts{force_refresh} || $opts{search}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"force_refresh"} or $opts{"search"}) 2 0 2
Condition33%1/3
$opts{"force_refresh"} or $opts{"search"}
l!l&&r!l&&!r
002
MC/DC0%0/2
$opts{"force_refresh"} or $opts{"search"}
$opts{"force_refresh"} $opts{"search"}
Truth table33%1/3
A or B
A= $opts{"force_refresh"}B= $opts{"search"}
ABresult
1X1
011
000
81 2 my $cache = $self->_models_cache;
82 2 ▶ 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) 1 1 2
Condition66%2/3
$$cache{"timestamp"} and time - $$cache{"timestamp"} < $self->models_cache_ttl
!ll&&!rl&&r
101
MC/DC50%1/2
$$cache{"timestamp"} and time - $$cache{"timestamp"} < $self->models_cache_ttl
$$cache{"timestamp"} time - $$cache{"timestamp"} < $self->models_cache_ttl
Truth table66%2/3
A and B
A= $$cache{"timestamp"}B= time - $$cache{"timestamp"} < $self->models_cache_ttl
ABresult
0X0
100
111
83 1 ▶ return $opts{full} ? $cache->{models} : $cache->{model_ids};
Branch: 1 branch
BranchTrueFalseTotal
$opts{'full'} ? : 1 0 1
84 }
85 }
86  
87 1 my $request = $self->list_models_request(%opts);
88 1 my $response = $self->user_agent->request($request);
89 1 my $models = $request->response_call->($response);
90  
91 1 my @model_ids = map { $_->{id} } @$models;
92  
93 # Only cache non-search results
94 1 ▶ unless ($opts{search}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"search"}) 1 0 1
95 1 $self->_models_cache({
96 timestamp => time,
97 models => $models,
98 model_ids => \@model_ids,
99 });
100 }
101  
102 1 ▶ return $opts{full} ? $models : \@model_ids;
Branch: 1 branch
BranchTrueFalseTotal
$opts{'full'} ? : 0 1 1
103 }
104  
105  
106 8 __PACKAGE__->meta->make_immutable;
107  
108  
109 8 1;
110  
111 __END__
112  
113 =pod
114  
115 =encoding UTF-8
116  
117 =head1 NAME
118  
119 Langertha::Engine::HuggingFace - HuggingFace Inference Providers API
120  
121 =head1 VERSION
122  
123 version 0.503
124  
125 =head1 SYNOPSIS
126  
127 use Langertha::Engine::HuggingFace;
128  
129 my $hf = Langertha::Engine::HuggingFace->new(
130 api_key => $ENV{HF_TOKEN},
131 model => 'Qwen/Qwen2.5-7B-Instruct',
132 );
133  
134 print $hf->simple_chat('Hello from Perl!');
135  
136 # Access many models through one API
137 my $llama = Langertha::Engine::HuggingFace->new(
138 api_key => $ENV{HF_TOKEN},
139 model => 'meta-llama/Llama-3.3-70B-Instruct',
140 );
141  
142 =head1 DESCRIPTION
143  
144 Provides access to HuggingFace Inference Providers, a unified API gateway
145 for open-source models hosted on the HuggingFace Hub. The endpoint at
146 C<https://router.huggingface.co/v1> is 100% OpenAI-compatible.
147  
148 Model names use C<org/model> format (e.g., C<Qwen/Qwen2.5-7B-Instruct>,
149 C<meta-llama/Llama-3.3-70B-Instruct>). No default model is set;
150 C<model> must be specified explicitly.
151  
152 Supports chat, streaming, and MCP tool calling. Embeddings and transcription
153 are not supported.
154  
155 Get your API token at L<https://huggingface.co/settings/tokens> and set
156 C<LANGERTHA_HUGGINGFACE_API_KEY> in your environment.
157  
158 B<THIS API IS WORK IN PROGRESS>
159  
160 =head2 hub_url
161  
162 Base URL for the HuggingFace Hub API. Default: C<https://huggingface.co>.
163 Used by C<list_models> to query available inference models.
164  
165 =head2 list_models_request
166  
167 my $request = $engine->list_models_request(%opts);
168  
169 Generates an HTTP GET request for the HuggingFace Hub API models
170 endpoint with inference provider filtering. Accepts options:
171 C<search>, C<pipeline_tag> (default: C<text-generation>),
172 C<inference_provider> (default: C<all>), C<limit> (default: 50).
173  
174 =head2 list_models_response
175  
176 my $models = $engine->list_models_response($http_response);
177  
178 Parses the Hub API response. Returns an ArrayRef of model objects
179 with C<id>, C<pipeline_tag>, C<inferenceProviderMapping>, etc.
180  
181 =head2 list_models
182  
183 # All text-generation models with inference providers
184 my $ids = $hf->list_models;
185  
186 # Search for specific models
187 my $ids = $hf->list_models(search => 'llama');
188  
189 # Filter by pipeline tag
190 my $ids = $hf->list_models(pipeline_tag => 'text-to-image');
191  
192 # Full model objects with provider details
193 my $models = $hf->list_models(full => 1);
194  
195 Queries the HuggingFace Hub API for models available via inference
196 providers. Only returns models that have at least one active inference
197 provider. Results are cached for C<models_cache_ttl> seconds (search
198 results are not cached).
199  
200 =head1 SEE ALSO
201  
202 =over
203  
204 =item * L<https://huggingface.co/docs/inference-providers/index> - HuggingFace Inference Providers docs
205  
206 =item * L<https://huggingface.co/models> - Browse available models
207  
208 =item * L<https://status.huggingface.co/> - HuggingFace service status
209  
210 =item * L<Langertha::Role::OpenAICompatible> - OpenAI API format role
211  
212 =back
213  
214 =head1 SUPPORT
215  
216 =head2 Issues
217  
218 Please report bugs and feature requests on GitHub at
219 L<https://github.com/Getty/langertha/issues>.
220  
221 =head2 IRC
222  
223 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
224  
225 =head1 CONTRIBUTING
226  
227 Contributions are welcome! Please fork the repository and submit a pull request.
228  
229 =head1 AUTHOR
230  
231 Torsten Raudssus <getty@cpan.org>
232  
233 =head1 COPYRIGHT AND LICENSE
234  
235 This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>.
236  
237 This is free software; you can redistribute it and/or modify it under
238 the same terms as the Perl 5 programming language system itself.
239  
240 =cut
« blib/lib/Langertha.pm ↑ Summary blib/lib/Langertha/Role/ContextSize.pm »