| 1 | package Langertha::Engine::LMStudio; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: LM Studio native REST API | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 3 | 3 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use File::ShareDir::ProjectDistDir qw( :all ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Module::Runtime qw( use_module ); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 10 | 3 | extends 'Langertha::Engine::Remote'; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 3 | with map { 'Langertha::Role::'.$_ } qw( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 13 | OpenAPI | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 14 | Models | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 15 | Temperature | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 16 | ResponseSize | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 17 | ContextSize | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 18 | SystemPrompt | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 19 | Streaming | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 20 | Chat | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 21 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 24 | has '+url' => ( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 25 | lazy => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | ▶ | default => sub { 'http://localhost:1234' }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 3 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 3 | has api_key => ( | |||||||||||||||||||||||||||||||||||||||||||||||||
| 30 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 31 | lazy_build => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 34 | sub _build_api_key { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 35 | 4 | ▶ | return $ENV{LANGERTHA_LMSTUDIO_API_KEY}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_api_key: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 39 | sub update_request { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 40 | 4 | ▶ | my ( $self, $request ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
update_request: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | 4 | my $key = $self->api_key; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 42 | 4 | ▶ | $request->header('Authorization', 'Bearer '.$key) if defined $key; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 45 | 1 | ▶ | sub default_model { 'default' } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 6 | ▶ | sub default_response_size { 1024 } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_response_size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub openapi_file { yaml => dist_file('Langertha','lmstudio.yaml') }; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
openapi_file: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 51 | sub _build_openapi_operations { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | ▶ | return use_module('Langertha::Spec::LMStudio')->data; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_openapi_operations: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | ▶ | sub _build_supported_operations {[qw( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_supported_operations: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | chat | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 57 | listModels | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 58 | )]} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 60 | sub openai { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 61 | 2 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
openai: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 63 | 2 | require Langertha::Engine::LMStudioOpenAI; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 2 | my $url = $self->url; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 66 | 2 | $url =~ s{/\z}{}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 2 | ▶ | my $api_key = defined $self->api_key ? $self->api_key : 'lmstudio'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 2 | ▶ | return Langertha::Engine::LMStudioOpenAI->new( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 70 | url => $url.'/v1', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 71 | model => $self->model, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 72 | api_key => $api_key, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 73 | $self->has_system_prompt ? ( system_prompt => $self->system_prompt ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 74 | $self->has_temperature ? ( temperature => $self->temperature ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 75 | %args, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 76 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 80 | sub anthropic { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 81 | 2 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
anthropic: called CC=4 SCAR=14.2
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 83 | 2 | require Langertha::Engine::LMStudioAnthropic; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 2 | ▶ | my $api_key = defined $self->api_key ? $self->api_key : 'lmstudio'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 87 | 2 | ▶ | return Langertha::Engine::LMStudioAnthropic->new( | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 88 | url => $self->url, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 89 | model => $self->model, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 90 | api_key => $api_key, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 91 | $self->has_system_prompt ? ( system_prompt => $self->system_prompt ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | $self->has_temperature ? ( temperature => $self->temperature ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | %args, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | sub _normalize_usage { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | my ( $usage ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_usage: not called CC=14 SCAR=53.5
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | return undef unless $usage && ref $usage eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$usage and ref $usage eq "HASH"
MC/DC0%0/2
$usage and ref $usage eq "HASH"
$usage
ref $usage eq "HASH"
Truth table0%0/3
A and B
A= $usageB= ref $usage eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | 0 | my %normalized; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 103 | $normalized{prompt_tokens} = $usage->{prompt_tokens} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | if defined $usage->{prompt_tokens}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 105 | $normalized{completion_tokens} = $usage->{completion_tokens} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if defined $usage->{completion_tokens}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 107 | $normalized{total_tokens} = $usage->{total_tokens} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 108 | 0 | ▶ | if defined $usage->{total_tokens}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 110 | $normalized{prompt_tokens} = $usage->{input_tokens} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | if !defined($normalized{prompt_tokens}) && defined($usage->{input_tokens}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $normalized{"prompt_tokens"} and defined $$usage{"input_tokens"}
MC/DC0%0/2
not defined $normalized{"prompt_tokens"} and defined $$usage{"input_tokens"}
not defined $normalized{"prompt_tokens"}
defined $$usage{"input_tokens"}
Truth table0%0/3
not(A) and B
A= not defined $normalized{"prompt_tokens"}B= defined $$usage{"input_tokens"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 112 | $normalized{completion_tokens} = $usage->{output_tokens} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | if !defined($normalized{completion_tokens}) && defined($usage->{output_tokens}); | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $normalized{"completion_tokens"} and defined $$usage{"output_tokens"}
MC/DC0%0/2
not defined $normalized{"completion_tokens"} and defined $$usage{"output_tokens"}
not defined $normalized{"completion_tokens"}
defined $$usage{"output_tokens"}
Truth table0%0/3
not(A) and B
A= not defined $normalized{"completion_tokens"}B= defined $$usage{"output_tokens"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | if (!defined($normalized{total_tokens}) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
not defined $normalized{"total_tokens"} and defined $normalized{"prompt_tokens"} and defined $normalized{"completion_tokens"}
not defined $normalized{"total_tokens"} and defined $normalized{"prompt_tokens"}
MC/DC0%0/3
not defined $normalized{"total_tokens"} and defined $normalized{"prompt_tokens"} and defined $normalized{"completion_tokens"}
not defined $normalized{"total_tokens"}
defined $normalized{"prompt_tokens"}
defined $normalized{"completion_tokens"}
Truth table0%0/4
not(A) and B and C
A= not defined $normalized{"total_tokens"}B= defined $normalized{"prompt_tokens"}C= defined $normalized{"completion_tokens"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 116 | && defined($normalized{prompt_tokens}) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 117 | && defined($normalized{completion_tokens})) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $normalized{total_tokens} = $normalized{prompt_tokens} + $normalized{completion_tokens}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | return %normalized ? \%normalized : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 124 | sub _extract_text { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | my ( $content ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_text: not called CC=11 SCAR=48.8
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | return '' unless defined $content; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | return $content unless ref $content; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | return '' unless ref $content eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | my @parts; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | for my $part (@{$content}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | next unless ref $part eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | if (defined $part->{text}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | push @parts, $part->{text}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | if (defined $part->{content} && !ref $part->{content}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $$part{"content"} and not ref $$part{"content"}
MC/DC0%0/2
defined $$part{"content"} and not ref $$part{"content"}
defined $$part{"content"}
not ref $$part{"content"}
Truth table0%0/3
A and not(B)
A= defined $$part{"content"}B= not ref $$part{"content"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 139 | 0 | push @parts, $part->{content}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | if (defined $part->{delta} && !ref $part->{delta}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $$part{"delta"} and not ref $$part{"delta"}
MC/DC0%0/2
defined $$part{"delta"} and not ref $$part{"delta"}
defined $$part{"delta"}
not ref $$part{"delta"}
Truth table0%0/3
A and not(B)
A= defined $$part{"delta"}B= not ref $$part{"delta"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | push @parts, $part->{delta}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 148 | 0 | return join('', @parts); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 151 | sub _extract_response_text { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | ▶ | my ( $data ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_response_text: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | return '' unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my @pieces; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | for my $item (@{$data->{output} // []}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$$data{"output"} // []
MC/DC0%0/1
$$data{"output"} // []
$$data{"output"}
Truth table0%0/2
A
A= $$data{"output"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | ▶ | next unless ref $item eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 158 | push @pieces, $item->{content} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if ($item->{type} // '') eq 'message' && defined $item->{content}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
($$item{"type"} // "") eq "message" and defined $$item{"content"}
$$item{"type"} // ""
MC/DC0%0/2
($$item{"type"} // "") eq "message" and defined $$item{"content"}
$$item{"type"}
defined $$item{"content"}
Truth table0%0/3
A and B
A= $$item{"type"}B= defined $$item{"content"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 161 | 0 | ▶ | return join('', @pieces) if @pieces; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 163 | 0 | return ''; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 166 | sub _extract_reasoning_text { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | my ( $data ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_reasoning_text: not called CC=9 SCAR=45.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | return undef unless ref $data eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | my @parts; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | for my $item (@{$data->{output} // []}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$$data{"output"} // []
MC/DC0%0/1
$$data{"output"} // []
$$data{"output"}
Truth table0%0/2
A
A= $$data{"output"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 172 | 0 | ▶ | next unless ref $item eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 173 | push @parts, $item->{content} | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | if ($item->{type} // '') eq 'reasoning' && defined $item->{content}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
($$item{"type"} // "") eq "reasoning" and defined $$item{"content"}
$$item{"type"} // ""
MC/DC0%0/2
($$item{"type"} // "") eq "reasoning" and defined $$item{"content"}
$$item{"type"}
defined $$item{"content"}
Truth table0%0/3
A and B
A= $$item{"type"}B= defined $$item{"content"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | return @parts ? join("\n", @parts) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 179 | sub _normalize_input { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 180 | 4 | ▶ | my ( $messages ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_input: called CC=9 SCAR=23.6
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 181 | 4 | my @items; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 182 | 4 | for my $msg (@{$messages}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 183 | 5 | ▶ | next unless ref $msg eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 184 | 5 | ▶ | next if ($msg->{role} // '') eq 'system'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$$msg{"role"} // ""
MC/DC0%0/1
$$msg{"role"} // ""
$$msg{"role"}
Truth table50%1/2
A
A= $$msg{"role"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 185 | 4 | ▶ | next unless defined $msg->{content}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 186 | 4 | ▶ | my $content = ref $msg->{content} ? _extract_text($msg->{content}) : $msg->{content}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 187 | 4 | push @items, { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 188 | type => 'message', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 189 | content => $content, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 190 | }; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 193 | 4 | ▶ | return '' unless @items; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 194 | 4 | ▶ | return $items[0]{content} if @items == 1; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | return \@items; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 196 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 198 | sub _normalize_system_prompt { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 199 | 4 | ▶ | my ( $messages ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_system_prompt: called CC=7 SCAR=19.7
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 200 | 4 | my @system; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 201 | 4 | for my $msg (@{$messages}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 202 | 5 | ▶ | next unless ref $msg eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 203 | 5 | ▶ | next unless ($msg->{role} // '') eq 'system'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$$msg{"role"} // ""
MC/DC0%0/1
$$msg{"role"} // ""
$$msg{"role"}
Truth table50%1/2
A
A= $$msg{"role"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 204 | 1 | ▶ | next unless defined $msg->{content}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 205 | 1 | push @system, $msg->{content}; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 206 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 207 | 4 | ▶ | return @system ? join("\n\n", @system) : undef; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 210 | sub chat_request { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 211 | 4 | ▶ | my ( $self, $messages, %extra ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chat_request: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 212 | 4 | my $system_prompt = _normalize_system_prompt($messages); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 213 | 4 | my $input = _normalize_input($messages); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 215 | return $self->generate_request( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 216 | 'chat', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | ▶ | sub { $self->chat_response(shift) }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 218 | 4 | ▶ | model => $self->chat_model, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 219 | input => $input, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 220 | $system_prompt ? ( system_prompt => $system_prompt ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 221 | $self->has_temperature ? ( temperature => $self->temperature ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 222 | $self->get_response_size ? ( max_output_tokens => $self->get_response_size ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 223 | $self->has_context_size ? ( context_length => $self->get_context_size ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 224 | %extra, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 225 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 228 | sub chat_response { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | ▶ | my ( $self, $response ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chat_response: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 230 | 0 | my $data = $self->parse_response($response); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | my $text = _extract_response_text($data); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | my $thinking = _extract_reasoning_text($data); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 233 | my $usage = _normalize_usage({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 234 | input_tokens => $data->{stats}{input_tokens}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 235 | output_tokens => $data->{stats}{total_output_tokens}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 236 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | require Langertha::Response; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 239 | return Langertha::Response->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 240 | content => $text, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 241 | raw => $data, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 242 | $data->{response_id} ? ( id => $data->{response_id} ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 243 | 0 | ▶ | $data->{model_instance_id} ? ( model => $data->{model_instance_id} ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 244 | $usage ? ( usage => $usage ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 245 | defined $thinking ? ( thinking => $thinking ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 246 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 249 | 0 | ▶ | sub stream_format { 'sse' } | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stream_format: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 251 | sub chat_stream_request { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 252 | 0 | ▶ | my ( $self, $messages, %extra ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chat_stream_request: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 253 | 0 | my $system_prompt = _normalize_system_prompt($messages); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 254 | 0 | my $input = _normalize_input($messages); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 256 | return $self->generate_request( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 257 | 'chat', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 258 | ▶ | sub {}, | |||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | ▶ | model => $self->chat_model, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 260 | input => $input, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 261 | $system_prompt ? ( system_prompt => $system_prompt ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 262 | stream => JSON->true, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 263 | $self->has_temperature ? ( temperature => $self->temperature ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 264 | $self->get_response_size ? ( max_output_tokens => $self->get_response_size ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 265 | $self->has_context_size ? ( context_length => $self->get_context_size ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 266 | %extra, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 267 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 270 | sub parse_stream_chunk { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 271 | 0 | ▶ | my ( $self, $data, $event ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_stream_chunk: not called CC=13 SCAR=52.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 273 | 0 | require Langertha::Stream::Chunk; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 275 | # LM Studio native SSE event stream (/api/v1/chat) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 276 | 0 | ▶ | my $type = $data->{type} // $event // ''; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/5
($$data{"type"} // $event) // ""
$$data{"type"} // $event
MC/DC0%0/2
($$data{"type"} // $event) // ""
$$data{"type"}
$event
Truth table0%0/3
A // B
A= $$data{"type"}B= $event
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 277 | 0 | ▶ | if ($type eq 'error') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 278 | 0 | ▶ | my $message = ref $data->{error} eq 'HASH' ? ($data->{error}{message} // 'Unknown LM Studio stream error') : 'Unknown LM Studio stream error'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$$data{"error"}{"message"} // "Unknown LM Studio stream error"
MC/DC0%0/1
$$data{"error"}{"message"} // "Unknown LM Studio stream error"
$$data{"error"}{"message"}
Truth table0%0/2
A
A= $$data{"error"}{"message"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 279 | 0 | croak "LMStudio stream error: $message"; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | if ($type eq 'message.delta') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 282 | return Langertha::Stream::Chunk->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | content => $data->{content} // '', | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$$data{"content"} // ""
MC/DC0%0/1
$$data{"content"} // ""
$$data{"content"}
Truth table0%0/2
A
A= $$data{"content"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 284 | raw => $data, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 285 | is_final => 0, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 286 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if ($type eq 'chat.end') { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 289 | 0 | ▶ | my $result = $data->{result} || {}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
$$data{'result'} || {}
MC/DC0%0/1
$$data{'result'} || {}
$$data{'result'}
Truth table0%0/2
A
A= $$data{'result'}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 290 | my $usage = _normalize_usage({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 291 | input_tokens => $result->{stats}{input_tokens}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 292 | output_tokens => $result->{stats}{total_output_tokens}, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | }); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 295 | return Langertha::Stream::Chunk->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 296 | content => '', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 297 | raw => $data, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 298 | is_final => 1, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 299 | defined $result->{response_id} ? ( finish_reason => 'end' ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 300 | 0 | ▶ | $result->{model_instance_id} ? ( model => $result->{model_instance_id} ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 301 | $usage ? ( usage => $usage ) : (), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 302 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 305 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 308 | # Dynamic model listing | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 309 | sub list_models_request { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 310 | 0 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models_request: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 311 | return $self->generate_request( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 312 | 'listModels', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 313 | 0 | ▶ | sub { $self->list_models_response(shift) }, | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 314 | 0 | ); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 315 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 317 | sub list_models_response { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 318 | 0 | ▶ | my ( $self, $response ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models_response: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 319 | 0 | return $self->parse_response($response); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 322 | sub list_models { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 323 | 0 | ▶ | my ( $self, %opts ) = @_; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models: not called CC=16 SCAR=56.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 325 | 0 | ▶ | unless ($opts{force_refresh}) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 326 | 0 | my $cache = $self->_models_cache; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 327 | 0 | ▶ | if ($cache->{timestamp} && time - $cache->{timestamp} < $self->models_cache_ttl) { | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$$cache{"timestamp"} and time - $$cache{"timestamp"} < $self->models_cache_ttl
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 328 | 0 | ▶ | return $opts{full} ? $cache->{models} : $cache->{model_ids}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 329 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 332 | 0 | my $request = $self->list_models_request; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 333 | 0 | my $response = $self->user_agent->request($request); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 334 | 0 | my $data = $request->response_call->($response); | |||||||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 336 | my $models = ref $data eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 337 | 0 | ▶ | ? ($data->{data} // $data->{models} // []) | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/5
($$data{"data"} // $$data{"models"}) // []
$$data{"data"} // $$data{"models"}
MC/DC0%0/2
($$data{"data"} // $$data{"models"}) // []
$$data{"data"}
$$data{"models"}
Truth table0%0/3
A // B
A= $$data{"data"}B= $$data{"models"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 338 | : $data; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 339 | 0 | ▶ | $models = [] unless ref $models eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 341 | 0 | my @model_ids; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 342 | 0 | for my $model (@{$models}) { | |||||||||||||||||||||||||||||||||||||||||||||||||
| 343 | 0 | ▶ | next unless ref $model eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 344 | 0 | ▶ | my $id = $model->{key} // $model->{id} // $model->{model} // $model->{name}; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/9
(($$model{"key"} // $$model{"id"}) // $$model{"model"}) // $$model{"name"}
($$model{"key"} // $$model{"id"}) // $$model{"model"}
$$model{"key"} // $$model{"id"}
MC/DC0%0/4
(($$model{"key"} // $$model{"id"}) // $$model{"model"}) // $$model{"name"}
$$model{"key"}
$$model{"id"}
$$model{"model"}
$$model{"name"}
Truth table0%0/5
A // B // C // D
A= $$model{"key"}B= $$model{"id"}C= $$model{"model"}D= $$model{"name"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 345 | 0 | ▶ | push @model_ids, $id if defined $id; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 348 | $self->_models_cache({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | timestamp => time, | |||||||||||||||||||||||||||||||||||||||||||||||||
| 350 | models => $models, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 351 | model_ids => \@model_ids, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 352 | }); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 354 | 0 | ▶ | return $opts{full} ? $models : \@model_ids; | ||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 357 | 3 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 360 | 3 | 1; | |||||||||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 362 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 364 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 366 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 368 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 370 | Langertha::Engine::LMStudio - LM Studio native REST API | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 372 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 374 | version 0.502 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 376 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 378 | use Langertha::Engine::LMStudio; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 380 | my $lmstudio = Langertha::Engine::LMStudio->new( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 381 | url => 'http://localhost:1234', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 382 | model => 'qwen2.5-7b-instruct', | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 383 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 385 | print $lmstudio->simple_chat('Hello from LM Studio native API'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 387 | $lmstudio->simple_chat_stream(sub { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 388 | print shift->content; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 389 | }, 'Explain Perl Moo vs Moose'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 391 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 393 | Provides access to LM Studio's native local REST API (C</api/v1/...>), | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 394 | without using the OpenAI-compatible C</v1> endpoints. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 396 | Implemented operations: | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 398 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 400 | =item * Chat: C<POST /api/v1/chat> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 402 | =item * Streaming chat (SSE): C<stream => true> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 404 | =item * Model listing: C<GET /api/v1/models> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 406 | =item * OpenAI-compatible wrapper via L</openai> (C</v1>) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 408 | =item * Anthropic-compatible wrapper via L</anthropic> (C</v1/messages>) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 410 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 412 | Authentication is optional. If C<api_key> (or C<LANGERTHA_LMSTUDIO_API_KEY>) | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 413 | is set, requests include C<Authorization: Bearer ...>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 415 | B<THIS API IS WORK IN PROGRESS> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 417 | =head2 api_key | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 419 | Optional LM Studio API token for bearer authentication. If not provided, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 420 | reads from C<LANGERTHA_LMSTUDIO_API_KEY>. When undefined, no bearer header | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 421 | is sent. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 423 | =head2 openapi_file | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 425 | Returns the bundled native LM Studio OpenAPI spec file | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 426 | C<share/lmstudio.yaml>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 428 | =head2 openai | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 430 | my $oai = $lmstudio->openai; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 431 | my $oai = $lmstudio->openai(model => 'other-model'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 433 | Returns a L<Langertha::Engine::LMStudioOpenAI> instance configured for LM Studio's | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 434 | OpenAI-compatible C</v1> endpoint. Carries over model, api_key, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 435 | system_prompt, and temperature by default. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 437 | =head2 anthropic | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 439 | my $anthropic = $lmstudio->anthropic; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 440 | my $anthropic = $lmstudio->anthropic(model => 'other-model'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 442 | Returns a L<Langertha::Engine::LMStudioAnthropic> instance configured for | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 443 | LM Studio's Anthropic-compatible C</v1/messages> endpoint. Carries over model, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 444 | api_key, system_prompt, and temperature by default. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 446 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 448 | =over | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 450 | =item * L<https://lmstudio.ai/docs/developer> - LM Studio developer docs | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 452 | =item * L<Langertha::Engine::Ollama> - Another native local engine | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 454 | =item * L<Langertha::Engine::OpenAI> - Cloud OpenAI engine | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 456 | =back | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 458 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 460 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 462 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 463 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 465 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 467 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 469 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 471 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 473 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 475 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 477 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 479 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 481 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 482 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||||||||||
| 484 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||||||