| 1 | package Langertha::Knarr::Protocol::Ollama; | ||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Ollama-compatible wire protocol (/api/chat, /api/tags) for Knarr | ||||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||
| 4 | 12 | our $VERSION = '1.100'; | |||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use Time::HiRes qw( time ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use POSIX qw( strftime ); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use Langertha::Knarr::Request; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use Langertha::Knarr::Response; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | 12 | with 'Langertha::Knarr::Protocol'; | |||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | # --- Streaming model --- | ||||||||||||||||||||||||||||||||||
| 15 | # Ollama streams via newline-delimited JSON (NDJSON), NOT SSE. | ||||||||||||||||||||||||||||||||||
| 16 | # Each chunk: { model, created_at, message:{role,content}, done:false } | ||||||||||||||||||||||||||||||||||
| 17 | # Final: { model, created_at, message:{role,content:""}, done:true, | ||||||||||||||||||||||||||||||||||
| 18 | # total_duration, eval_count, ... } | ||||||||||||||||||||||||||||||||||
| 19 | # Content-Type stays application/x-ndjson (or application/json with chunked). | ||||||||||||||||||||||||||||||||||
| 20 | # ---------------------- | ||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||
| 22 | 16 | ▶ | has _json => ( is => 'ro', default => sub { JSON::MaybeXS->new( utf8 => 1, canonical => 1 ) } ); | ||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||
| 24 | 1 | ▶ | sub protocol_name { 'ollama' } | ||||||||||||||||||||||||||||||||
|
Subroutine
protocol_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||
| 26 | sub protocol_routes { | ||||||||||||||||||||||||||||||||||
| 27 | return [ | ||||||||||||||||||||||||||||||||||
| 28 | 14 | ▶ | { method => 'POST', path => '/api/chat', action => 'chat' }, | ||||||||||||||||||||||||||||||||
|
Subroutine
protocol_routes: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 29 | { method => 'POST', path => '/api/generate', action => 'chat' }, | ||||||||||||||||||||||||||||||||||
| 30 | { method => 'GET', path => '/api/tags', action => 'models' }, | ||||||||||||||||||||||||||||||||||
| 31 | { method => 'GET', path => '/api/version', action => 'version' }, | ||||||||||||||||||||||||||||||||||
| 32 | ]; | ||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||
| 35 | 23 | ▶ | sub _ts { strftime( "%Y-%m-%dT%H:%M:%S.000000000Z", gmtime ) } | ||||||||||||||||||||||||||||||||
|
Subroutine
_ts: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||
| 37 | sub parse_chat_request { | ||||||||||||||||||||||||||||||||||
| 38 | 7 | ▶ | my ($self, $http_req, $body_ref) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
parse_chat_request: called CC=6 SCAR=19.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 39 | 7 | ▶ | my $data = $self->_json->decode( $$body_ref || '{}' ); | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$body_ref || '{}'
MC/DC0%0/1
$$body_ref || '{}'
$$body_ref
Truth table50%1/2
A
A= $$body_ref
| |||||||||||||||||||||||||||||||||||
| 40 | 7 | my @msgs; | |||||||||||||||||||||||||||||||||
| 41 | 7 | ▶ | if ( $data->{messages} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 42 | 7 | @msgs = @{ $data->{messages} }; | |||||||||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||||||||
| 44 | elsif ( defined $data->{prompt} ) { | ||||||||||||||||||||||||||||||||||
| 45 | 0 | @msgs = ( { role => 'user', content => $data->{prompt} } ); | |||||||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||||||||
| 47 | return Langertha::Knarr::Request->new( | ||||||||||||||||||||||||||||||||||
| 48 | protocol => 'ollama', | ||||||||||||||||||||||||||||||||||
| 49 | raw => $data, | ||||||||||||||||||||||||||||||||||
| 50 | model => $data->{model}, | ||||||||||||||||||||||||||||||||||
| 51 | messages => \@msgs, | ||||||||||||||||||||||||||||||||||
| 52 | stream => exists $data->{stream} ? ( $data->{stream} ? 1 : 0 ) : 1, # Ollama defaults to stream | ||||||||||||||||||||||||||||||||||
| 53 | temperature => $data->{options}{temperature}, | ||||||||||||||||||||||||||||||||||
| 54 | tools => $data->{tools}, | ||||||||||||||||||||||||||||||||||
| 55 | response_format => $data->{format}, | ||||||||||||||||||||||||||||||||||
| 56 | 7 | ▶ | ); | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||||||||
| 59 | sub format_chat_response { | ||||||||||||||||||||||||||||||||||
| 60 | 4 | ▶ | my ($self, $response, $request) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
format_chat_response: called CC=8 SCAR=23.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 61 | 4 | my $r = Langertha::Knarr::Response->coerce($response); | |||||||||||||||||||||||||||||||||
| 62 | 4 | my $message = { role => 'assistant', content => $r->content }; | |||||||||||||||||||||||||||||||||
| 63 | 4 | ▶ | $message->{tool_calls} = [ map { $_->to_ollama } @{ $r->tool_calls } ] | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 64 | if $r->has_tool_calls; | ||||||||||||||||||||||||||||||||||
| 65 | 4 | ▶ | my $payload = { | ||||||||||||||||||||||||||||||||
|
Condition57%4/7
($r->model // $request->model) // "unknown"
$r->model // $request->model
$r->finish_reason // "stop"
MC/DC0%0/2
($r->model // $request->model) // "unknown"
$r->model
$request->model
MC/DC100%1/1
$r->finish_reason // "stop"
$r->finish_reason
Truth table33%1/3
A // B
A= $r->modelB= $request->model
Truth table100%2/2
A
A= $r->finish_reason
| |||||||||||||||||||||||||||||||||||
| 66 | model => $r->model // $request->model // 'unknown', | ||||||||||||||||||||||||||||||||||
| 67 | created_at => _ts(), | ||||||||||||||||||||||||||||||||||
| 68 | message => $message, | ||||||||||||||||||||||||||||||||||
| 69 | done => JSON::MaybeXS::true(), | ||||||||||||||||||||||||||||||||||
| 70 | done_reason => $r->finish_reason // 'stop', | ||||||||||||||||||||||||||||||||||
| 71 | }; | ||||||||||||||||||||||||||||||||||
| 72 | 4 | ▶ | if ( $r->usage && $r->usage->can('to_ollama_format') ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$r->usage and $r->usage->can("to_ollama_format")
MC/DC0%0/2
$r->usage and $r->usage->can("to_ollama_format")
$r->usage
$r->usage->can("to_ollama_format")
Truth table33%1/3
A and B
A= $r->usageB= $r->usage->can("to_ollama_format")
| |||||||||||||||||||||||||||||||||||
| 73 | 0 | my $u = $r->usage->to_ollama_format; | |||||||||||||||||||||||||||||||||
| 74 | 0 | $payload->{$_} = $u->{$_} for keys %$u; | |||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||
| 76 | 4 | return ( 200, { 'Content-Type' => 'application/json' }, $self->_json->encode($payload) ); | |||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||
| 79 | sub format_models_response { | ||||||||||||||||||||||||||||||||||
| 80 | 0 | ▶ | my ($self, $models) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
format_models_response: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 81 | my @data = map { | ||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $id = ref $_ eq 'HASH' ? $_->{id} : "$_"; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 83 | 0 | { name => $id, model => $id, modified_at => _ts(), size => 0 } | |||||||||||||||||||||||||||||||||
| 84 | } @$models; | ||||||||||||||||||||||||||||||||||
| 85 | 0 | return ( 200, { 'Content-Type' => 'application/json' }, | |||||||||||||||||||||||||||||||||
| 86 | $self->_json->encode({ models => \@data }) ); | ||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||
| 89 | sub format_stream_chunk { | ||||||||||||||||||||||||||||||||||
| 90 | 14 | ▶ | my ($self, $delta_text, $request) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
format_stream_chunk: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 91 | 14 | ▶ | my $payload = { | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$request->model // "unknown"
MC/DC0%0/1
$request->model // "unknown"
$request->model
Truth table50%1/2
A
A= $request->model
| |||||||||||||||||||||||||||||||||||
| 92 | model => $request->model // 'unknown', | ||||||||||||||||||||||||||||||||||
| 93 | created_at => _ts(), | ||||||||||||||||||||||||||||||||||
| 94 | message => { role => 'assistant', content => $delta_text }, | ||||||||||||||||||||||||||||||||||
| 95 | done => JSON::MaybeXS::false(), | ||||||||||||||||||||||||||||||||||
| 96 | }; | ||||||||||||||||||||||||||||||||||
| 97 | 14 | return $self->_json->encode($payload) . "\n"; | |||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||
| 100 | 6 | ▶ | sub stream_content_type { 'application/x-ndjson' } | ||||||||||||||||||||||||||||||||
|
Subroutine
stream_content_type: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||
| 102 | sub format_stream_done { | ||||||||||||||||||||||||||||||||||
| 103 | 5 | ▶ | my ($self, $request) = @_; | ||||||||||||||||||||||||||||||||
|
Subroutine
format_stream_done: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 104 | 5 | ▶ | my $payload = { | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$request->model // "unknown"
MC/DC0%0/1
$request->model // "unknown"
$request->model
Truth table50%1/2
A
A= $request->model
| |||||||||||||||||||||||||||||||||||
| 105 | model => $request->model // 'unknown', | ||||||||||||||||||||||||||||||||||
| 106 | created_at => _ts(), | ||||||||||||||||||||||||||||||||||
| 107 | message => { role => 'assistant', content => '' }, | ||||||||||||||||||||||||||||||||||
| 108 | done => JSON::MaybeXS::true(), | ||||||||||||||||||||||||||||||||||
| 109 | done_reason => 'stop', | ||||||||||||||||||||||||||||||||||
| 110 | }; | ||||||||||||||||||||||||||||||||||
| 111 | 5 | return $self->_json->encode($payload) . "\n"; | |||||||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | 12 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||
| 115 | 12 | 1; | |||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||
| 117 | __END__ | ||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||
| 119 | =pod | ||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||
| 121 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||
| 123 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | Langertha::Knarr::Protocol::Ollama - Ollama-compatible wire protocol (/api/chat, /api/tags) for Knarr | ||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||
| 127 | =head1 VERSION | ||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||
| 129 | version 1.100 | ||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||
| 131 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||
| 133 | Implements the Ollama wire format on top of | ||||||||||||||||||||||||||||||||||
| 134 | L<Langertha::Knarr::Protocol>. Loaded by default. | ||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||
| 136 | =over | ||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||
| 138 | =item * C<POST /api/chat>, C<POST /api/generate> — chat with NDJSON streaming | ||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||
| 140 | =item * C<GET /api/tags> — model listing | ||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||
| 142 | =item * C<GET /api/version> — version probe | ||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||
| 144 | =back | ||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||
| 146 | Streaming uses newline-delimited JSON (NDJSON) rather than SSE — the | ||||||||||||||||||||||||||||||||||
| 147 | C<Content-Type> is C<application/x-ndjson> and each chunk is a single | ||||||||||||||||||||||||||||||||||
| 148 | JSON object per line. The final chunk has C<done: true>. | ||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||
| 150 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||
| 152 | =head2 Issues | ||||||||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||||||||
| 154 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||
| 155 | L<https://github.com/Getty/langertha-knarr/issues>. | ||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||
| 157 | =head2 IRC | ||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||
| 159 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||
| 161 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||
| 163 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||
| 165 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | Torsten Raudssus <torsten@raudssus.de> L<https://raudssus.de/> | ||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||
| 169 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||
| 171 | This software is copyright (c) 2026 by Torsten Raudssus. | ||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||
| 173 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||
| 174 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||||||||||||||||