view release on metacpan or search on metacpan
README.mkdn view on Meta::CPAN
repeat {
my ($res) = $responses->shift;
if( $res ) {
my $str = $res->get;
say $str;
}
Future::Mojo->done( defined $res );
} until => sub($done) { $done->get };
Generate a response for a given prompt with a provided model.
Returns a [AI::Ollama::GenerateCompletionResponse](https://metacpan.org/pod/AI%3A%3AOllama%3A%3AGenerateCompletionResponse).
## `pullModel`
my $res = $client->pullModel(
name => 'llama',
)->get;
Download a model from the ollama library.
README.mkdn view on Meta::CPAN
Upload a model to a model library.
Returns a [AI::Ollama::PushModelResponse](https://metacpan.org/pod/AI%3A%3AOllama%3A%3APushModelResponse).
## `showModelInfo`
my $info = $client->showModelInfo()->get;
say $info->modelfile;
Show details about a model including modelfile, template, parameters, license, and system prompt.
Returns a [AI::Ollama::ModelInfo](https://metacpan.org/pod/AI%3A%3AOllama%3A%3AModelInfo).
## `listModels`
my $info = $client->listModels()->get;
for my $model ($info->models->@*) {
say $model->model; # llama2:latest
}
lib/AI/Ollama/Client.pm view on Meta::CPAN
repeat {
my ($res) = $responses->shift;
if( $res ) {
my $str = $res->get;
say $str;
}
Future::Mojo->done( defined $res );
} until => sub($done) { $done->get };
Generate a response for a given prompt with a provided model.
Returns a L<< AI::Ollama::GenerateCompletionResponse >>.
=cut
around 'generateCompletion' => sub ( $super, $self, %options ) {
# Encode images as base64, if images exist:
# (but create a copy so we don't over write the input array)
if (my $images = $options{images}) {
lib/AI/Ollama/Client.pm view on Meta::CPAN
Returns a L<< AI::Ollama::PushModelResponse >>.
=cut
=head2 C<< showModelInfo >>
my $info = $client->showModelInfo()->get;
say $info->modelfile;
Show details about a model including modelfile, template, parameters, license, and system prompt.
Returns a L<< AI::Ollama::ModelInfo >>.
=cut
=head2 C<< listModels >>
my $info = $client->listModels()->get;
for my $model ($info->models->@*) {
say $model->model; # llama2:latest
lib/AI/Ollama/Client/Impl.pm view on Meta::CPAN
=head3 Options
=over 4
=item C<< format >>
The format to return a response in. Currently the only accepted value is json.
Enable JSON mode by setting the format parameter to json. This will structure the response as valid JSON.
Note: it's important to instruct the model to use JSON in the prompt. Otherwise, the model may generate large amounts whitespace.
=item C<< keep_alive >>
How long (in minutes) to keep the model loaded in memory.
=over
=item -
If set to a positive duration (e.g. 20), the model will stay loaded for the provided duration.
lib/AI/Ollama/Client/Impl.pm view on Meta::CPAN
=item C<< model >>
The model name.
Model names follow a C<model:tag> format. Some examples are C<orca-mini:3b-q4_1> and C<llama2:70b>. The tag is optional and, if not provided, will default to C<latest>. The tag is used to identify a specific version.
=item C<< options >>
Additional model parameters listed in the documentation for the Modelfile such as C<temperature>.
=item C<< prompt >>
Text to generate embeddings for.
=back
Returns a L<< AI::Ollama::GenerateEmbeddingResponse >> on success.
=cut
sub build_generateEmbedding_request( $self, %options ) {
lib/AI/Ollama/Client/Impl.pm view on Meta::CPAN
repeat {
my ($res) = $streamed->shift;
if( $res ) {
my $str = $res->get;
say $str;
}
Future::Mojo->done( defined $res );
} until => sub($done) { $done->get };
Generate a response for a given prompt with a provided model.
The final response object will include statistics and additional data from the request.
=head3 Options
=over 4
=item C<< context >>
The context parameter returned from a previous request to [generateCompletion], this can be used to keep a short conversational memory.
=item C<< format >>
The format to return a response in. Currently the only accepted value is json.
Enable JSON mode by setting the format parameter to json. This will structure the response as valid JSON.
Note: it's important to instruct the model to use JSON in the prompt. Otherwise, the model may generate large amounts whitespace.
=item C<< images >>
(optional) a list of Base64-encoded images to include in the message (for multimodal models such as llava)
=item C<< keep_alive >>
How long (in minutes) to keep the model loaded in memory.
=over
lib/AI/Ollama/Client/Impl.pm view on Meta::CPAN
=item C<< model >>
The model name.
Model names follow a C<model:tag> format. Some examples are C<orca-mini:3b-q4_1> and C<llama2:70b>. The tag is optional and, if not provided, will default to C<latest>. The tag is used to identify a specific version.
=item C<< options >>
Additional model parameters listed in the documentation for the Modelfile such as C<temperature>.
=item C<< prompt >>
The prompt to generate a response.
=item C<< raw >>
If C<true> no formatting will be applied to the prompt and no context will be returned.
You may choose to use the C<raw> parameter if you are specifying a full templated prompt in your request to the API, and are managing history yourself.
=item C<< stream >>
If C<false> the response will be returned as a single response object, otherwise the response will be streamed as a series of objects.
=item C<< system >>
The system prompt to (overrides what is defined in the Modelfile).
=item C<< template >>
The full prompt or prompt template (overrides what is defined in the Modelfile).
=back
Returns a L<< AI::Ollama::GenerateCompletionResponse >> on success.
=cut
sub build_generateCompletion_request( $self, %options ) {
my $method = 'POST';
my $path = '/generate';
lib/AI/Ollama/Client/Impl.pm view on Meta::CPAN
}
=head2 C<< build_showModelInfo_request >>
Build an HTTP request as L<Mojo::Request> object. For the parameters see below.
=head2 C<< showModelInfo >>
my $res = $client->showModelInfo()->get;
Show details about a model including modelfile, template, parameters, license, and system prompt.
=head3 Options
=over 4
=item C<< name >>
The model name.
lib/AI/Ollama/GenerateChatCompletionRequest.pm view on Meta::CPAN
}
=head1 PROPERTIES
=head2 C<< format >>
The format to return a response in. Currently the only accepted value is json.
Enable JSON mode by setting the format parameter to json. This will structure the response as valid JSON.
Note: it's important to instruct the model to use JSON in the prompt. Otherwise, the model may generate large amounts whitespace.
=cut
has 'format' => (
is => 'ro',
isa => Enum[
"json",
],
);
lib/AI/Ollama/GenerateChatCompletionResponse.pm view on Meta::CPAN
Model names follow a `model:tag` format. Some examples are `orca-mini:3b-q4_1` and `llama2:70b`. The tag is optional and, if not provided, will default to `latest`. The tag is used to identify a specific version.
=cut
has 'model' => (
is => 'ro',
isa => Str,
);
=head2 C<< prompt_eval_count >>
Number of tokens in the prompt.
=cut
has 'prompt_eval_count' => (
is => 'ro',
isa => Int,
);
=head2 C<< prompt_eval_duration >>
Time spent in nanoseconds evaluating the prompt.
=cut
has 'prompt_eval_duration' => (
is => 'ro',
isa => Int,
);
=head2 C<< total_duration >>
Time spent generating the response.
=cut
lib/AI/Ollama/GenerateCompletionRequest.pm view on Meta::CPAN
is => 'ro',
isa => ArrayRef[Int],
);
=head2 C<< format >>
The format to return a response in. Currently the only accepted value is json.
Enable JSON mode by setting the format parameter to json. This will structure the response as valid JSON.
Note: it's important to instruct the model to use JSON in the prompt. Otherwise, the model may generate large amounts whitespace.
=cut
has 'format' => (
is => 'ro',
isa => Enum[
"json",
],
);
lib/AI/Ollama/GenerateCompletionRequest.pm view on Meta::CPAN
Additional model parameters listed in the documentation for the Modelfile such as `temperature`.
=cut
has 'options' => (
is => 'ro',
isa => HashRef,
);
=head2 C<< prompt >>
The prompt to generate a response.
=cut
has 'prompt' => (
is => 'ro',
isa => Str,
required => 1,
);
=head2 C<< raw >>
If `true` no formatting will be applied to the prompt and no context will be returned.
You may choose to use the `raw` parameter if you are specifying a full templated prompt in your request to the API, and are managing history yourself.
=cut
has 'raw' => (
is => 'ro',
);
=head2 C<< stream >>
If `false` the response will be returned as a single response object, otherwise the response will be streamed as a series of objects.
=cut
has 'stream' => (
is => 'ro',
);
=head2 C<< system >>
The system prompt to (overrides what is defined in the Modelfile).
=cut
has 'system' => (
is => 'ro',
isa => Str,
);
=head2 C<< template >>
The full prompt or prompt template (overrides what is defined in the Modelfile).
=cut
has 'template' => (
is => 'ro',
isa => Str,
);
1;
lib/AI/Ollama/GenerateCompletionResponse.pm view on Meta::CPAN
Model names follow a `model:tag` format. Some examples are `orca-mini:3b-q4_1` and `llama2:70b`. The tag is optional and, if not provided, will default to `latest`. The tag is used to identify a specific version.
=cut
has 'model' => (
is => 'ro',
isa => Str,
);
=head2 C<< prompt_eval_count >>
Number of tokens in the prompt.
=cut
has 'prompt_eval_count' => (
is => 'ro',
isa => Int,
);
=head2 C<< prompt_eval_duration >>
Time spent in nanoseconds evaluating the prompt.
=cut
has 'prompt_eval_duration' => (
is => 'ro',
isa => Int,
);
=head2 C<< response >>
The response for a given prompt with a provided model.
=cut
has 'response' => (
is => 'ro',
isa => Str,
);
=head2 C<< total_duration >>
lib/AI/Ollama/GenerateEmbeddingRequest.pm view on Meta::CPAN
Additional model parameters listed in the documentation for the Modelfile such as `temperature`.
=cut
has 'options' => (
is => 'ro',
isa => HashRef,
);
=head2 C<< prompt >>
Text to generate embeddings for.
=cut
has 'prompt' => (
is => 'ro',
isa => Str,
required => 1,
);
1;
lib/AI/Ollama/GenerateEmbeddingResponse.pm view on Meta::CPAN
=cut
sub as_hash( $self ) {
return { $self->%* }
}
=head1 PROPERTIES
=head2 C<< embedding >>
The embedding for the prompt.
=cut
has 'embedding' => (
is => 'ro',
isa => ArrayRef[Num],
);
1;
lib/AI/Ollama/ModelInfo.pm view on Meta::CPAN
=cut
has 'parameters' => (
is => 'ro',
isa => Str,
);
=head2 C<< template >>
The prompt template for the model.
=cut
has 'template' => (
is => 'ro',
isa => Str,
);
1;
lib/AI/Ollama/RequestOptions.pm view on Meta::CPAN
=cut
has 'num_gqa' => (
is => 'ro',
isa => Int,
);
=head2 C<< num_keep >>
Number of tokens to keep from the prompt.
=cut
has 'num_keep' => (
is => 'ro',
isa => Int,
);
=head2 C<< num_predict >>
lib/AI/Ollama/RequestOptions.pm view on Meta::CPAN
=cut
has 'rope_frequency_scale' => (
is => 'ro',
isa => Num,
);
=head2 C<< seed >>
Sets the random number seed to use for generation. Setting this to a specific number will make the model generate the same text for the same prompt. (Default: 0)
=cut
has 'seed' => (
is => 'ro',
isa => Int,
);
=head2 C<< stop >>
ollama/ollama-curated.json view on Meta::CPAN
{"openapi":"3.0.3","components":{"schemas":{"PushModelResponse":{"properties":{"total":{"type":"integer","description":"total size of the model","example":"2142590208"},"status":{"$ref":"#/components/schemas/PushModelStatus"},"digest":{"example":"sha...
ollama/ollama-curated.yaml view on Meta::CPAN
title: Ollama API
description: API Spec for Ollama API. Please see https://github.com/jmorganca/ollama/blob/main/docs/api.md for more details.
version: 0.1.9
#servers:
# - url: http://localhost:11434/api
# description: Ollama server URL
tags:
- name: Completions
description: Given a prompt, the model will generate a completion.
- name: Chat
description: Given a list of messages comprising a conversation, the model will return a response.
- name: Embeddings
description: Get a vector representation of a given input.
- name: Models
description: List and describe the various models available.
paths:
/generate:
post:
operationId: generateCompletion
tags:
- Completions
summary: Generate a response for a given prompt with a provided model.
description: The final response object will include statistics and additional data from the request.
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/GenerateCompletionRequest'
responses:
'200':
description: Successful operation.
content:
ollama/ollama-curated.yaml view on Meta::CPAN
description: Successful operation.
content:
application/json:
schema:
$ref: '#/components/schemas/ModelsResponse'
/show:
post:
operationId: showModelInfo
tags:
- Models
summary: Show details about a model including modelfile, template, parameters, license, and system prompt.
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/ModelInfoRequest'
responses:
'200':
description: Successful operation.
content:
application/json:
ollama/ollama-curated.yaml view on Meta::CPAN
type: object
description: Request class for the generate endpoint.
properties:
model:
type: string
description: &model_name |
The model name.
Model names follow a `model:tag` format. Some examples are `orca-mini:3b-q4_1` and `llama2:70b`. The tag is optional and, if not provided, will default to `latest`. The tag is used to identify a specific version.
example: llama2:7b
prompt:
type: string
description: The prompt to generate a response.
example: Why is the sky blue?
images:
type: array
description: (optional) a list of Base64-encoded images to include in the message (for multimodal models such as llava)
items:
type: string
contentEncoding: base64
description: Base64-encoded image (for multimodal models such as llava)
example: iVBORw0KGgoAAAANSUhEUgAAAAkAAAANCAIAAAD0YtNRAAAABnRSTlMA/AD+APzoM1ogAAAAWklEQVR4AWP48+8PLkR7uUdzcMvtU8EhdykHKAciEXL3pvw5FQIURaBDJkARoDhY3zEXiCgCHbNBmAlUiyaBkENoxZSDWnOtBmoAQu7TnT+3WuDOA7KBIkAGAGwiNeqjusp/AAAAAElFTkSuQmCC
system:
type: string
description: The system prompt to (overrides what is defined in the Modelfile).
template:
type: string
description: The full prompt or prompt template (overrides what is defined in the Modelfile).
context:
type: array
description: The context parameter returned from a previous request to [generateCompletion], this can be used to keep a short conversational memory.
items:
type: integer
options:
$ref: '#/components/schemas/RequestOptions'
format:
$ref: '#/components/schemas/ResponseFormat'
raw:
type: boolean
description: |
If `true` no formatting will be applied to the prompt and no context will be returned.
You may choose to use the `raw` parameter if you are specifying a full templated prompt in your request to the API, and are managing history yourself.
stream:
type: boolean
description: &stream |
If `false` the response will be returned as a single response object, otherwise the response will be streamed as a series of objects.
default: false
keep_alive:
type: integer
description: &keep_alive |
How long (in minutes) to keep the model loaded in memory.
- If set to a positive duration (e.g. 20), the model will stay loaded for the provided duration.
- If set to a negative duration (e.g. -1), the model will stay loaded indefinitely.
- If set to 0, the model will be unloaded immediately once finished.
- If not set, the model will stay loaded for 5 minutes by default
required:
- model
- prompt
RequestOptions:
type: object
description: Additional model parameters listed in the documentation for the Modelfile such as `temperature`.
properties:
num_keep:
type: integer
description: |
Number of tokens to keep from the prompt.
seed:
type: integer
description: |
Sets the random number seed to use for generation. Setting this to a specific number will make the model generate the same text for the same prompt. (Default: 0)
num_predict:
type: integer
description: |
Maximum number of tokens to predict when generating text. (Default: 128, -1 = infinite generation, -2 = fill context)
top_k:
type: integer
description: |
Reduces the probability of generating nonsense. A higher value (e.g. 100) will give more diverse answers, while a lower value (e.g. 10) will be more conservative. (Default: 40)
top_p:
type: number
ollama/ollama-curated.yaml view on Meta::CPAN
type: integer
description: |
Sets the number of threads to use during computation. By default, Ollama will detect this for optimal performance. It is recommended to set this value to the number of physical CPU cores your system has (as opposed to the logical number o...
ResponseFormat:
type: string
description: |
The format to return a response in. Currently the only accepted value is json.
Enable JSON mode by setting the format parameter to json. This will structure the response as valid JSON.
Note: it's important to instruct the model to use JSON in the prompt. Otherwise, the model may generate large amounts whitespace.
enum:
- json
GenerateCompletionResponse:
type: object
description: The response class for the generate endpoint.
properties:
model:
type: string
description: *model_name
example: llama2:7b
created_at:
type: string
format: date-time
description: Date on which a model was created.
example: 2023-08-04T19:22:45.499127Z
response:
type: string
description: The response for a given prompt with a provided model.
example: The sky appears blue because of a phenomenon called Rayleigh scattering.
done:
type: boolean
description: Whether the response has completed.
example: true
context:
type: array
description: |
An encoding of the conversation used in this response, this can be sent in the next request to keep a conversational memory.
items:
type: integer
example: [ 1, 2, 3 ]
total_duration:
type: integer
description: Time spent generating the response.
example: 5589157167
load_duration:
type: integer
description: Time spent in nanoseconds loading the model.
example: 3013701500
prompt_eval_count:
type: integer
description: Number of tokens in the prompt.
example: 46
prompt_eval_duration:
type: integer
description: Time spent in nanoseconds evaluating the prompt.
example: 1160282000
eval_count:
type: integer
description: Number of tokens the response.
example: 113
eval_duration:
type: integer
description: Time in nanoseconds spent generating the response.
example: 1325948000
GenerateChatCompletionRequest:
ollama/ollama-curated.yaml view on Meta::CPAN
description: Whether the response has completed.
example: true
total_duration:
type: integer
description: Time spent generating the response.
example: 5589157167
load_duration:
type: integer
description: Time spent in nanoseconds loading the model.
example: 3013701500
prompt_eval_count:
type: integer
description: Number of tokens in the prompt.
example: 46
prompt_eval_duration:
type: integer
description: Time spent in nanoseconds evaluating the prompt.
example: 1160282000
eval_count:
type: integer
description: Number of tokens the response.
example: 113
eval_duration:
type: integer
description: Time in nanoseconds spent generating the response.
example: 1325948000
Message:
ollama/ollama-curated.yaml view on Meta::CPAN
- role
- content
GenerateEmbeddingRequest:
description: Generate embeddings from a model.
type: object
properties:
model:
type: string
description: *model_name
example: llama2:7b
prompt:
type: string
description: Text to generate embeddings for.
example: 'Here is an article about llamas...'
options:
$ref: '#/components/schemas/RequestOptions'
required:
- model
- prompt
GenerateEmbeddingResponse:
type: object
description: Returns the embedding information.
properties:
embedding:
type: array
description: The embedding for the prompt.
items:
type: number
format: double
example: [ 0.5670403838157654, 0.009260174818336964, ... ]
CreateModelRequest:
type: object
description: Create model request object.
properties:
name:
type: string
ollama/ollama-curated.yaml view on Meta::CPAN
description: Request class for the show model info endpoint.
type: object
properties:
name:
type: string
description: *model_name
example: llama2:7b
required:
- name
ModelInfo:
description: Details about a model including modelfile, template, parameters, license, and system prompt.
type: object
properties:
license:
type: string
description: The model's license.
example: <contents of license block>
modelfile:
type: string
description: The modelfile associated with the model.
example: 'Modelfile generated by \"ollama show\"\n# To build a new Modelfile based on this one, replace the FROM line with:\n# FROM llama2:latest\n\nFROM /Users/username/.ollama/models/blobs/sha256:8daa9615cce30c259a9555b1cc250d461d1bc69980...
parameters:
type: string
description: The model parameters.
example: 'stop [INST]\nstop [/INST]\nstop <<SYS>>\nstop <</SYS>>'
template:
type: string
description: The prompt template for the model.
example: '[INST] {{ if and .First .System }}<<SYS>>{{ .System }}<</SYS>>\n\n{{ end }}{{ .Prompt }} [/INST]'
CopyModelRequest:
description: Request class for copying a model.
type: object
properties:
source:
type: string
description: Name of the model to copy.
example: llama2:7b
destination:
scripts/code-completion.pl view on Meta::CPAN
my $ol = AI::Ollama::Client->new(
server => 'http://192.168.1.97:11434/api',
);
my $model = 'codellama:13b-code';
my $tx = $ol->pullModel(
name => $model,
)->get;
my @prompts = @ARGV ? @ARGV : (qq{fetch an url and print its content with Mojolicious; write concise code <PRE> sub fetch {\n <SUF> } <MID>});
for my $prompt (@prompts) {
my $response = $ol->generateCompletion(
model => $model,
prompt => $prompt,
system => 'You are a helpful concise coding assistant',
);
my $code;
my $responses = $response->get;
repeat {
my ($res) = $responses->shift;
my $info;
if( $res ) {
$info = $res->get;
local $| = 1;
print $info->response;
$code .= $info->response;
};
Future::Mojo->done( $info->done || !defined $res );
} until => sub($done) { my $res = $done->get; return $res };
if( $code =~ /\A(.*?)<EOT>/s ) {
my $insert = $1;
my ($pre,$suf) = ($prompt =~ /<PRE>(.*?)<SUF>(.*?)<MID>/s);
print "$pre$insert$suf";
}
}
scripts/describe-image.pl view on Meta::CPAN
name => 'llava:latest',
)->catch(sub {
use Data::Dumper; warn Dumper \@_;
})->get;
my @images = @ARGV ? @ARGV : ('t/testdata/objectdetection.jpg');
for my $image (@images) {
my $response = $ol->generateCompletion(
model => 'llava:latest',
prompt => 'You are tagging images. Please list all the objects in this image as tags. Also list the location where it was taken.',
images => [
{ filename => $image },
],
);
my $responses = $response->get;
repeat {
my ($res) = $responses->shift;
my $info;
if( $res ) {
scripts/music-genre-json.pl view on Meta::CPAN
server => 'http://192.168.1.97:11434/api',
);
#my $model = 'llava:13b';
my $model = 'llama2';
my $tx = $ol->pullModel(
name => $model,
stream => JSON::PP::false(),
)->get;
warn "Pulled '$model'";
my @prompts = @ARGV ? @ARGV : (
qq!Please tell me three musical genres of the song "Go West" by "The Pet Shop Boys" as JSON like ```[{"genre":"the genre name"}, ...]```!
);
for my $prompt (@prompts) {
my $response = $ol->generateChatCompletion(
model => $model,
prompt => $prompt,
temperature => '0.0',
messages => [
{role => 'system',
content => join "\n",
'You are a music expert.',
'You are given an artist name and song title.',
'Please suggest three musical genres of that title and performer.',
'Only list the musical genres.',
#'Answer in JSON only with an array containing objects { "genre": "the genre", "sub-genre": "the sub genre" }.',
},
{ role => 'user', content => $prompt },
],
);
my $chat;
my $responses = $response->get;
repeat {
my $check = eval {
my ($res) = $responses->shift;
my $info;
if( $res ) {
scripts/music-genre-json.pl view on Meta::CPAN
};
if( ! @genres ) {
say "Did not find genres in:";
say $chat;
};
use Data::Dumper; warn Dumper \@genres;
#if( $code =~ /\A(.*?)<EOT>/s ) {
# my $insert = $1;
# my ($pre,$suf) = ($prompt =~ /<PRE>(.*?)<SUF>(.*?)<MID>/s);
# print "$pre$insert$suf";
#}
}
t/generate.request view on Meta::CPAN
Host: localhost:11434
Origin: https://dhcode.github.io
Pragma: no-cache
Sec-Fetch-Dest: empty
Sec-Fetch-Mode: cors
Sec-Fetch-Site: cross-site
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:123.0) Gecko/20100101 Firefox/123.0
{
"model": "",
"prompt": "",
"images": [
""
],
"system": "",
"template": "",
"context": [
0
],
"options": {
"num_keep": 0,