blib/lib/Langertha/Skeid/Protocol/Ollama.pm

filter: Statementstmt 92.5% 99 / 107 Branchbran 82.8% 53 / 64 Conditioncond 69.7% 69 / 99 MC/DCmcdc 44.0% 22 / 50 Subroutinesub 100% 14 / 14 Podpod 100% 7 / 7 totaltotal 77.4% 264 / 341 CC 77 SCAR 29.7
CC
77
Cov
82%
_messages_to_openai
38.6
response_from_openai
17.6
generate_response_from_openai
12.8
CRAP 19.5
« blib/lib/Langertha/Skeid/UsageStore/DBI.pm ↑ Summary blib/lib/Langertha/Skeid/CapacityProbe/Registry.pm »
1 package Langertha::Skeid::Protocol::Ollama;
2 68 our $VERSION = '0.003';
3 # ABSTRACT: Translate between the Ollama chat format and the upstream OpenAI call
4 68 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 68 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 68 ▶use Langertha::Skeid::Protocol;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 68 ▶use Langertha::ToolCall;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9  
10 208 ▶sub request_to_openai {
Subroutine request_to_openai: called CC=7 SCAR=19.6
Pod documented
11 208 my ($class, $body) = @_;
12 208 ▶ my $options = ref($body->{options}) eq 'HASH' ? $body->{options} : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $body->{'options'} eq 'HASH' ? : 1 207 208
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 208 ▶ _response_format($body->{format}),
Branch: 4 branches
BranchTrueFalseTotal
defined $options->{'temperature'} ? : 1 207 208
defined $options->{'num_predict'} ? : 1 207 208
defined $body->{'tools'} ? : 1 207 208
defined $body->{'tool_choice'} ? : 0 208 208
Condition50%1/2
$body->{"model"} // ""
l!l
2080
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 208 ▶sub _response_format {
Subroutine _response_format: called CC=5 SCAR=16.1
28 208 my ($format) = @_;
29 208 ▶ return (response_format => { type => 'json_object' })
Branch: 1 branch
BranchTrueFalseTotal
if defined $format and not ref $format and $format eq "json" 4 204 208
Condition100%6/6
defined $format and not ref $format and $format eq "json"
!ll&&!rl&&r
20044
defined $format and not ref $format
!ll&&!rl&&r
19558
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 204 ▶ return (response_format => {
Branch: 1 branch
BranchTrueFalseTotal
if ref $format eq "HASH" 5 199 204
32 type => 'json_schema',
33 json_schema => { name => 'ollama_format', schema => $format },
34 }) if ref($format) eq 'HASH';
35 199 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 208 ▶sub _messages_to_openai {
Subroutine _messages_to_openai: called CC=29 SCAR=36.5
41 208 my ($messages) = @_;
42 208 ▶ return [] unless ref($messages) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $messages eq "ARRAY" 0 208 208
43  
44 208 my $next_id = 0;
45 208 my @unanswered; # [ id, name ] of the latest assistant turn's calls, not yet answered
46 my @out;
47  
48 208 for my $msg (@$messages) {
49 223 ▶ if (ref($msg) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg ne "HASH") 0 223 223
50 0 push @out, $msg;
51 0 next;
52 }
53 223 ▶ my $role = $msg->{role} // '';
Condition50%1/2
$msg->{"role"} // ""
l!l
2230
MC/DC0%0/1
$msg->{"role"} // ""
$msg->{"role"}
Truth table50%1/2
A
A= $msg->{"role"}
Aresult
11
00
54  
55 223 ▶ 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 218 223
Condition66%2/3
$role eq "assistant" and ref $msg->{"tool_calls"} eq "ARRAY"
!ll&&!rl&&r
21805
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 218 ▶ if ($role eq 'tool') {
Branch: 1 branch
BranchTrueFalseTotal
if ($role eq "tool") 10 208 218
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 1 @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 208 ▶ 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 204 208
Condition66%4/6
$role ne "assistant" and ref $msg->{"images"} eq "ARRAY" and @{$msg->{"images"};}
!ll&&!rl&&r
20404
$role ne "assistant" and ref $msg->{"images"} eq "ARRAY"
!ll&&!rl&&r
02044
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 204 push @out, $msg;
110 }
111  
112 208 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 37 ▶sub response_from_openai {
Subroutine response_from_openai: called CC=17 SCAR=28.7
Pod documented
125 37 my ($class, $res) = @_;
126 37 ▶ my $choice = (ref($res->{choices}) eq 'ARRAY' ? $res->{choices}[0] : {}) || {};
Branch: 1 branch
BranchTrueFalseTotal
ref $res->{'choices'} eq 'ARRAY' ? : 36 1 37
Condition50%1/2
(ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {}) || {}
l!l
370
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 37 ▶ my $msg = $choice->{message} || {};
Condition100%2/2
$choice->{'message'} || {}
l!l
361
MC/DC100%1/1
$choice->{'message'} || {}
$choice->{'message'}
Truth table100%2/2
A
A= $choice->{'message'}
Aresult
11
00
128 37 ▶ my $text = ($msg->{content} // '');
Condition100%2/2
$msg->{"content"} // ""
l!l
361
MC/DC100%1/1
$msg->{"content"} // ""
$msg->{"content"}
Truth table100%2/2
A
A= $msg->{"content"}
Aresult
11
00
129 37 my $tool_calls = [];
130  
131 37 ▶ if (ref($msg->{tool_calls}) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $msg->{'tool_calls'} eq 'ARRAY') { } 1 36 37
elsif (length $text) { } 35 1 36
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 35 my ($clean, $calls) = Langertha::ToolCall->extract_hermes_from_text($text);
136 35 ▶ if (@$calls) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$calls) 0 35 35
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 37 ▶ eval_count => 0 + (($res->{usage} || {})->{completion_tokens} // 0),
Branch: 1 branch
BranchTrueFalseTotal
@$tool_calls ? : 1 36 37
Condition100%14/14
$res->{"model"} // ""
l!l
361
$msg->{"role"} // "assistant"
l!l
361
$choice->{"finish_reason"} // "stop"
l!l
361
+($res->{"usage"} || {})->{"prompt_tokens"} // 0
l!l
343
$res->{'usage'} || {}
l!l
361
+($res->{"usage"} || {})->{"completion_tokens"} // 0
l!l
343
$res->{'usage'} || {}
l!l
361
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 91 ▶sub generate_request_to_openai {
Subroutine generate_request_to_openai: called CC=10 SCAR=24.1
Pod documented
159 91 my ($class, $body) = @_;
160 91 my $prompt = $body->{prompt};
161 91 my @messages;
162 push @messages, { role => 'system', content => "$body->{system}" }
163 91 ▶ 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 90 91
Condition66%4/6
defined $body->{"system"} and not ref $body->{"system"} and length $body->{"system"}
!ll&&!rl&&r
9001
defined $body->{"system"} and not ref $body->{"system"}
!ll&&!rl&&r
9001
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 91 ▶ (ref($body->{images}) eq 'ARRAY' ? (images => $body->{images}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
defined $prompt && !ref($prompt) ? : 91 0 91
ref $body->{'images'} eq 'ARRAY' ? : 2 89 91
Condition33%1/3
defined $prompt and not ref $prompt
!ll&&!rl&&r
0091
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 91 ▶ (exists $body->{format} ? (format => $body->{format}) : ()),
Branch: 3 branches
BranchTrueFalseTotal
exists $body->{'model'} ? : 91 0 91
exists $body->{'options'} ? : 1 90 91
exists $body->{'format'} ? : 8 83 91
174 messages => \@messages,
175 });
176 }
177  
178  
179 30 ▶sub generate_response_from_openai {
Subroutine generate_response_from_openai: called CC=10 SCAR=25.5
Pod documented
180 30 my ($class, $res) = @_;
181 30 ▶ my $choice = (ref($res->{choices}) eq 'ARRAY' ? $res->{choices}[0] : {}) || {};
Branch: 1 branch
BranchTrueFalseTotal
ref $res->{'choices'} eq 'ARRAY' ? : 30 0 30
Condition50%1/2
(ref $res->{'choices'} eq 'ARRAY' ? $res->{'choices'}[0] : {}) || {}
l!l
300
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 30 ▶ my $msg = $choice->{message} || {};
Condition50%1/2
$choice->{'message'} || {}
l!l
300
MC/DC0%0/1
$choice->{'message'} || {}
$choice->{'message'}
Truth table50%1/2
A
A= $choice->{'message'}
Aresult
11
00
183 30 ▶ my $usage = $res->{usage} || {};
Condition50%1/2
$res->{'usage'} || {}
l!l
300
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 30 ▶ eval_count => 0 + ($usage->{completion_tokens} // 0),
Condition70%7/10
$res->{"model"} // ""
l!l
300
$msg->{"content"} // ""
l!l
300
$choice->{"finish_reason"} // "stop"
l!l
300
$usage->{"prompt_tokens"} // 0
l!l
282
$usage->{"completion_tokens"} // 0
l!l
282
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 7 ▶sub tags_from_models {
Subroutine tags_from_models: called CC=3 SCAR=11.0
Pod documented
198 7 my ($class, $models) = @_;
199 my @models = map {
200 +{
201 name => $_->{model},
202 model => $_->{model},
203 modified_at => Langertha::Skeid::Protocol::iso8601_now(),
204 size => 0,
205 digest => '',
206 details => {
207 17 ▶ family => ($_->{engine} || 'openaibase'),
Condition100%2/2
$_->{'engine'} || 'openaibase'
l!l
134
MC/DC100%1/1
$_->{'engine'} || 'openaibase'
$_->{'engine'}
Truth table100%2/2
A
A= $_->{'engine'}
Aresult
11
00
208 parameter_size => 'unknown',
209 quantization_level => 'unknown',
210 },
211 }
212 7 ▶ } @{$models || []};
Condition50%1/2
$models || []
l!l
70
MC/DC0%0/1
$models || []
$models
Truth table50%1/2
A
A= $models
Aresult
11
00
213  
214 7 return { models => \@models };
215 }
216  
217  
218 18 ▶sub manifest_endpoint {
Subroutine manifest_endpoint: called CC=1 SCAR=0.0
Pod documented
219 return {
220 18 dialect => 'ollama',
221 path => '',
222 capabilities => [qw(
223 chat streaming system_prompt
224 tools_native tools_hermes
225 temperature response_size
226 image_input
227 response_format_json_object response_format_json_schema
228 )],
229 };
230 }
231  
232  
233 88 ▶sub error_body {
Subroutine error_body: called CC=2 SCAR=7.2
Pod documented
234 88 my ($class, $message) = @_;
235 88 ▶ return { error => '' . ($message // '') };
Condition50%1/2
$message // ""
l!l
880
MC/DC0%0/1
$message // ""
$message
Truth table50%1/2
A
A= $message
Aresult
11
00
236 }
237  
238  
239 68 1;
240  
241 __END__
242  
243 =pod
244  
245 =encoding UTF-8
246  
247 =head1 NAME
248  
249 Langertha::Skeid::Protocol::Ollama - Translate between the Ollama chat format and the upstream OpenAI call
250  
251 =head1 VERSION
252  
253 version 0.003
254  
255 =head1 DESCRIPTION
256  
257 Serves C<POST /api/chat>, C<POST /api/generate> and C<GET /api/tags>. Ollama-specific field names — C<done_reason>,
258 C<prompt_eval_count>, C<eval_count> — live here and nowhere else in Skeid.
259  
260 Ollama's messages are already OpenAI-shaped, so the request translation is small — but it is
261 not empty: generation settings arrive nested under C<options> with Ollama's own names.
262  
263 Streaming is translated, not refused. C<stream: true> on this route — and an absent
264 C<stream> field, which Ollama defaults to true — is rewritten to an OpenAI stream with
265 C<stream_options.include_usage>; the response is re-emitted as Ollama's newline-delimited
266 JSON, one line per delta and a closing line that carries C<done>, C<done_reason>, and the
267 token counts an Ollama client reads from. C<done_reason> is the OpenAI C<finish_reason>
268 passed through verbatim — there is no translation to do. See
269 L<Langertha::Skeid::Protocol::Ollama::Stream> for the per-chunk rewrite and
270 F<t/31-stream-translation.t> for what the wire looks like end-to-end.
271  
272 =head2 request_to_openai
273  
274 my $openai_body = Langertha::Skeid::Protocol::Ollama->request_to_openai($body);
275  
276 Turns an Ollama chat request into the OpenAI chat-completions body Skeid forwards.
277 C<options.temperature> and C<options.num_predict> are lifted out of the nested hash to
278 C<temperature> and C<max_tokens>; C<tools> and C<tool_choice> pass through unchanged. No other
279 option is carried.
280  
281 Tool-call history is made OpenAI-shaped: an assistant message's C<tool_calls> get their
282 C<arguments> object encoded as a JSON string and an id (C<call_skeid_N>) where they have none,
283 and a C<tool> message that names its call only by C<tool_name> gets the C<tool_call_id> of the
284 matching unanswered call of the preceding assistant turn (the first unanswered one when no name
285 matches).
286  
287 C<format>, Ollama's structured output, becomes C<response_format>: C<"json"> is
288 C<< {type => 'json_object'} >>, a JSON schema object is
289 C<< {type => 'json_schema', json_schema => {name => 'ollama_format', schema => ...}} >> with the
290 schema as sent. No C<strict> is set -- Ollama's format has no such switch. An empty string,
291 C<null> or any other value is no format, and no C<response_format> is sent.
292  
293 A user message's C<images>, raw base64 strings, become an OpenAI content array: the message
294 text as a C<text> part, then one C<image_url> part per image, each a C<data:> URL whose media
295 type is read from the image's magic bytes (PNG, JPEG, GIF, WebP; PNG otherwise, see
296 L<Langertha::Skeid::Protocol/image_media_type>). A message without images keeps its string
297 content.
298  
299 =head2 response_from_openai
300  
301 my $ollama = Langertha::Skeid::Protocol::Ollama->response_from_openai($res);
302  
303 Turns the upstream OpenAI response into an Ollama chat response. Tool calls come from
304 L<Langertha::ToolCall>, including Hermes-style calls recovered from plain text — when they are
305 recovered, the text they were embedded in is stripped from the message content.
306  
307 Token counts are reported under Ollama's names; C<done> is always true because this path never
308 streams.
309  
310 =head2 generate_request_to_openai
311  
312 my $openai_body = Langertha::Skeid::Protocol::Ollama->generate_request_to_openai($body);
313  
314 Turns an Ollama C</api/generate> request into the same OpenAI chat-completions body
315 L</request_to_openai> builds for C</api/chat>, by way of a chat conversation: C<system>, when
316 given, becomes a system message, and C<prompt> with its C<images> becomes one user message --
317 so the images become C<image_url> parts exactly as a chat message's do. C<model>,
318 C<options> and C<format> are read as on C</api/chat>.
319  
320 Everything else a generate request can carry is not forwarded: C<think> and
321 C<options.seed> (not carried on C</api/chat> either), and the fields that only mean something
322 to an Ollama server's own prompt handling -- C<suffix>, C<template>, C<raw>, C<context>,
323 C<keep_alive>.
324  
325 =head2 generate_response_from_openai
326  
327 my $ollama = Langertha::Skeid::Protocol::Ollama->generate_response_from_openai($res);
328  
329 Turns the upstream OpenAI response into an Ollama generate response: the answer text as
330 C<response>, C<done> true, C<done_reason> and the token counts under the same names as
331 L</response_from_openai>. The text is passed as the model wrote it -- generate has no tool
332 calls, so nothing is lifted out of it. Ollama's C<context> (its token ids for the next call)
333 and its timing durations are not reported; Skeid has neither.
334  
335 =head2 tags_from_models
336  
337 my $tags = Langertha::Skeid::Protocol::Ollama->tags_from_models($skeid->list_models(api_key_id => $id));
338  
339 Renders a model list (L<Langertha::Skeid/list_models>: hashes with C<model> and C<engine>) as an
340 Ollama C<< /api/tags >> answer, one entry per name, the same names C</v1/models> lists. C<family>
341 is the entry's C<engine>, C<openaibase> when it has none (an alias).
342  
343 The fields Ollama clients expect but Skeid cannot know — size, digest, parameter size,
344 quantisation — are filled with empty or C<'unknown'> placeholders rather than invented, so a
345 client that displays them shows nothing instead of showing a lie.
346  
347 =head2 manifest_endpoint
348  
349 my $spec = Langertha::Skeid::Protocol::Ollama->manifest_endpoint;
350 # { dialect => 'ollama', path => '', capabilities => [ ... ] }
351  
352 How this face appears in the provider manifest (skeid #29): C<ollama> at the public root, and
353 the capability flags L</request_to_openai> actually carries to the upstream -- messages
354 (a C<system> message included), C<tools>, C<options.temperature>, C<options.num_predict>
355 (response size), C<stream>, a message's C<images> (C<image_input>) and C<format> as
356 C<"json"> or a schema (C<response_format_json_object>, C<response_format_json_schema>). A
357 model is published here only with the capabilities declared for it that are in this list.
358  
359 C</api/generate> needs no entry of its own: the C<ollama> dialect names the whole Ollama API at
360 this root, and the manifest's capabilities describe a chat call, which C</api/chat> is.
361  
362 Not carried, so never claimed: C<options.seed> and C<think>.
363 C<tool_choice> is passed through when a client sends one, but the Ollama dialect has no such
364 field, so no C<tool_choice_*> flag is claimed.
365  
366 =head2 error_body
367  
368 my $body = Langertha::Skeid::Protocol::Ollama->error_body("Model 'x' is not available for this key");
369  
370 Ollama's error envelope, C<< { error => $message } >> -- the message as a plain string, not an
371 object: the Ollama clients decode C<error> as a string (the Go client's C<StatusError>) and fail
372 on anything else. Every error Skeid answers on C</api/*> is rendered from this, with the HTTP
373 status of the failure, and so is the mid-stream error line (see
374 L<Langertha::Skeid::Protocol::Ollama::Stream/error_event>) (skeid #47).
375  
376 =head1 SEE ALSO
377  
378 L<Langertha::Skeid::Protocol::Ollama::Stream>, L<Langertha::Skeid::Protocol>,
379 L<Langertha::Skeid::Proxy>
380  
381 =head1 SUPPORT
382  
383 =head2 Issues
384  
385 Please report bugs and feature requests on GitHub at
386 L<https://github.com/Getty/langertha-skeid/issues>.
387  
388 =head2 IRC
389  
390 Join C<#langertha> on C<irc.perl.org> or message Getty directly.
391  
392 =head1 CONTRIBUTING
393  
394 Contributions are welcome! Please fork the repository and submit a pull request.
395  
396 =head1 AUTHOR
397  
398 Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/>
399  
400 =head1 COPYRIGHT AND LICENSE
401  
402 This software is copyright (c) 2026 by Torsten Raudssus.
403  
404 This is free software; you can redistribute it and/or modify it under
405 the same terms as the Perl 5 programming language system itself.
406  
407 =cut
« blib/lib/Langertha/Skeid/UsageStore/DBI.pm ↑ Summary blib/lib/Langertha/Skeid/CapacityProbe/Registry.pm »