lib/Langertha/Skeid/Protocol/Ollama.pm

filter: Statementstmt 94.0% 127 / 135 Branchbran 81.4% 70 / 86 Conditioncond 64.8% 83 / 128 MC/DCmcdc 37.3% 25 / 67 Subroutinesub 100% 20 / 20 Podpod 100% 11 / 11 totaltotal 75.1% 336 / 447 CC 100 SCAR 27.9
CC
100
Cov
80%
_messages_to_openai
38.6
response_from_openai
17.6
generate_response_from_openai
12.8
CRAP 16.3
« blib/lib/Langertha/Skeid/CapacityProbe/Registry.pm ↑ Summary lib/Langertha/Skeid/Proxy.pm »
1 package Langertha::Skeid::Protocol::Ollama;
2 128 our $VERSION = '0.005';
3 # ABSTRACT: Translate between the Ollama chat format and the upstream OpenAI call
4 128 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 128 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 128 ▶use Langertha::Skeid::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 128 ▶use Langertha::ToolCall;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 263 ▶sub request_to_openai {
Subroutine request_to_openai: called CC=7 SCAR=19.6
Pod documented
11 263 my ($class, $body) = @_;
12 263 ▶ my $options = ref($body->{options}) eq 'HASH' ? $body->{options} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $body->{'options'} eq 'HASH' ? : 1 262 263
13  
14 return {
15 model => ($body->{model} // ''),
16 messages => _messages_to_openai($body->{messages}),
17 (defined($options->{temperature}) ? (temperature => 0 + $options->{temperature}) : ()),
18 (defined($options->{num_predict}) ? (max_tokens => 0 + $options->{num_predict}) : ()),
19 (defined($body->{tools}) ? (tools => $body->{tools}) : ()),
20 (defined($body->{tool_choice}) ? (tool_choice => $body->{tool_choice}) : ()),
21 263 ▶ _response_format($body->{format}),
Branch: 4 branches
BranchTrueFalseTotal
defined $options->{'temperature'} ? : 1 262 263
defined $options->{'num_predict'} ? : 1 262 263
defined $body->{'tools'} ? : 2 261 263
defined $body->{'tool_choice'} ? : 0 263 263
Condition50%1/2
$body->{"model"} // ""
l!l
2630
MC/DC0%0/1
$body->{"model"} // ""
$body->{"model"}
Truth table50%1/2
A
A= $body->{"model"}
Aresult
11
00
22 };
23 }
24  
25 # Ollama's format -> OpenAI's response_format: "json" is any JSON object, a schema object is
26 # json_schema. Nothing else is a format (Ollama itself treats "" and null as none).
27 263 ▶sub _response_format {
Subroutine _response_format: called CC=5 SCAR=16.1
28 263 my ($format) = @_;
29 263 ▶ return (response_format => { type => 'json_object' })
Branch: 1 branch
BranchTrueFalseTotal
if defined $format and not ref $format and $format eq "json" 4 259 263
Condition100%6/6
defined $format and not ref $format and $format eq "json"
!ll&&!rl&&r
25544
defined $format and not ref $format
!ll&&!rl&&r
25058
MC/DC100%3/3
defined $format and not ref $format and $format eq "json"
defined $format not ref $format $format eq "json"
Truth table100%4/4
A and not(B) and C
A= defined $formatB= not ref $formatC= $format eq "json"
ABCresult
0XX0
10X0
1100
1111
30 if defined($format) && !ref($format) && $format eq 'json';
31 259 ▶ return (response_format => {
Branch: 1 branch
BranchTrueFalseTotal
if ref $format eq "HASH" 5 254 259
32 type => 'json_schema',
33 json_schema => { name => 'ollama_format', schema => $format },
34 }) if ref($format) eq 'HASH';
35 254 return ();
36 }
37  
38 # Assistant tool_calls: arguments object -> character JSON string, ids synthesized; tool
39 # messages: tool_name -> tool_call_id of the matching call of the preceding assistant turn.
40 263 ▶sub _messages_to_openai {
Subroutine _messages_to_openai: called CC=29 SCAR=36.5
41 263 my ($messages) = @_;
42 263 ▶ return [] unless ref($messages) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $messages eq "ARRAY" 0 263 263
43  
44 263 my $next_id = 0;
45 263 my @unanswered; # [ id, name ] of the latest assistant turn's calls, not yet answered
46 my @out;
47  
48 263 for my $msg (@$messages) {
49 278 ▶ if (ref($msg) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg ne "HASH") 0 278 278
50 0 push @out, $msg;
51 0 next;
52 }
53 278 ▶ my $role = $msg->{role} // '';
Condition50%1/2
$msg->{"role"} // ""
l!l
2780
MC/DC0%0/1
$msg->{"role"} // ""
$msg->{"role"}
Truth table50%1/2
A
A= $msg->{"role"}
Aresult
11
00
54  
55 278 ▶ if ($role eq 'assistant' && ref($msg->{tool_calls}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if ($role eq "assistant" and ref $msg->{"tool_calls"} eq "ARRAY") 5 273 278
Condition66%2/3
$role eq "assistant" and ref $msg->{"tool_calls"} eq "ARRAY"
!ll&&!rl&&r
27305
MC/DC50%1/2
$role eq "assistant" and ref $msg->{"tool_calls"} eq "ARRAY"
$role eq "assistant" ref $msg->{"tool_calls"} eq "ARRAY"
Truth table66%2/3
A and B
A= $role eq "assistant"B= ref $msg->{"tool_calls"} eq "ARRAY"
ABresult
0X0
100
111
56 5 @unanswered = ();
57 5 my @calls;
58 5 for my $call (@{$msg->{tool_calls}}) {
59 9 ▶ if (ref($call) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $call ne "HASH") 0 9 9
60 0 push @calls, $call;
61 0 next;
62 }
63 9 ▶ my $function = ref($call->{function}) eq 'HASH' ? $call->{function} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $call->{'function'} eq 'HASH' ? : 9 0 9
64 9 my $args = $function->{arguments};
65 9 ▶ my $id = (defined($call->{id}) && length($call->{id})) ? $call->{id} : 'call_skeid_' . $next_id++;
Branch: 1 branch
BranchTrueFalseTotal
defined $call->{'id'} && length $call->{'id'} ? : 1 8 9
Condition66%2/3
defined $call->{"id"} and length $call->{"id"}
!ll&&!rl&&r
801
MC/DC50%1/2
defined $call->{"id"} and length $call->{"id"}
defined $call->{"id"} length $call->{"id"}
Truth table66%2/3
A and B
A= defined $call->{"id"}B= length $call->{"id"}
ABresult
0X0
100
111
66 9 ▶ push @calls, {
Branch: 1 branch
BranchTrueFalseTotal
ref $args ? : 8 1 9
Condition50%1/2
$args // "{}"
l!l
10
MC/DC0%0/1
$args // "{}"
$args
Truth table50%1/2
A
A= $args
Aresult
11
00
67 %$call,
68 id => $id,
69 type => 'function',
70 function => {
71 %$function,
72 arguments => (ref($args) ? Langertha::Skeid::Protocol::encode_json_text_safe($args)
73 : ($args // '{}')),
74 },
75 };
76 9 ▶ push @unanswered, [ $id, $function->{name} // '' ];
Condition50%1/2
$function->{"name"} // ""
l!l
90
MC/DC0%0/1
$function->{"name"} // ""
$function->{"name"}
Truth table50%1/2
A
A= $function->{"name"}
Aresult
11
00
77 }
78 5 push @out, { %$msg, tool_calls => \@calls };
79 5 next;
80 }
81  
82 273 ▶ if ($role eq 'tool') {
Branch: 1 branch
BranchTrueFalseTotal
if ($role eq "tool") 10 263 273
83 10 my %tool = %$msg;
84 10 my $tool_name = delete $tool{tool_name};
85 10 ▶ if (defined($tool{tool_call_id}) && length($tool{tool_call_id})) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $tool{'tool_call_id'} and length $tool{'tool_call_id'}) { } 1 9 10
elsif (@unanswered) { } 9 0 9
Condition66%2/3
defined $tool{"tool_call_id"} and length $tool{"tool_call_id"}
!ll&&!rl&&r
901
MC/DC50%1/2
defined $tool{"tool_call_id"} and length $tool{"tool_call_id"}
defined $tool{"tool_call_id"} length $tool{"tool_call_id"}
Truth table66%2/3
A and B
A= defined $tool{"tool_call_id"}B= length $tool{"tool_call_id"}
ABresult
0X0
100
111
86 0 @unanswered = grep { $_->[0] ne $tool{tool_call_id} } @unanswered;
87 } elsif (@unanswered) {
88 9 ▶ my ($pick) = grep { defined($tool_name) && $unanswered[$_][1] eq $tool_name } 0 .. $#unanswered;
Condition100%3/3
defined $tool_name && $unanswered[$_][1] eq $tool_name
!ll&&!rl&&r
337
MC/DC100%2/2
defined $tool_name && $unanswered[$_][1] eq $tool_name
defined $tool_name $unanswered[$_][1] eq $tool_name
Truth table100%3/3
A && B
A= defined $tool_nameB= $unanswered[$_][1] eq $tool_name
ABresult
0X0
100
111
89 9 ▶ $pick //= 0;
Condition100%2/2
$pick //= 0
l!l
72
MC/DC100%1/1
$pick //= 0
$pick
Truth table100%2/2
A
A= $pick
Aresult
11
00
90 9 $tool{tool_call_id} = $unanswered[$pick][0];
91 9 splice @unanswered, $pick, 1;
92 }
93 10 push @out, \%tool;
94 10 next;
95 }
96  
97 263 ▶ if ($role ne 'assistant' && ref($msg->{images}) eq 'ARRAY' && @{$msg->{images}}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($role ne "assistant" and ref $msg->{"images"} eq "ARRAY" and @{$msg->{"images"};}) 4 259 263
Condition66%4/6
$role ne "assistant" and ref $msg->{"images"} eq "ARRAY" and @{$msg->{"images"};}
!ll&&!rl&&r
25904
$role ne "assistant" and ref $msg->{"images"} eq "ARRAY"
!ll&&!rl&&r
02594
MC/DC33%1/3
$role ne "assistant" and ref $msg->{"images"} eq "ARRAY" and @{$msg->{"images"};}
$role ne "assistant" ref $msg->{"images"} eq "ARRAY" @{$msg->{"images"};}
Truth table50%2/4
A and B and C
A= $role ne "assistant"B= ref $msg->{"images"} eq "ARRAY"C= @{$msg->{"images"};}
ABCresult
0XX0
10X0
1100
1111
98 4 my %with_images = %$msg;
99 4 my $images = delete $with_images{images};
100 4 my $text = $with_images{content};
101 $with_images{content} = [
102 ((defined($text) && !ref($text) && length($text)) ? { type => 'text', text => "$text" } : ()),
103 4 ▶ map { _image_part($_) } grep { defined($_) && !ref($_) && length($_) } @$images,
Branch: 1 branch
BranchTrueFalseTotal
defined $text && !ref($text) && length $text ? : 2 2 4
Condition41%5/12
defined $text and not ref $text and length $text
!ll&&!rl&&r
022
defined $text and not ref $text
!ll&&!rl&&r
004
defined $_ && !ref($_) && length $_
!ll&&!rl&&r
008
defined $_ and not ref $_
!ll&&!rl&&r
008
MC/DC33%1/3
defined $text and not ref $text and length $text
defined $text not ref $text length $text
MC/DC0%0/3
defined $_ && !ref($_) && length $_
defined $_ not ref $_ length $_
Truth table50%2/4
A and not(B) and C
A= defined $textB= not ref $textC= length $text
ABCresult
0XX0
10X0
1100
1111
Truth table25%1/4
A and not(B) && C
A= defined $_B= not ref $_C= length $_
ABCresult
0XX0
10X0
1100
1111
104 ];
105 4 push @out, \%with_images;
106 4 next;
107 }
108  
109 259 push @out, $msg;
110 }
111  
112 263 return \@out;
113 }
114  
115 # Ollama sends images as raw base64 without a type; the OpenAI upstream wants a URL. A client
116 # that already sends a data: URL keeps it.
117 8 ▶sub _image_part {
Subroutine _image_part: called CC=2 SCAR=7.1
118 8 my ($image) = @_;
119 8 ▶ return Langertha::Skeid::Protocol::image_url_part($image) if $image =~ /\Adata:/;
Branch: 1 branch
BranchTrueFalseTotal
if $image =~ /\Adata:/ 0 8 8
120 8 return Langertha::Skeid::Protocol::image_url_part(undef, $image);
121 }
122  
123  
124 84 ▶sub response_from_openai {
Subroutine response_from_openai: called CC=17 SCAR=28.7
Pod documented
125 84 my ($class, $res) = @_;
126 84 ▶ my $choice = (ref($res->{choices}) eq 'ARRAY' ? $res->{choices}[0] : {}) || {};
Branch: 1 branch
BranchTrueFalseTotal
ref $res->{'choices'} eq 'ARRAY' ? : 83 1 84
Condition50%1/2
(ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {}) || {}
l!l
840
MC/DC0%0/1
(ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {}) || {}
(ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {})
Truth table50%1/2
A
A= (ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {})
Aresult
11
00
127 84 ▶ my $msg = $choice->{message} || {};
Condition100%2/2
$choice->{'message'} || {}
l!l
831
MC/DC100%1/1
$choice->{'message'} || {}
$choice->{'message'}
Truth table100%2/2
A
A= $choice->{'message'}
Aresult
11
00
128 84 ▶ my $text = ($msg->{content} // '');
Condition100%2/2
$msg->{"content"} // ""
l!l
831
MC/DC100%1/1
$msg->{"content"} // ""
$msg->{"content"}
Truth table100%2/2
A
A= $msg->{"content"}
Aresult
11
00
129 84 my $tool_calls = [];
130  
131 84 ▶ if (ref($msg->{tool_calls}) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $msg->{'tool_calls'} eq 'ARRAY') { } 1 83 84
elsif (length $text) { } 82 1 83
132 1 ▶ my @calls = Langertha::ToolCall->extract('openai', $res || {});
Condition50%1/2
$res || {}
l!l
10
MC/DC0%0/1
$res || {}
$res
Truth table50%1/2
A
A= $res
Aresult
11
00
133 1 $tool_calls = [ map { $_->to_ollama } @calls ];
134 } elsif (length($text)) {
135 82 my ($clean, $calls) = Langertha::ToolCall->extract_hermes_from_text($text);
136 82 ▶ if (@$calls) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$calls) 0 82 82
137 0 $text = $clean;
138 0 $tool_calls = [ map { $_->to_ollama } @$calls ];
139 }
140 }
141  
142 return {
143 model => ($res->{model} // ''),
144 created_at => Langertha::Skeid::Protocol::iso8601_now(),
145 message => {
146 role => ($msg->{role} // 'assistant'),
147 content => $text,
148 (@$tool_calls ? (tool_calls => $tool_calls) : ()),
149 },
150 done => \1,
151 done_reason => ($choice->{finish_reason} // 'stop'),
152 prompt_eval_count => 0 + (($res->{usage} || {})->{prompt_tokens} // 0),
153 84 ▶ eval_count => 0 + (($res->{usage} || {})->{completion_tokens} // 0),
Branch: 1 branch
BranchTrueFalseTotal
@$tool_calls ? : 1 83 84
Condition100%14/14
$res->{"model"} // ""
l!l
831
$msg->{"role"} // "assistant"
l!l
831
$choice->{"finish_reason"} // "stop"
l!l
831
+($res->{"usage"} || {})->{"prompt_tokens"} // 0
l!l
804
$res->{'usage'} || {}
l!l
822
+($res->{"usage"} || {})->{"completion_tokens"} // 0
l!l
804
$res->{'usage'} || {}
l!l
822
MC/DC100%1/1
$res->{"model"} // ""
$res->{"model"}
MC/DC100%1/1
$msg->{"role"} // "assistant"
$msg->{"role"}
MC/DC100%1/1
$choice->{"finish_reason"} // "stop"
$choice->{"finish_reason"}
MC/DC100%1/1
+($res->{"usage"} || {})->{"prompt_tokens"} // 0
$res->{'usage'}
MC/DC100%1/1
+($res->{"usage"} || {})->{"completion_tokens"} // 0
$res->{'usage'}
Truth table100%2/2
A
A= $res->{"model"}
Aresult
11
00
Truth table100%2/2
A
A= $msg->{"role"}
Aresult
11
00
Truth table100%2/2
A
A= $choice->{"finish_reason"}
Aresult
11
00
Truth table100%2/2
A
A= $res->{'usage'}
Aresult
11
00
Truth table100%2/2
A
A= $res->{'usage'}
Aresult
11
00
154 };
155 }
156  
157  
158 96 ▶sub generate_request_to_openai {
Subroutine generate_request_to_openai: called CC=10 SCAR=24.1
Pod documented
159 96 my ($class, $body) = @_;
160 96 my $prompt = $body->{prompt};
161 96 my @messages;
162 push @messages, { role => 'system', content => "$body->{system}" }
163 96 ▶ if defined($body->{system}) && !ref($body->{system}) && length($body->{system});
Branch: 1 branch
BranchTrueFalseTotal
if defined $body->{"system"} and not ref $body->{"system"} and length $body->{"system"} 1 95 96
Condition66%4/6
defined $body->{"system"} and not ref $body->{"system"} and length $body->{"system"}
!ll&&!rl&&r
9501
defined $body->{"system"} and not ref $body->{"system"}
!ll&&!rl&&r
9501
MC/DC33%1/3
defined $body->{"system"} and not ref $body->{"system"} and length $body->{"system"}
defined $body->{"system"} not ref $body->{"system"} length $body->{"system"}
Truth table50%2/4
A and not(B) and C
A= defined $body->{"system"}B= not ref $body->{"system"}C= length $body->{"system"}
ABCresult
0XX0
10X0
1100
1111
164 push @messages, {
165 role => 'user',
166 content => ((defined($prompt) && !ref($prompt)) ? "$prompt" : ''),
167 96 ▶ (ref($body->{images}) eq 'ARRAY' ? (images => $body->{images}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $prompt && !ref($prompt) ? : 96 0 96
ref $body->{'images'} eq 'ARRAY' ? : 2 94 96
Condition33%1/3
defined $prompt and not ref $prompt
!ll&&!rl&&r
0096
MC/DC0%0/2
defined $prompt and not ref $prompt
defined $prompt not ref $prompt
Truth table33%1/3
A and not(B)
A= defined $promptB= not ref $prompt
ABresult
0X0
100
111
168 };
169  
170 return $class->request_to_openai({
171 (exists $body->{model} ? (model => $body->{model}) : ()),
172 (exists $body->{options} ? (options => $body->{options}) : ()),
173 96 ▶ (exists $body->{format} ? (format => $body->{format}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
exists $body->{'model'} ? : 96 0 96
exists $body->{'options'} ? : 1 95 96
exists $body->{'format'} ? : 8 88 96
174 messages => \@messages,
175 });
176 }
177  
178  
179 35 ▶sub generate_response_from_openai {
Subroutine generate_response_from_openai: called CC=10 SCAR=25.5
Pod documented
180 35 my ($class, $res) = @_;
181 35 ▶ my $choice = (ref($res->{choices}) eq 'ARRAY' ? $res->{choices}[0] : {}) || {};
Branch: 1 branch
BranchTrueFalseTotal
ref $res->{'choices'} eq 'ARRAY' ? : 35 0 35
Condition50%1/2
(ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {}) || {}
l!l
350
MC/DC0%0/1
(ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {}) || {}
(ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {})
Truth table50%1/2
A
A= (ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {})
Aresult
11
00
182 35 ▶ my $msg = $choice->{message} || {};
Condition50%1/2
$choice->{'message'} || {}
l!l
350
MC/DC0%0/1
$choice->{'message'} || {}
$choice->{'message'}
Truth table50%1/2
A
A= $choice->{'message'}
Aresult
11
00
183 35 ▶ my $usage = $res->{usage} || {};
Condition50%1/2
$res->{'usage'} || {}
l!l
350
MC/DC0%0/1
$res->{'usage'} || {}
$res->{'usage'}
Truth table50%1/2
A
A= $res->{'usage'}
Aresult
11
00
184  
185 return {
186 model => ($res->{model} // ''),
187 created_at => Langertha::Skeid::Protocol::iso8601_now(),
188 response => ($msg->{content} // ''),
189 done => \1,
190 done_reason => ($choice->{finish_reason} // 'stop'),
191 prompt_eval_count => 0 + ($usage->{prompt_tokens} // 0),
192 35 ▶ eval_count => 0 + ($usage->{completion_tokens} // 0),
Condition70%7/10
$res->{"model"} // ""
l!l
350
$msg->{"content"} // ""
l!l
350
$choice->{"finish_reason"} // "stop"
l!l
350
$usage->{"prompt_tokens"} // 0
l!l
332
$usage->{"completion_tokens"} // 0
l!l
332
MC/DC0%0/1
$res->{"model"} // ""
$res->{"model"}
MC/DC0%0/1
$msg->{"content"} // ""
$msg->{"content"}
MC/DC0%0/1
$choice->{"finish_reason"} // "stop"
$choice->{"finish_reason"}
MC/DC100%1/1
$usage->{"prompt_tokens"} // 0
$usage->{"prompt_tokens"}
MC/DC100%1/1
$usage->{"completion_tokens"} // 0
$usage->{"completion_tokens"}
Truth table50%1/2
A
A= $res->{"model"}
Aresult
11
00
Truth table50%1/2
A
A= $msg->{"content"}
Aresult
11
00
Truth table50%1/2
A
A= $choice->{"finish_reason"}
Aresult
11
00
Truth table100%2/2
A
A= $usage->{"prompt_tokens"}
Aresult
11
00
Truth table100%2/2
A
A= $usage->{"completion_tokens"}
Aresult
11
00
193 };
194 }
195  
196  
197 8 ▶sub embed_request_to_openai {
Subroutine embed_request_to_openai: called CC=6 SCAR=18.0
Pod documented
198 8 my ($class, $body) = @_;
199 8 my $input = $body->{input};
200 8 ▶ die "input must be a string or a list of strings\n"
Branch: 1 branch
BranchTrueFalseTotal
unless _embedding_input($input) 1 7 8
201 unless _embedding_input($input);
202 return {
203 model => ($body->{model} // ''),
204 4 ▶ input => (ref($input) ? [ map { "$_" } @$input ] : "$input"),
Branch: 1 branch
BranchTrueFalseTotal
defined $body->{'dimensions'} && !ref($body->{'dimensions'}) ? : 1 6 7
Condition66%2/3
defined $body->{"dimensions"} and not ref $body->{"dimensions"}
!ll&&!rl&&r
601
MC/DC50%1/2
defined $body->{"dimensions"} and not ref $body->{"dimensions"}
defined $body->{"dimensions"} not ref $body->{"dimensions"}
Truth table66%2/3
A and not(B)
A= defined $body->{"dimensions"}B= not ref $body->{"dimensions"}
ABresult
0X0
100
111
205 7 ▶ (defined($body->{dimensions}) && !ref($body->{dimensions}) ? (dimensions => 0 + $body->{dimensions}) : ()),
Branch: 1 branch
BranchTrueFalseTotal
ref $input ? : 2 5 7
Condition50%1/2
$body->{"model"} // ""
l!l
70
MC/DC0%0/1
$body->{"model"} // ""
$body->{"model"}
Truth table50%1/2
A
A= $body->{"model"}
Aresult
11
00
206 };
207 }
208  
209  
210 4 ▶sub embed_response_from_openai {
Subroutine embed_response_from_openai: called CC=6 SCAR=23.5
Pod documented
211 4 my ($class, $res, $model) = @_;
212 4 ▶ my $usage = ref($res->{usage}) eq 'HASH' ? $res->{usage} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $res->{'usage'} eq 'HASH' ? : 4 0 4
213 return {
214 model => ((defined($model) && length($model)) ? $model : ($res->{model} // '')),
215 embeddings => _vectors($res),
216 4 ▶ prompt_eval_count => 0 + ($usage->{prompt_tokens} // 0),
Branch: 1 branch
BranchTrueFalseTotal
defined $model && length $model ? : 4 0 4
Condition28%2/7
defined $model and length $model
!ll&&!rl&&r
004
$res->{"model"} // ""
l!l
00
$usage->{"prompt_tokens"} // 0
l!l
40
MC/DC0%0/2
defined $model and length $model
defined $model length $model
MC/DC0%0/1
$res->{"model"} // ""
$res->{"model"}
MC/DC0%0/1
$usage->{"prompt_tokens"} // 0
$usage->{"prompt_tokens"}
Truth table33%1/3
A and B
A= defined $modelB= length $model
ABresult
0X0
100
111
Truth table0%0/2
A
A= $res->{"model"}
Aresult
11
00
Truth table50%1/2
A
A= $usage->{"prompt_tokens"}
Aresult
11
00
217 };
218 }
219  
220  
221 3 ▶sub embeddings_request_to_openai {
Subroutine embeddings_request_to_openai: called CC=4 SCAR=14.1
Pod documented
222 3 my ($class, $body) = @_;
223 3 my $prompt = $body->{prompt};
224 3 ▶ die "prompt must be a string\n" unless defined($prompt) && !ref($prompt);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $prompt and not ref $prompt 1 2 3
Condition66%2/3
defined $prompt and not ref $prompt
!ll&&!rl&&r
102
MC/DC50%1/2
defined $prompt and not ref $prompt
defined $prompt not ref $prompt
Truth table66%2/3
A and not(B)
A= defined $promptB= not ref $prompt
ABresult
0X0
100
111
225 2 ▶ return { model => ($body->{model} // ''), input => "$prompt" };
Condition50%1/2
$body->{"model"} // ""
l!l
20
MC/DC0%0/1
$body->{"model"} // ""
$body->{"model"}
Truth table50%1/2
A
A= $body->{"model"}
Aresult
11
00
226 }
227  
228  
229 2 ▶sub embeddings_response_from_openai {
Subroutine embeddings_response_from_openai: called CC=2 SCAR=7.2
Pod documented
230 2 my ($class, $res) = @_;
231 2 ▶ return { embedding => (_vectors($res)->[0] // []) };
Condition50%1/2
_vectors($res)->[0] // []
l!l
20
MC/DC0%0/1
_vectors($res)->[0] // []
_vectors($res)->[0]
Truth table50%1/2
A
A= _vectors($res)->[0]
Aresult
11
00
232 }
233  
234 8 ▶sub _embedding_input {
Subroutine _embedding_input: called CC=6 SCAR=18.6
235 8 my ($input) = @_;
236 8 ▶ return 0 unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 8 8
237 8 ▶ return 1 unless ref $input;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input 5 3 8
238 3 ▶ return 0 unless ref($input) eq 'ARRAY' && @$input;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $input eq "ARRAY" and @$input 1 2 3
Condition66%2/3
ref $input eq "ARRAY" and @$input
!ll&&!rl&&r
102
MC/DC50%1/2
ref $input eq "ARRAY" and @$input
ref $input eq "ARRAY" @$input
Truth table66%2/3
A and B
A= ref $input eq "ARRAY"B= @$input
ABresult
0X0
100
111
239 2 ▶ return !grep { !defined($_) || ref($_) } @$input;
Condition33%1/3
!defined($_) || ref $_
l!l&&r!l&&!r
004
MC/DC0%0/2
!defined($_) || ref $_
!defined($_) ref $_
Truth table33%1/3
not(A) || B
A= !defined($_)B= ref $_
ABresult
1X1
011
000
240 }
241  
242 # The vectors of an OpenAI embeddings answer, in input order. A node may answer data out of
243 # order; `index` says where each belongs.
244 6 ▶sub _vectors {
Subroutine _vectors: called CC=5 SCAR=16.8
245 6 my ($res) = @_;
246 6 ▶ my @data = grep { ref($_) eq 'HASH' } @{ ref($res->{data}) eq 'ARRAY' ? $res->{data} : [] };
Branch: 1 branch
BranchTrueFalseTotal
ref $res->{'data'} eq 'ARRAY' ? : 6 0 6
247 6 ▶ @data = sort { ($a->{index} // 0) <=> ($b->{index} // 0) } @data;
Condition50%2/4
$Langertha::Skeid::Protocol::Ollama::a->{"index"} // 0
l!l
20
$Langertha::Skeid::Protocol::Ollama::b->{"index"} // 0
l!l
20
MC/DC0%0/1
$Langertha::Skeid::Protocol::Ollama::a->{"index"} // 0
$Langertha::Skeid::Protocol::Ollama::a->{"index"}
MC/DC0%0/1
$Langertha::Skeid::Protocol::Ollama::b->{"index"} // 0
$Langertha::Skeid::Protocol::Ollama::b->{"index"}
Truth table50%1/2
A
A= $Langertha::Skeid::Protocol::Ollama::a->{"index"}
Aresult
11
00
Truth table50%1/2
A
A= $Langertha::Skeid::Protocol::Ollama::b->{"index"}
Aresult
11
00
248 6 ▶ return [ map { ref($_->{embedding}) eq 'ARRAY' ? $_->{embedding} : [] } @data ];
Branch: 1 branch
BranchTrueFalseTotal
ref $_->{'embedding'} eq 'ARRAY' ? : 8 0 8
249 }
250  
251  
252 15 ▶sub tags_from_models {
Subroutine tags_from_models: called CC=3 SCAR=11.0
Pod documented
253 15 my ($class, $models) = @_;
254 my @models = map {
255 +{
256 name => $_->{model},
257 model => $_->{model},
258 modified_at => Langertha::Skeid::Protocol::iso8601_now(),
259 size => 0,
260 digest => '',
261 details => {
262 29 ▶ family => ($_->{engine} || 'openaibase'),
Condition100%2/2
$_->{'engine'} || 'openaibase'
l!l
236
MC/DC100%1/1
$_->{'engine'} || 'openaibase'
$_->{'engine'}
Truth table100%2/2
A
A= $_->{'engine'}
Aresult
11
00
263 parameter_size => 'unknown',
264 quantization_level => 'unknown',
265 },
266 }
267 15 ▶ } @{$models || []};
Condition50%1/2
$models || []
l!l
150
MC/DC0%0/1
$models || []
$models
Truth table50%1/2
A
A= $models
Aresult
11
00
268  
269 15 return { models => \@models };
270 }
271  
272  
273 19 ▶sub manifest_endpoint {
Subroutine manifest_endpoint: called CC=1 SCAR=0.0
Pod documented
274 return {
275 19 dialect => 'ollama',
276 path => '',
277 capabilities => [qw(
278 chat streaming system_prompt
279 tools_native tools_hermes
280 temperature response_size
281 image_input
282 response_format_json_object response_format_json_schema
283 )],
284 };
285 }
286  
287  
288 113 ▶sub error_body {
Subroutine error_body: called CC=2 SCAR=7.2
Pod documented
289 113 my ($class, $message) = @_;
290 113 ▶ return { error => '' . ($message // '') };
Condition50%1/2
$message // ""
l!l
1130
MC/DC0%0/1
$message // ""
$message
Truth table50%1/2
A
A= $message
Aresult
11
00
291 }
292  
293  
294 128 1;
295  
296 __END__
« blib/lib/Langertha/Skeid/CapacityProbe/Registry.pm ↑ Summary lib/Langertha/Skeid/Proxy.pm »