| 1 | package Langertha::Engine::HuggingFace; | ||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: HuggingFace Inference Providers API | ||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | our $VERSION = '0.505'; | |||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 5 | 10 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use URI; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | extends 'Langertha::Engine::OpenAIBase'; | |||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||
| 10 | # StreamUsage: the chat-completion task specification lists | ||||||||||||||||||||||||||||||||||||||||
| 11 | # stream_options.include_usage (ADR 0038). | ||||||||||||||||||||||||||||||||||||||||
| 12 | 10 | with 'Langertha::Role::Tools', 'Langertha::Role::StreamUsage'; | |||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||
| 15 | has '+url' => ( | ||||||||||||||||||||||||||||||||||||||||
| 16 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||
| 17 | 3 | ▶ | default => sub { 'https://router.huggingface.co/v1' }, | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 18 | 10 | ); | |||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||
| 20 | 1 | ▶ | sub _build_api_key { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_api_key: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||||||||||||||
| 21 | 1 | my ( $self ) = @_; | |||||||||||||||||||||||||||||||||||||||
| 22 | return $ENV{LANGERTHA_HUGGINGFACE_API_KEY} | ||||||||||||||||||||||||||||||||||||||||
| 23 | 1 | ▶ | || croak "".(ref $self)." requires LANGERTHA_HUGGINGFACE_API_KEY or api_key set"; | ||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$ENV{'LANGERTHA_HUGGINGFACE_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_HUGGINGFACE_API_KEY or api_key set')
MC/DC0%0/1
$ENV{'LANGERTHA_HUGGINGFACE_API_KEY'} || croak('' . ref($self) . ' requires LANGERTHA_HUGGINGFACE_API_KEY or api_key set')
$ENV{'LANGERTHA_HUGGINGFACE_API_KEY'}
Truth table50%1/2
A
A= $ENV{'LANGERTHA_HUGGINGFACE_API_KEY'}
| |||||||||||||||||||||||||||||||||||||||||
| 24 | } | ||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||
| 26 | 1 | ▶ | sub default_model { croak "".(ref $_[0])." requires model to be set" } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||
| 28 | 15 | ▶ | sub _build_supported_operations {[qw( | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_supported_operations: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 29 | createChatCompletion | ||||||||||||||||||||||||||||||||||||||||
| 30 | )]} | ||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||
| 32 | # The router's chat-completion spec lists tools / tool_choice / tool_prompt but | ||||||||||||||||||||||||||||||||||||||||
| 33 | # not parallel_tool_calls; whether a backend provider honors it varies. Clear | ||||||||||||||||||||||||||||||||||||||||
| 34 | # parallel_tool_use (the flag means the wire documents the field); the raw | ||||||||||||||||||||||||||||||||||||||||
| 35 | # parallel_tool_calls kwarg still reaches a backend that honors it | ||||||||||||||||||||||||||||||||||||||||
| 36 | # (karr k242, docs only). | ||||||||||||||||||||||||||||||||||||||||
| 37 | 85 | ▶ | around engine_capabilities => sub { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 38 | 85 | my ( $orig, $self, @rest ) = @_; | |||||||||||||||||||||||||||||||||||||||
| 39 | 85 | my $caps = $self->$orig(@rest); | |||||||||||||||||||||||||||||||||||||||
| 40 | 85 | delete $caps->{parallel_tool_use}; | |||||||||||||||||||||||||||||||||||||||
| 41 | # image_input (k266, ADR 0019): a gateway: the model behind it is unknown to the client, so no claim. | ||||||||||||||||||||||||||||||||||||||||
| 42 | 85 | delete $caps->{image_input}; | |||||||||||||||||||||||||||||||||||||||
| 43 | 85 | return $caps; | |||||||||||||||||||||||||||||||||||||||
| 44 | 10 | }; | |||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||
| 46 | has hub_url => ( | ||||||||||||||||||||||||||||||||||||||||
| 47 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||
| 48 | isa => 'Str', | ||||||||||||||||||||||||||||||||||||||||
| 49 | 29 | ▶ | default => sub { 'https://huggingface.co' }, | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 50 | 10 | ); | |||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||||||||
| 53 | 3 | ▶ | sub list_models_request { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models_request: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 54 | 3 | my ($self, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||
| 55 | 3 | my $url = URI->new($self->hub_url.'/api/models'); | |||||||||||||||||||||||||||||||||||||||
| 56 | my %params = ( | ||||||||||||||||||||||||||||||||||||||||
| 57 | inference_provider => $opts{inference_provider} // 'all', | ||||||||||||||||||||||||||||||||||||||||
| 58 | pipeline_tag => $opts{pipeline_tag} // 'text-generation', | ||||||||||||||||||||||||||||||||||||||||
| 59 | limit => $opts{limit} // 50, | ||||||||||||||||||||||||||||||||||||||||
| 60 | 3 | ▶ | $opts{search} ? (search => $opts{search}) : (), | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%3/6
$opts{"inference_provider"} // "all"
$opts{"pipeline_tag"} // "text-generation"
$opts{"limit"} // 50
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"}
Truth table50%1/2
A
A= $opts{"pipeline_tag"}
Truth table50%1/2
A
A= $opts{"limit"}
| |||||||||||||||||||||||||||||||||||||||||
| 61 | 'expand[]' => 'inferenceProviderMapping', | ||||||||||||||||||||||||||||||||||||||||
| 62 | ); | ||||||||||||||||||||||||||||||||||||||||
| 63 | 3 | $url->query_form(%params); | |||||||||||||||||||||||||||||||||||||||
| 64 | return $self->generate_http_request( | ||||||||||||||||||||||||||||||||||||||||
| 65 | GET => $url->as_string, | ||||||||||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | sub { $self->list_models_response(shift, %opts) }, | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 67 | 3 | ); | |||||||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | ▶ | sub list_models_response { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models_response: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | my ($self, $response, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||
| 73 | 1 | my $data = $self->parse_response($response); | |||||||||||||||||||||||||||||||||||||||
| 74 | 1 | return $data; | |||||||||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||
| 78 | 2 | ▶ | sub list_models { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models: called CC=8 SCAR=21.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 79 | 2 | my ($self, %opts) = @_; | |||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||
| 81 | # Check cache unless force_refresh or search (searches are not cached) | ||||||||||||||||||||||||||||||||||||||||
| 82 | 2 | ▶ | unless ($opts{force_refresh} || $opts{search}) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$opts{"force_refresh"} or $opts{"search"}
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"}
| |||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | my $cache = $self->_models_cache; | |||||||||||||||||||||||||||||||||||||||
| 84 | 2 | ▶ | if ($cache->{timestamp} && time - $cache->{timestamp} < $self->models_cache_ttl) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$$cache{"timestamp"} and time - $$cache{"timestamp"} < $self->models_cache_ttl
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
| |||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | ▶ | return $opts{full} ? $cache->{models} : $cache->{model_ids}; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||
| 89 | 1 | my $request = $self->list_models_request(%opts); | |||||||||||||||||||||||||||||||||||||||
| 90 | 1 | my $response = $self->user_agent->request($request); | |||||||||||||||||||||||||||||||||||||||
| 91 | 1 | my $models = $request->response_call->($response); | |||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||
| 93 | 1 | my @model_ids = map { $_->{id} } @$models; | |||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||
| 95 | # Only cache non-search results | ||||||||||||||||||||||||||||||||||||||||
| 96 | 1 | ▶ | unless ($opts{search}) { | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 97 | 1 | $self->_models_cache({ | |||||||||||||||||||||||||||||||||||||||
| 98 | timestamp => time, | ||||||||||||||||||||||||||||||||||||||||
| 99 | models => $models, | ||||||||||||||||||||||||||||||||||||||||
| 100 | model_ids => \@model_ids, | ||||||||||||||||||||||||||||||||||||||||
| 101 | }); | ||||||||||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||
| 104 | 1 | ▶ | return $opts{full} ? $models : \@model_ids; | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||
| 108 | 10 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||
| 111 | 10 | 1; | |||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||
| 113 | __END__ | ||||||||||||||||||||||||||||||||||||||||