Agent/LLM/Ollama.pm

filter: Statementstmt 25.9% 7 / 27 Branchbran 0.0% 0 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 66.6% 2 / 3 Podpod 0.0% 0 / 1 totaltotal 19.1% 9 / 47 CC 9 SCAR 43.0
CC
9
Cov
16%
call_model
90.0
BEGIN
1.0
BEGIN
1.0
CRAP 73.8
« Agent/Runner.pm ↑ Summary Agent/Engine.pm »
1 package Sim::Agent::LLM::Ollama;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 sub call_model
7 0 ▶{
Subroutine call_model: not called CC=9 SCAR=45.0
Pod undocumented
8  
9 0 my (%args) = @_;
10  
11 0 ▶ my $model = $args{model} or die "model required";
Branch: 1 branch
BranchTrueFalseTotal
my $model = $args{"model"} or die "model required" 0 0 0
12 0 ▶ my $prompt = $args{prompt} or die "prompt required";
Branch: 1 branch
BranchTrueFalseTotal
my $prompt = $args{"prompt"} or die "prompt required" 0 0 0
13 0 ▶ my $agent_dir = $args{agent_dir} or die "agent_dir required";
Branch: 1 branch
BranchTrueFalseTotal
my $agent_dir = $args{"agent_dir"} or die "agent_dir required" 0 0 0
14 0 ▶ my $call_id = $args{call_id} or die "call_id required";
Branch: 1 branch
BranchTrueFalseTotal
my $call_id = $args{"call_id"} or die "call_id required" 0 0 0
15  
16 # SECURITY: validate model
17 0 ▶ die "Invalid model name"
Branch: 1 branch
BranchTrueFalseTotal
unless $model =~ /^[\w\.\:\-]+$/ 0 0 0
18 unless $model =~ /^[\w\.\:\-]+$/;
19  
20 0 my $prompt_file = sprintf("%s/%03d_prompt.txt", $agent_dir, $call_id);
21 0 my $output_file = sprintf("%s/%03d_output.txt", $agent_dir, $call_id);
22  
23 0 ▶ open my $pfh, '>', $prompt_file
Branch: 1 branch
BranchTrueFalseTotal
open my $pfh, ">", $prompt_file or die "Cannot write $prompt_file" 0 0 0
24 or die "Cannot write $prompt_file";
25 0 print {$pfh} $prompt;
26 0 close $pfh;
27  
28 0 my $cmd = "ollama run $model < $prompt_file > $output_file";
29  
30 0 my $exit = system($cmd);
31 0 ▶ die "Ollama call failed (exit=$exit)" if $exit != 0;
Branch: 1 branch
BranchTrueFalseTotal
if $exit != 0 0 0 0
32  
33 0 ▶ open my $ofh, '<', $output_file
Branch: 1 branch
BranchTrueFalseTotal
open my $ofh, "<", $output_file or die "Cannot read $output_file" 0 0 0
34 or die "Cannot read $output_file";
35 0 local $/;
36 0 my $output = <$ofh>;
37 0 close $ofh;
38  
39 0 return $output;
40 }
41  
42 1 1;
43  
44 =pod
45  
46 =head1 NAME
47  
48 Sim::Agent::LLM::Ollama - LLM adapter using the 'ollama' CLI
49  
50 =head1 DESCRIPTION
51  
52 Invokes C<ollama run MODEL> using prompt/output text files stored under the run directory.
53  
54 This adapter is intentionally minimal and avoids SDK dependencies.
55  
56 See L<Sim::Agent>.
57  
58 =head1 AUTHOR
59  
60 Gian Luca Brunetti (2026), gianluca.brunetti@gmail.com
61  
62 =head1 LICENSE
63  
64 The GNU General Public License v3.0
65  
66 =cut
67  
« Agent/Runner.pm ↑ Summary Agent/Engine.pm »