AI-Ollama-Client

 view release on metacpan or  search on metacpan

META.json  view on Meta::CPAN

         }
      },
      "runtime" : {
         "requires" : {
            "Carp" : "0",
            "File::ShareDir" : "0",
            "Future" : "0",
            "Future::Mojo" : "0",
            "Future::Queue" : "0",
            "Future::Utils" : "0",
            "Mojo::JSON" : "0",
            "Mojo::URL" : "0",
            "Mojo::UserAgent" : "0",
            "Mojolicious" : "9",
            "Moo" : "2",
            "OpenAPI::Modern" : "0",
            "PerlX::Maybe" : "0",
            "Role::EventEmitter" : "0",
            "URI::Template" : "0",
            "YAML::PP" : "0",
            "experimental" : "0.031",

META.json  view on Meta::CPAN

      "license" : [
         "https://dev.perl.org/licenses/"
      ],
      "repository" : {
         "type" : "git",
         "url" : "git://github.com/Corion/AI-Ollama-Client.git",
         "web" : "https://github.com/Corion/AI-Ollama-Client"
      }
   },
   "version" : "0.05",
   "x_serialization_backend" : "JSON::PP version 4.07",
   "x_static_install" : 1
}

META.yml  view on Meta::CPAN

  directory:
    - t
    - inc
requires:
  Carp: '0'
  File::ShareDir: '0'
  Future: '0'
  Future::Mojo: '0'
  Future::Queue: '0'
  Future::Utils: '0'
  Mojo::JSON: '0'
  Mojo::URL: '0'
  Mojo::UserAgent: '0'
  Mojolicious: '9'
  Moo: '2'
  OpenAPI::Modern: '0'
  PerlX::Maybe: '0'
  Role::EventEmitter: '0'
  URI::Template: '0'
  YAML::PP: '0'
  experimental: '0.031'

Makefile.PL  view on Meta::CPAN

        'stable'              => '0.031',
        'experimental'        => '0.031',
        'Carp'                => 0,
        'File::ShareDir'      => 0,
        'Future'              => 0,
        'Future::Mojo'        => 0,
        'Future::Utils'       => 0,
        'Future::Queue'       => 0,
        'Moo'                 => 2,
        'Mojolicious'         => 9,
        'Mojo::JSON'          => 0,
        'Mojo::URL'           => 0,
        'Mojo::UserAgent'     => 0,
        'OpenAPI::Modern'     => 0,
        'PerlX::Maybe'        => 0,
        'Role::EventEmitter'  => 0,
        'URI::Template'       => 0,
        'YAML::PP'            => 0,
    },
    TEST_REQUIRES => {
        'Test2::V0'    => 0,

lib/AI/Ollama/Client/Impl.pm  view on Meta::CPAN

with 'Role::EventEmitter';
use experimental 'signatures';
use PerlX::Maybe;
use Carp 'croak';

# These should go into a ::Role
use YAML::PP;
use Mojo::UserAgent;
use Mojo::URL;
use URI::Template;
use Mojo::JSON 'encode_json', 'decode_json';
use OpenAPI::Modern;

use File::ShareDir 'module_file';

use Future::Mojo;
use Future::Queue;

our $SCHEMA_VERSION = "0.1.9";

use AI::Ollama::CopyModelRequest;

lib/AI/Ollama/Client/Impl.pm  view on Meta::CPAN


has 'schema_file' => (
    is => 'lazy',
    default => sub { require AI::Ollama::Client::Impl; module_file('AI::Ollama::Client::Impl', 'ollama-curated.yaml') },
);

has 'schema' => (
    is => 'lazy',
    default => sub {
        if( my $fn = $_[0]->schema_file ) {
            YAML::PP->new( boolean => 'JSON::PP' )->load_file($fn);
        }
    },
);

has 'validate_requests' => (
    is => 'rw',
    default => 1,
);

has 'validate_responses' => (

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

=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/GenerateChatCompletionRequest.pm  view on Meta::CPAN

sub as_hash( $self ) {
    return { $self->%* }
}

=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/GenerateCompletionRequest.pm  view on Meta::CPAN


has 'context' => (
    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",
    ],
);

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

            The scale of the rope frequency. (Default: 1.0)
        num_thread:
          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

scripts/code-completion.pl  view on Meta::CPAN

package main;
use 5.020;
use Mojo::JSON 'decode_json';
use experimental 'signatures';
use AI::Ollama::Client;
use Future::Utils 'repeat';

#use Getopt::Long;
#GetOptions(
#    'prefix|p=s' => \my $prefix,
#    'suffix|s=s' => \my $suffix,
#);

scripts/describe-image.pl  view on Meta::CPAN

package main;
use 5.020;
use Mojo::JSON 'decode_json';
use experimental 'signatures';
use AI::Ollama::Client;
use Future::Utils 'repeat';

my $ol = AI::Ollama::Client->new(
    server => 'http://192.168.1.97:11434/api',
);

$ol->on('request' => sub( $ol, $tx ) {
    use Data::Dumper;

scripts/music-genre-json.pl  view on Meta::CPAN

package main;
use 5.020;
use Mojo::JSON 'decode_json';
use experimental 'signatures';
use AI::Ollama::Client;
use Future::Utils 'repeat';

my $ol = AI::Ollama::Client->new(
    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;

scripts/music-genre-json.pl  view on Meta::CPAN

            $chat .= $info->message->{content};
        };

        Future::Mojo->done( $info->done || !defined $res );
    }; warn $@ if $@;
    $check
    } until => sub($done) { my $res = $done->get; return $res };

    # Try to extract from a text list
    #my @genres = ($chat =~ /^\s*[\d]+\.\s*(.*?)$/mg);
    # Try to extract from a JSON string
    my @genres;
    my ($json) = ($chat =~ /^(\[.*\])$/msg);
    if( $json ) {
        @genres = decode_json( $json )->@*;
    };

    if( ! @genres ) {
        say "Did not find genres in:";
        say $chat;
    };



( run in 1.574 second using v1.01-cache-2.11-cpan-cdf2f3d4e48 )