Langertha/Engine/HuggingFace.pm

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