File Coverage

blib/lib/AI/Ollama/Client.pm
Criterion Covered Total %
statement 11 11 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 15 15 100.0


line stmt bran cond sub pod time code
1             package AI::Ollama::Client 0.05;
2 1     1   794 use 5.020;
  1         4  
3 1     1   776 use Moo 2;
  1         10503  
  1         6  
4 1     1   2675 use experimental 'signatures';
  1         2082  
  1         7  
5 1     1   906 use MIME::Base64 'encode_base64';
  1         1100  
  1         725  
6              
7             extends 'AI::Ollama::Client::Impl';
8              
9             =head1 NAME
10              
11             AI::Ollama::Client - Client for AI::Ollama
12              
13             =head1 SYNOPSIS
14              
15             use 5.020;
16             use AI::Ollama::Client;
17              
18             my $client = AI::Ollama::Client->new(
19             server => 'https://example.com/',
20             );
21             my $res = $client->someMethod()->get;
22             say $res;
23              
24             =head1 METHODS
25              
26             =head2 C<< checkBlob >>
27              
28             my $res = $client->checkBlob()->get;
29              
30             Check to see if a blob exists on the Ollama server which is useful when creating models.
31              
32             =cut
33              
34             around 'checkBlob' => sub ( $super, $self, %options ) {
35             $super->( $self, %options )->then( sub( $res ) {
36             if( $res->code =~ /^2\d\d$/ ) {
37             return Future->done( 1 )
38             } else {
39             return Future->done( 0 )
40             }
41             });
42             };
43              
44             =head2 C<< createBlob >>
45              
46             my $res = $client->createBlob()->get;
47              
48             Create a blob from a file. Returns the server file path.
49              
50             =cut
51              
52             =head2 C<< generateChatCompletion >>
53              
54             my $res = $client->generateChatCompletion()->get;
55              
56             Generate the next message in a chat with a provided model.
57              
58             Returns a L<< AI::Ollama::GenerateChatCompletionResponse >>.
59              
60             =cut
61              
62             =head2 C<< copyModel >>
63              
64             my $res = $client->copyModel()->get;
65              
66             Creates a model with another name from an existing model.
67              
68              
69             =cut
70              
71             =head2 C<< createModel >>
72              
73             my $res = $client->createModel()->get;
74              
75             Create a model from a Modelfile.
76              
77             Returns a L<< AI::Ollama::CreateModelResponse >>.
78              
79             =cut
80              
81             =head2 C<< deleteModel >>
82              
83             my $res = $client->deleteModel()->get;
84              
85             Delete a model and its data.
86              
87              
88             =cut
89              
90             =head2 C<< generateEmbedding >>
91              
92             my $res = $client->generateEmbedding()->get;
93              
94             Generate embeddings from a model.
95              
96             Returns a L<< AI::Ollama::GenerateEmbeddingResponse >>.
97              
98             =cut
99              
100             =head2 C<< generateCompletion >>
101              
102             use Future::Utils 'repeat';
103             my $responses = $client->generateCompletion();
104             repeat {
105             my ($res) = $responses->shift;
106             if( $res ) {
107             my $str = $res->get;
108             say $str;
109             }
110              
111             Future::Mojo->done( defined $res );
112             } until => sub($done) { $done->get };
113              
114             Generate a response for a given prompt with a provided model.
115              
116             Returns a L<< AI::Ollama::GenerateCompletionResponse >>.
117              
118             =cut
119              
120             around 'generateCompletion' => sub ( $super, $self, %options ) {
121             # Encode images as base64, if images exist:
122             # (but create a copy so we don't over write the input array)
123             if (my $images = $options{images}) {
124              
125             # Allow { filename => '/etc/passwd' }
126             $options{images} = [
127             map {
128             my $item = $_;
129             if( ref($item) eq 'HASH' ) {
130             $item = Mojo::File->new($item->{filename})->slurp();
131             };
132             encode_base64($item)
133             } @$images ];
134             }
135             return $super->($self, %options);
136             };
137              
138             =head2 C<< pullModel >>
139              
140             my $res = $client->pullModel(
141             name => 'llama',
142             )->get;
143              
144             Download a model from the ollama library.
145              
146             Returns a L<< AI::Ollama::PullModelResponse >>.
147              
148             =cut
149              
150             =head2 C<< pushModel >>
151              
152             my $res = $client->pushModel()->get;
153              
154             Upload a model to a model library.
155              
156             Returns a L<< AI::Ollama::PushModelResponse >>.
157              
158             =cut
159              
160             =head2 C<< showModelInfo >>
161              
162             my $info = $client->showModelInfo()->get;
163             say $info->modelfile;
164              
165             Show details about a model including modelfile, template, parameters, license, and system prompt.
166              
167             Returns a L<< AI::Ollama::ModelInfo >>.
168              
169             =cut
170              
171             =head2 C<< listModels >>
172              
173             my $info = $client->listModels()->get;
174             for my $model ($info->models->@*) {
175             say $model->model; # llama2:latest
176             }
177              
178             List models that are available locally.
179              
180             Returns a L<< AI::Ollama::ModelsResponse >>.
181              
182             =cut
183              
184             1;