| 1 | package Langertha::Engine::Ollama; | ||||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Ollama API | ||||||||||||||||||||||||||||||||||||||||||||
| 3 | 12 | our $VERSION = '0.502'; | |||||||||||||||||||||||||||||||||||||||||||
| 4 | 12 | ▶ | use Moose; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 5 | 12 | ▶ | use File::ShareDir::ProjectDistDir qw( :all ); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 6 | 12 | ▶ | use Carp qw( croak ); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 7 | 12 | ▶ | use JSON::MaybeXS; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 8 | 12 | ▶ | use Module::Runtime qw( use_module ); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 9 | 12 | ▶ | use Langertha::Response; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 10 | 12 | ▶ | use Langertha::ToolCall; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||||
| 12 | 12 | ▶ | use Langertha::Engine::OllamaOpenAI; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||||
| 14 | 12 | extends 'Langertha::Engine::Remote'; | |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | 12 | with map { 'Langertha::Role::'.$_ } qw( | |||||||||||||||||||||||||||||||||||||||||||
| 17 | OpenAPI | ||||||||||||||||||||||||||||||||||||||||||||
| 18 | Models | ||||||||||||||||||||||||||||||||||||||||||||
| 19 | Seed | ||||||||||||||||||||||||||||||||||||||||||||
| 20 | Temperature | ||||||||||||||||||||||||||||||||||||||||||||
| 21 | ContextSize | ||||||||||||||||||||||||||||||||||||||||||||
| 22 | ResponseSize | ||||||||||||||||||||||||||||||||||||||||||||
| 23 | SystemPrompt | ||||||||||||||||||||||||||||||||||||||||||||
| 24 | KeepAlive | ||||||||||||||||||||||||||||||||||||||||||||
| 25 | Chat | ||||||||||||||||||||||||||||||||||||||||||||
| 26 | Embedding | ||||||||||||||||||||||||||||||||||||||||||||
| 27 | ResponseFormat | ||||||||||||||||||||||||||||||||||||||||||||
| 28 | Streaming | ||||||||||||||||||||||||||||||||||||||||||||
| 29 | Tools | ||||||||||||||||||||||||||||||||||||||||||||
| 30 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||||||||
| 33 | sub openai { | ||||||||||||||||||||||||||||||||||||||||||||
| 34 | 1 | ▶ | my ( $self, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
openai: called CC=5 SCAR=18.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 35 | 1 | ▶ | return Langertha::Engine::OllamaOpenAI->new( | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||||||||||||||||||||||||
| 36 | url => $self->url.'/v1', | ||||||||||||||||||||||||||||||||||||||||||||
| 37 | model => $self->model, | ||||||||||||||||||||||||||||||||||||||||||||
| 38 | $self->embedding_model ? ( embedding_model => $self->embedding_model ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 39 | $self->chat_model ? ( chat_model => $self->chat_model ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 40 | $self->has_system_prompt ? ( system_prompt => $self->system_prompt ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 41 | $self->has_temperature ? ( temperature => $self->temperature ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 42 | %args, | ||||||||||||||||||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | sub new_openai { | ||||||||||||||||||||||||||||||||||||||||||||
| 48 | 0 | ▶ | my ( $class, %args ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new_openai: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 49 | 0 | ▶ | my $tools = delete $args{tools} || []; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
delete $args{'tools'} || []
MC/DC0%0/1
delete $args{'tools'} || []
delete $args{'tools'}
Truth table0%0/2
A
A= delete $args{'tools'}
| |||||||||||||||||||||||||||||||||||||||||||||
| 50 | 0 | my $self = $class->new(%args); | |||||||||||||||||||||||||||||||||||||||||||
| 51 | 0 | return $self->openai( tools => $tools ); | |||||||||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | ▶ | sub default_model { 'llama3.3' } | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | ▶ | sub default_embedding_model { 'mxbai-embed-large' } | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
default_embedding_model: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub openapi_file { yaml => dist_file('Langertha','ollama.yaml') }; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
openapi_file: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | sub _build_openapi_operations { | ||||||||||||||||||||||||||||||||||||||||||||
| 61 | 7 | ▶ | return use_module('Langertha::Spec::Ollama')->data; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_build_openapi_operations: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||||
| 65 | has json_format => ( | ||||||||||||||||||||||||||||||||||||||||||||
| 66 | isa => 'Bool', | ||||||||||||||||||||||||||||||||||||||||||||
| 67 | is => 'ro', | ||||||||||||||||||||||||||||||||||||||||||||
| 68 | 13 | ▶ | default => sub {0}, | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 69 | 12 | ); | |||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | sub embedding_request { | ||||||||||||||||||||||||||||||||||||||||||||
| 73 | 1 | ▶ | my ( $self, $prompt, %extra ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
embedding_request: called CC=1 SCAR=0.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 74 | 0 | ▶ | return $self->generate_request( embed => sub { $self->embedding_response(shift) }, | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 75 | 1 | model => $self->embedding_model, | |||||||||||||||||||||||||||||||||||||||||||
| 76 | input => $prompt, | ||||||||||||||||||||||||||||||||||||||||||||
| 77 | %extra, | ||||||||||||||||||||||||||||||||||||||||||||
| 78 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||
| 81 | sub embedding_response { | ||||||||||||||||||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my ( $self, $response ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
embedding_response: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 83 | 0 | my $data = $self->parse_response($response); | |||||||||||||||||||||||||||||||||||||||||||
| 84 | # New API returns embeddings as array of arrays | ||||||||||||||||||||||||||||||||||||||||||||
| 85 | 0 | return $data->{embeddings}[0]; | |||||||||||||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | sub chat_request { | ||||||||||||||||||||||||||||||||||||||||||||
| 89 | 5 | ▶ | my ( $self, $messages, %extra ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chat_request: called CC=18 SCAR=46.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||
| 91 | # Translate response_format -> Ollama's format parameter. Ollama | ||||||||||||||||||||||||||||||||||||||||||||
| 92 | # accepts either the literal string 'json' or a JSON-Schema HashRef. | ||||||||||||||||||||||||||||||||||||||||||||
| 93 | # Fall back to legacy json_format attribute when response_format is | ||||||||||||||||||||||||||||||||||||||||||||
| 94 | # not set. | ||||||||||||||||||||||||||||||||||||||||||||
| 95 | 5 | my $format; | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 5 | ▶ | if ( $self->has_response_format ) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 0 | my $rf = $self->response_format; | |||||||||||||||||||||||||||||||||||||||||||
| 98 | 0 | ▶ | my $type = ref($rf) eq 'HASH' ? ( $rf->{type} // '' ) : ''; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$$rf{"type"} // ""
MC/DC0%0/1
$$rf{"type"} // ""
$$rf{"type"}
Truth table0%0/2
A
A= $$rf{"type"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 99 | 0 | ▶ | if ( $type eq 'json_object' ) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | $format = 'json'; | |||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 102 | ▶ | elsif ( $type eq 'json_schema' | |||||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/6
$type eq "json_schema" and ref $$rf{"json_schema"} eq "HASH" and ref $$rf{"json_schema"}{"schema"} eq "HASH"
$type eq "json_schema" and ref $$rf{"json_schema"} eq "HASH"
MC/DC0%0/3
$type eq "json_schema" and ref $$rf{"json_schema"} eq "HASH" and ref $$rf{"json_schema"}{"schema"} eq "HASH"
$type eq "json_schema"
ref $$rf{"json_schema"} eq "HASH"
ref $$rf{"json_schema"}{"schema"} eq "HASH"
Truth table0%0/4
A and B and C
A= $type eq "json_schema"B= ref $$rf{"json_schema"} eq "HASH"C= ref $$rf{"json_schema"}{"schema"} eq "HASH"
| |||||||||||||||||||||||||||||||||||||||||||||
| 103 | && ref( $rf->{json_schema} ) eq 'HASH' | ||||||||||||||||||||||||||||||||||||||||||||
| 104 | && ref( $rf->{json_schema}{schema} ) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||||||||||
| 105 | 0 | $format = $rf->{json_schema}{schema}; | |||||||||||||||||||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 108 | 5 | ▶ | $format = 'json' if !defined $format && $self->json_format; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not defined $format and $self->json_format
MC/DC0%0/2
not defined $format and $self->json_format
not defined $format
$self->json_format
Truth table33%1/3
not(A) and B
A= not defined $formatB= $self->json_format
| |||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | return $self->generate_request( chat => sub { $self->chat_response(shift) }, | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||||
| 111 | model => $self->chat_model, | ||||||||||||||||||||||||||||||||||||||||||||
| 112 | messages => $messages, | ||||||||||||||||||||||||||||||||||||||||||||
| 113 | stream => JSON->false, | ||||||||||||||||||||||||||||||||||||||||||||
| 114 | defined $format ? ( format => $format ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 115 | defined $self->get_keep_alive ? ( keep_alive => $self->get_keep_alive ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 116 | options => { | ||||||||||||||||||||||||||||||||||||||||||||
| 117 | $self->has_temperature ? ( temperature => $self->temperature ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 118 | $self->has_context_size ? ( num_ctx => $self->get_context_size ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 119 | $self->get_response_size ? ( num_predict => $self->get_response_size ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 120 | $self->has_seed ? ( seed => $self->seed ) | ||||||||||||||||||||||||||||||||||||||||||||
| 121 | : $self->randomize_seed ? ( seed => $self->random_seed ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 122 | 5 | ▶ | $extra{options} ? (%{delete $extra{options}}) : (), | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||||||||
| 123 | }, | ||||||||||||||||||||||||||||||||||||||||||||
| 124 | %extra, | ||||||||||||||||||||||||||||||||||||||||||||
| 125 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||||
| 128 | sub chat_response { | ||||||||||||||||||||||||||||||||||||||||||||
| 129 | 2 | ▶ | my ( $self, $response ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chat_response: called CC=14 SCAR=26.7
| |||||||||||||||||||||||||||||||||||||||||||||
| 130 | 2 | my $data = $self->parse_response($response); | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 2 | my $msg = $data->{message}; | |||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||||
| 133 | 2 | my $usage = {}; | |||||||||||||||||||||||||||||||||||||||||||
| 134 | 2 | ▶ | $usage->{prompt_tokens} = $data->{prompt_eval_count} if $data->{prompt_eval_count}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 135 | 2 | ▶ | $usage->{completion_tokens} = $data->{eval_count} if $data->{eval_count}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 136 | 2 | ▶ | $usage = undef unless %$usage; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||||
| 138 | 2 | my $timing = {}; | |||||||||||||||||||||||||||||||||||||||||||
| 139 | 2 | for my $k (qw( total_duration load_duration prompt_eval_duration eval_duration )) { | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 8 | ▶ | $timing->{$k} = $data->{$k} if $data->{$k}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 142 | 2 | ▶ | $timing = undef unless %$timing; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||
| 144 | 2 | my @tcs = Langertha::ToolCall->extract($data); | |||||||||||||||||||||||||||||||||||||||||||
| 145 | return Langertha::Response->new( | ||||||||||||||||||||||||||||||||||||||||||||
| 146 | content => $msg->{content} // '', | ||||||||||||||||||||||||||||||||||||||||||||
| 147 | raw => $data, | ||||||||||||||||||||||||||||||||||||||||||||
| 148 | $data->{model} ? ( model => $data->{model} ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 149 | defined $data->{done_reason} ? ( finish_reason => $data->{done_reason} ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 150 | $usage ? ( usage => $usage ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 151 | $timing ? ( timing => $timing ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 152 | 2 | ▶ | $data->{created_at} ? ( created => $data->{created_at} ) : (), | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 6 branches
Condition50%1/2
$$msg{"content"} // ""
MC/DC0%0/1
$$msg{"content"} // ""
$$msg{"content"}
Truth table50%1/2
A
A= $$msg{"content"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 153 | @tcs ? ( tool_calls => [ @tcs ] ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 154 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||
| 157 | 1 | ▶ | sub tags { $_[0]->tags_request } | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tags: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 158 | sub tags_request { | ||||||||||||||||||||||||||||||||||||||||||||
| 159 | 1 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tags_request: called CC=1 SCAR=0.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 160 | 1 | ▶ | return $self->generate_request( list => sub { $self->tags_response(shift) } ); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||||
| 164 | sub tags_response { | ||||||||||||||||||||||||||||||||||||||||||||
| 165 | 0 | ▶ | my ( $self, $response ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
tags_response: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | my $data = $self->parse_response($response); | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | my @model_list = map { $_->{model} } @{$data->{models}}; | |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | $self->models(\@model_list); | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 0 | return $data->{models}; | |||||||||||||||||||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||||||
| 172 | sub simple_tags { | ||||||||||||||||||||||||||||||||||||||||||||
| 173 | 0 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
simple_tags: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | my $request = $self->tags; | |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | my $response = $self->user_agent->request($request); | |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | return $request->response_call->($response); | |||||||||||||||||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||
| 180 | 1 | ▶ | sub ps { $_[0]->ps_request } | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ps: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 181 | sub ps_request { | ||||||||||||||||||||||||||||||||||||||||||||
| 182 | 1 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ps_request: called CC=1 SCAR=0.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 183 | 1 | ▶ | return $self->generate_request( ps => sub { $self->ps_response(shift) } ); | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||||||||||||||||||
| 187 | sub ps_response { | ||||||||||||||||||||||||||||||||||||||||||||
| 188 | 0 | ▶ | my ( $self, $response ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
ps_response: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 189 | 0 | my $data = $self->parse_response($response); | |||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | return $data->{models}; | |||||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||||||
| 193 | sub simple_ps { | ||||||||||||||||||||||||||||||||||||||||||||
| 194 | 0 | ▶ | my ( $self ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
simple_ps: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | my $request = $self->ps; | |||||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | my $response = $self->user_agent->request($request); | |||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | return $request->response_call->($response); | |||||||||||||||||||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||||||||||||||||||
| 201 | # Dynamic model listing (wrapper around simple_tags with caching) | ||||||||||||||||||||||||||||||||||||||||||||
| 202 | sub list_models { | ||||||||||||||||||||||||||||||||||||||||||||
| 203 | 0 | ▶ | my ($self, %opts) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
list_models: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||||||||||||||||||
| 205 | # Check cache unless force_refresh requested | ||||||||||||||||||||||||||||||||||||||||||||
| 206 | 0 | ▶ | unless ($opts{force_refresh}) { | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 207 | 0 | my $cache = $self->_models_cache; | |||||||||||||||||||||||||||||||||||||||||||
| 208 | 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
| |||||||||||||||||||||||||||||||||||||||||||||
| 209 | 0 | ▶ | return $opts{full} ? $cache->{models} : $cache->{model_ids}; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||||||||||||||||||
| 213 | # Fetch from API via simple_tags | ||||||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | my $models = $self->simple_tags; | |||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||
| 216 | # Extract IDs and update cache | ||||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | my @model_ids = map { $_->{model} } @$models; | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | $self->_models_cache({ | |||||||||||||||||||||||||||||||||||||||||||
| 219 | timestamp => time, | ||||||||||||||||||||||||||||||||||||||||||||
| 220 | models => $models, | ||||||||||||||||||||||||||||||||||||||||||||
| 221 | model_ids => \@model_ids, | ||||||||||||||||||||||||||||||||||||||||||||
| 222 | }); | ||||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | return $opts{full} ? $models : \@model_ids; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||||
| 228 | 2 | ▶ | sub stream_format { 'ndjson' } | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
stream_format: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||||
| 230 | sub chat_stream_request { | ||||||||||||||||||||||||||||||||||||||||||||
| 231 | 1 | ▶ | my ( $self, $messages, %extra ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
chat_stream_request: called CC=9 SCAR=28.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 232 | ▶ | return $self->generate_request( chat => sub {}, | |||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||
| 233 | model => $self->chat_model, | ||||||||||||||||||||||||||||||||||||||||||||
| 234 | messages => $messages, | ||||||||||||||||||||||||||||||||||||||||||||
| 235 | stream => JSON->true, | ||||||||||||||||||||||||||||||||||||||||||||
| 236 | $self->json_format ? ( format => 'json' ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 237 | defined $self->get_keep_alive ? ( keep_alive => $self->get_keep_alive ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 238 | options => { | ||||||||||||||||||||||||||||||||||||||||||||
| 239 | $self->has_temperature ? ( temperature => $self->temperature ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 240 | $self->has_context_size ? ( num_ctx => $self->get_context_size ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 241 | $self->get_response_size ? ( num_predict => $self->get_response_size ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 242 | $self->has_seed ? ( seed => $self->seed ) | ||||||||||||||||||||||||||||||||||||||||||||
| 243 | : $self->randomize_seed ? ( seed => $self->random_seed ) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 244 | 1 | ▶ | $extra{options} ? (%{delete $extra{options}}) : (), | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 8 branches
| |||||||||||||||||||||||||||||||||||||||||||||
| 245 | }, | ||||||||||||||||||||||||||||||||||||||||||||
| 246 | %extra, | ||||||||||||||||||||||||||||||||||||||||||||
| 247 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 248 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||||||||||||||||||
| 250 | sub parse_stream_chunk { | ||||||||||||||||||||||||||||||||||||||||||||
| 251 | 3 | ▶ | my ( $self, $data ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_stream_chunk: called CC=9 SCAR=23.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||||
| 253 | 3 | ▶ | my $content = $data->{message}{content} // ''; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$data{"message"}{"content"} // ""
MC/DC0%0/1
$$data{"message"}{"content"} // ""
$$data{"message"}{"content"}
Truth table50%1/2
A
A= $$data{"message"}{"content"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 254 | 3 | ▶ | my $is_done = $data->{done} ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||||
| 256 | 3 | require Langertha::Stream::Chunk; | |||||||||||||||||||||||||||||||||||||||||||
| 257 | return Langertha::Stream::Chunk->new( | ||||||||||||||||||||||||||||||||||||||||||||
| 258 | content => $content, | ||||||||||||||||||||||||||||||||||||||||||||
| 259 | raw => $data, | ||||||||||||||||||||||||||||||||||||||||||||
| 260 | is_final => $is_done, | ||||||||||||||||||||||||||||||||||||||||||||
| 261 | $data->{model} ? (model => $data->{model}) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 262 | $is_done && $data->{done_reason} ? (finish_reason => $data->{done_reason}) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 263 | $is_done ? (usage => { | ||||||||||||||||||||||||||||||||||||||||||||
| 264 | $data->{eval_count} ? (completion_tokens => $data->{eval_count}) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 265 | 3 | ▶ | $data->{prompt_eval_count} ? (prompt_tokens => $data->{prompt_eval_count}) : (), | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 5 branches
Condition66%2/3
$is_done and $$data{"done_reason"}
MC/DC50%1/2
$is_done and $$data{"done_reason"}
$is_done
$$data{"done_reason"}
Truth table66%2/3
A and B
A= $is_doneB= $$data{"done_reason"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 266 | }) : (), | ||||||||||||||||||||||||||||||||||||||||||||
| 267 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 268 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||||||||||
| 270 | # Tool calling support (MCP) | ||||||||||||||||||||||||||||||||||||||||||||
| 271 | # Ollama uses the same tool format as OpenAI | ||||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||||
| 273 | sub format_tools { | ||||||||||||||||||||||||||||||||||||||||||||
| 274 | 2 | ▶ | my ( $self, $mcp_tools ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_tools: called CC=3 SCAR=13.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 275 | return [map { | ||||||||||||||||||||||||||||||||||||||||||||
| 276 | 2 | { | |||||||||||||||||||||||||||||||||||||||||||
| 277 | type => 'function', | ||||||||||||||||||||||||||||||||||||||||||||
| 278 | function => { | ||||||||||||||||||||||||||||||||||||||||||||
| 279 | name => $_->{name}, | ||||||||||||||||||||||||||||||||||||||||||||
| 280 | description => $_->{description}, | ||||||||||||||||||||||||||||||||||||||||||||
| 281 | parameters => $_->{input_schema} // $_->{inputSchema} // $_->{parameters}, | ||||||||||||||||||||||||||||||||||||||||||||
| 282 | }, | ||||||||||||||||||||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 284 | 2 | ▶ | } @$mcp_tools]; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition33%2/6
($$_{"input_schema"} // $$_{"inputSchema"}) // $$_{"parameters"}
$$_{"input_schema"} // $$_{"inputSchema"}
MC/DC0%0/3
($$_{"input_schema"} // $$_{"inputSchema"}) // $$_{"parameters"}
$$_{"input_schema"}
$$_{"inputSchema"}
$$_{"parameters"}
Truth table25%1/4
A // B // C
A= $$_{"input_schema"}B= $$_{"inputSchema"}C= $$_{"parameters"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 285 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||||||||||||||||||
| 287 | sub response_tool_calls { | ||||||||||||||||||||||||||||||||||||||||||||
| 288 | 4 | ▶ | my ( $self, $data ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
response_tool_calls: called CC=3 SCAR=11.1
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 289 | 4 | ▶ | my $msg = $data->{message} or return []; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 290 | 4 | ▶ | return $msg->{tool_calls} // []; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$$msg{"tool_calls"} // []
MC/DC100%1/1
$$msg{"tool_calls"} // []
$$msg{"tool_calls"}
Truth table100%2/2
A
A= $$msg{"tool_calls"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||||||||||||||||||
| 293 | sub extract_tool_call { | ||||||||||||||||||||||||||||||||||||||||||||
| 294 | 1 | ▶ | my ( $self, $tc ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
extract_tool_call: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 295 | 1 | return ( $tc->{function}{name}, $tc->{function}{arguments} ); | |||||||||||||||||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||||||||||||||||||
| 298 | sub response_text_content { | ||||||||||||||||||||||||||||||||||||||||||||
| 299 | 3 | ▶ | my ( $self, $data ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
response_text_content: called CC=3 SCAR=11.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 300 | 3 | ▶ | my $msg = $data->{message} or return ''; | ||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||
| 301 | 3 | ▶ | return $msg->{content} // ''; | ||||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$$msg{"content"} // ""
MC/DC0%0/1
$$msg{"content"} // ""
$$msg{"content"}
Truth table50%1/2
A
A= $$msg{"content"}
| |||||||||||||||||||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||||||||||||||||||
| 304 | sub format_tool_results { | ||||||||||||||||||||||||||||||||||||||||||||
| 305 | 3 | ▶ | my ( $self, $data, $results ) = @_; | ||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
format_tool_results: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||||||||||
| 306 | return ( | ||||||||||||||||||||||||||||||||||||||||||||
| 307 | { role => 'assistant', content => $data->{message}{content}, | ||||||||||||||||||||||||||||||||||||||||||||
| 308 | tool_calls => $data->{message}{tool_calls} }, | ||||||||||||||||||||||||||||||||||||||||||||
| 309 | map { | ||||||||||||||||||||||||||||||||||||||||||||
| 310 | 3 | my $r = $_; | |||||||||||||||||||||||||||||||||||||||||||
| 311 | { | ||||||||||||||||||||||||||||||||||||||||||||
| 312 | role => 'tool', | ||||||||||||||||||||||||||||||||||||||||||||
| 313 | 4 | content => $self->json->encode($r->{result}{content}), | |||||||||||||||||||||||||||||||||||||||||||
| 314 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 315 | } @$results | ||||||||||||||||||||||||||||||||||||||||||||
| 316 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||||||||||||||||||
| 318 | |||||||||||||||||||||||||||||||||||||||||||||
| 319 | 12 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||||
| 322 | 12 | 1; | |||||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||||
| 324 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||||||||||||||||||
| 326 | =pod | ||||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||||
| 328 | =encoding UTF-8 | ||||||||||||||||||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||||||||||||||||||
| 330 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||||||
| 332 | Langertha::Engine::Ollama - Ollama API | ||||||||||||||||||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||||||||||||||||||
| 334 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||||
| 336 | version 0.502 | ||||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||||
| 338 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||||
| 340 | use Langertha::Engine::Ollama; | ||||||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||||||
| 342 | my $ollama = Langertha::Engine::Ollama->new( | ||||||||||||||||||||||||||||||||||||||||||||
| 343 | url => $ENV{OLLAMA_URL}, | ||||||||||||||||||||||||||||||||||||||||||||
| 344 | model => 'llama3.3', | ||||||||||||||||||||||||||||||||||||||||||||
| 345 | system_prompt => 'You are a helpful assistant', | ||||||||||||||||||||||||||||||||||||||||||||
| 346 | context_size => 2048, | ||||||||||||||||||||||||||||||||||||||||||||
| 347 | temperature => 0.5, | ||||||||||||||||||||||||||||||||||||||||||||
| 348 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||||||||||
| 350 | print $ollama->simple_chat('Say something nice'); | ||||||||||||||||||||||||||||||||||||||||||||
| 351 | |||||||||||||||||||||||||||||||||||||||||||||
| 352 | my $embedding = $ollama->embedding($content); | ||||||||||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||||||||||
| 354 | # Get OpenAI-compatible API access to Ollama | ||||||||||||||||||||||||||||||||||||||||||||
| 355 | my $ollama_openai = $ollama->openai; | ||||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||||
| 357 | # List available models | ||||||||||||||||||||||||||||||||||||||||||||
| 358 | my $models = $ollama->simple_tags; | ||||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||||
| 360 | # Show running models | ||||||||||||||||||||||||||||||||||||||||||||
| 361 | my $running = $ollama->simple_ps; | ||||||||||||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||||||||||
| 363 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||||
| 365 | Provides access to Ollama, which runs large language models locally. Ollama | ||||||||||||||||||||||||||||||||||||||||||||
| 366 | supports many popular open-source models including C<llama3.3> (default), | ||||||||||||||||||||||||||||||||||||||||||||
| 367 | C<qwen2.5>, C<deepseek-coder-v2>, C<mixtral>, and C<mxbai-embed-large> | ||||||||||||||||||||||||||||||||||||||||||||
| 368 | (default embedding model). | ||||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||||
| 370 | Supports chat, embeddings, streaming, MCP tool calling (OpenAI-compatible | ||||||||||||||||||||||||||||||||||||||||||||
| 371 | format), and an OpenAI-compatible API via L</openai>. Not all models support | ||||||||||||||||||||||||||||||||||||||||||||
| 372 | tool calling; known working models include C<qwen3:8b> and C<llama3.2:3b>. | ||||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||||
| 374 | For Hermes-format tool calling in models without API-level tool support, | ||||||||||||||||||||||||||||||||||||||||||||
| 375 | compose L<Langertha::Role::HermesTools>. See L<Langertha::Role::HermesTools> | ||||||||||||||||||||||||||||||||||||||||||||
| 376 | for details. | ||||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||||
| 378 | B<THIS API IS WORK IN PROGRESS> | ||||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||||
| 380 | =head2 openai | ||||||||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||||||
| 382 | my $oai = $ollama->openai; | ||||||||||||||||||||||||||||||||||||||||||||
| 383 | my $oai = $ollama->openai(model => 'different_model'); | ||||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||||
| 385 | Returns a L<Langertha::Engine::OllamaOpenAI> instance configured for Ollama's | ||||||||||||||||||||||||||||||||||||||||||||
| 386 | C</v1> OpenAI-compatible endpoint, inheriting the current model, embedding | ||||||||||||||||||||||||||||||||||||||||||||
| 387 | model, system prompt, and temperature settings. Supports streaming, embeddings, | ||||||||||||||||||||||||||||||||||||||||||||
| 388 | and MCP tool calling. | ||||||||||||||||||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||||||||||||||||||
| 390 | =head2 new_openai | ||||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||||
| 392 | my $oai = Langertha::Engine::Ollama->new_openai( | ||||||||||||||||||||||||||||||||||||||||||||
| 393 | url => 'http://localhost:11434', | ||||||||||||||||||||||||||||||||||||||||||||
| 394 | model => 'llama3.3', | ||||||||||||||||||||||||||||||||||||||||||||
| 395 | tools => \@mcp_tools, | ||||||||||||||||||||||||||||||||||||||||||||
| 396 | ); | ||||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||||
| 398 | Class method. Constructs a native Ollama engine and immediately returns an | ||||||||||||||||||||||||||||||||||||||||||||
| 399 | L<Langertha::Engine::OllamaOpenAI> instance from its C<openai()> method. | ||||||||||||||||||||||||||||||||||||||||||||
| 400 | The optional C<tools> list is passed to C<openai()>. | ||||||||||||||||||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||||||||||||||||||
| 402 | =head2 json_format | ||||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||||
| 404 | When set to a true value, passes C<format => 'json'> to the Ollama API, | ||||||||||||||||||||||||||||||||||||||||||||
| 405 | requesting JSON-formatted output from the model. Defaults to C<0>. | ||||||||||||||||||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||||||||||||||||||
| 407 | =head2 tags | ||||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||||
| 409 | my $request = $ollama->tags; | ||||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||||
| 411 | Returns an HTTP request object for the Ollama C<GET /api/tags> endpoint. | ||||||||||||||||||||||||||||||||||||||||||||
| 412 | Execute it with C<simple_tags> or pass it to an async HTTP client. | ||||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||||
| 414 | =head2 simple_tags | ||||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||||
| 416 | my $models = $ollama->simple_tags; | ||||||||||||||||||||||||||||||||||||||||||||
| 417 | # Returns: [{name => 'llama3.3', model => 'llama3.3', ...}, ...] | ||||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||||
| 419 | Synchronously fetches and returns the list of locally available models from | ||||||||||||||||||||||||||||||||||||||||||||
| 420 | the Ollama C</api/tags> endpoint. Also updates the engine's C<models> list. | ||||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||||
| 422 | =head2 ps | ||||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||||
| 424 | my $request = $ollama->ps; | ||||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||||
| 426 | Returns an HTTP request object for the Ollama C<GET /api/ps> endpoint which | ||||||||||||||||||||||||||||||||||||||||||||
| 427 | lists currently loaded (running) models. | ||||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||||
| 429 | =head2 simple_ps | ||||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||||
| 431 | my $running = $ollama->simple_ps; | ||||||||||||||||||||||||||||||||||||||||||||
| 432 | # Returns: [{name => 'llama3.3', ...}, ...] | ||||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||||
| 434 | Synchronously fetches and returns the list of models currently loaded in | ||||||||||||||||||||||||||||||||||||||||||||
| 435 | Ollama's memory from the C</api/ps> endpoint. | ||||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||||
| 437 | =head2 list_models | ||||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||||
| 439 | my $model_ids = $ollama->list_models; | ||||||||||||||||||||||||||||||||||||||||||||
| 440 | my $models = $ollama->list_models(full => 1); | ||||||||||||||||||||||||||||||||||||||||||||
| 441 | my $models = $ollama->list_models(force_refresh => 1); | ||||||||||||||||||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||||||||||||||||||
| 443 | Fetches locally available models from Ollama via L</simple_tags> with caching. | ||||||||||||||||||||||||||||||||||||||||||||
| 444 | Returns an ArrayRef of model name strings by default, or full model objects | ||||||||||||||||||||||||||||||||||||||||||||
| 445 | when C<full => 1> is passed. Results are cached for C<models_cache_ttl> | ||||||||||||||||||||||||||||||||||||||||||||
| 446 | seconds (default: 3600). | ||||||||||||||||||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||||||||||||||||||
| 448 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||||
| 450 | =over | ||||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||||
| 452 | =item * L<https://ollama.com/library> - Ollama model library | ||||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||||
| 454 | =item * L<https://github.com/ollama/ollama> - Ollama project | ||||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||||
| 456 | =item * L<Langertha::Engine::OllamaOpenAI> - OpenAI-compatible Ollama access via L</openai> | ||||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||||
| 458 | =item * L<Langertha::Role::Tools> - MCP tool calling interface | ||||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||||
| 460 | =item * L<Langertha::Role::Seed> - Seed for reproducible outputs (composed by this engine) | ||||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||||
| 462 | =item * L<Langertha::Role::ContextSize> - Context window size (composed by this engine) | ||||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||||
| 464 | =back | ||||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||||
| 466 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||||
| 468 | =head2 Issues | ||||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||||
| 470 | Please report bugs and feature requests on GitHub at | ||||||||||||||||||||||||||||||||||||||||||||
| 471 | L<https://github.com/Getty/langertha/issues>. | ||||||||||||||||||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||||||||||||||||||
| 473 | =head2 IRC | ||||||||||||||||||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||||||||||||||||||
| 475 | Join C<#langertha> on C<irc.perl.org> or message Getty directly. | ||||||||||||||||||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||||||||||||||||||
| 477 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||||||||||||||||||
| 478 | |||||||||||||||||||||||||||||||||||||||||||||
| 479 | Contributions are welcome! Please fork the repository and submit a pull request. | ||||||||||||||||||||||||||||||||||||||||||||
| 480 | |||||||||||||||||||||||||||||||||||||||||||||
| 481 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||||
| 483 | Torsten Raudssus <getty@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||||||||||||||||||
| 485 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||||||||||||||||||
| 487 | This software is copyright (c) 2026 by Torsten Raudssus L<https://raudssus.de/>. | ||||||||||||||||||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||||||||||||||||||
| 489 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||||||||||||||||||
| 490 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||||
| 492 | =cut | ||||||||||||||||||||||||||||||||||||||||||||