PHP code example of ardagnsrn / ollama-php

1. Go to this page and download the library: Download ardagnsrn/ollama-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ardagnsrn / ollama-php example snippets


// with default base URL
$client = \ArdaGnsrn\Ollama\Ollama::client();

// or with custom base URL
$client = \ArdaGnsrn\Ollama\Ollama::client('http://localhost:11434');

$completions = $client->completions()->create([
    'model' => 'llama3.1',
    'prompt' => 'Once upon a time',
]);

$completions->response; // '...in a land far, far away...'

$response->toArray(); // ['model' => 'llama3.1', 'response' => '...in a land far, far away...', ...]

$completions = $client->completions()->createStreamed([
    'model' => 'llama3.1',
    'prompt' => 'Once upon a time',
]);


foreach ($completions as $completion) {
    echo $completion->response;
}
// 1. Iteration: '...in'
// 2. Iteration: ' a'
// 3. Iteration: ' land'
// 4. Iteration: ' far,'
// ...

$response = $client->chat()->create([
    'model' => 'llama3.1',
    'messages' => [
        ['role' => 'system', 'content' => 'You are a llama.'],
        ['role' => 'user', 'content' => 'Hello!'],
        ['role' => 'assistant', 'content' => 'Hi! How can I help you today?'],
        ['role' => 'user', 'content' => 'I need help with my taxes.'],
    ],
]);

$response->message->content; // 'Ah, taxes... *chew chew* Hmm, not really sure how to help with that.'

$response->toArray(); // ['model' => 'llama3.1', 'message' => ['role' => 'assistant', 'content' => 'Ah, taxes...'], ...]

$response = $client->chat()->create([
    'model' => 'llama3.1',
    'messages' => [
        ['role' => 'user', 'content' => 'What is the weather today in Paris?'],
    ],
    'tools' => [
        [
            'type' => 'function',
            'function' => [
                'name' => 'get_current_weather',
                'description' => 'Get the current weather',
                'parameters' => [
                    'type' => 'object',
                    'properties' => [
                        'location' => [
                            'type' => 'string',
                            'description' => 'The location to get the weather for, e.g. San Francisco, CA',
                        ],
                        'format' => [
                            'type' => 'string',
                            'description' => 'The location to get the weather for, e.g. San Francisco, CA',
                            'enum' => ['celsius', 'fahrenheit']
                        ],
                    ],
                    '

$responses = $client->chat()->createStreamed([
    'model' => 'llama3.1',
    'messages' => [
        ['role' => 'system', 'content' => 'You are a llama.'],
        ['role' => 'user', 'content' => 'Hello!'],
        ['role' => 'assistant', 'content' => 'Hi! How can I help you today?'],
        ['role' => 'user', 'content' => 'I need help with my taxes.'],
    ],
]);


foreach ($responses as $response) {
    echo $response->message->content;
}
// 1. Iteration: 'Ah,'
// 2. Iteration: ' taxes'
// 3. Iteration: '... '
// 4. Iteration: ' *chew,'
// ...

$response = $client->models()->list();

$response->toArray(); // ['models' => [['name' => 'llama3.1', ...], ['name' => 'llama3.1:80b', ...], ...]]

$response = $client->models()->show('llama3.1');

$response->toArray(); // ['modelfile' => '...', 'parameters' => '...', 'template' => '...']

$response = $client->models()->create([
    'name' => 'mario',
    'modelfile' => "FROM llama3.1\nSYSTEM You are mario from Super Mario Bros."
]);

$response->status; // 'success'

$responses = $client->models()->createStreamed([
    'name' => 'mario',
    'modelfile' => "FROM llama3.1\nSYSTEM You are mario from Super Mario Bros."
]);

foreach ($responses as $response) {
    echo $response->status;
}

$client->models()->copy('llama3.1', 'llama3.2'); // bool

$client->models()->delete('mario'); // bool

$response = $client->models()->pull('llama3.1'); 
$response->toArray() // ['status' => 'downloading digestname', 'digest' => 'digestname', 'total' => 2142590208, 'completed' => 241970]

$responses = $client->models()->pullStreamed('llama3.1'); 

foreach ($responses as $response) {
    echo $response->status; 
}

$response = $client->models()->push('llama3.1');
$response->toArray() // ['status' => 'uploading digestname', 'digest' => 'digestname', 'total' => 2142590208]

$responses = $client->models()->pushStreamed('llama3.1');

foreach ($responses as $response) {
    echo $response->status; 
}

$response = $client->models()->runningList();

$response->toArray(); // ['models' => [['name' => 'llama3.1', ...], ['name' => 'llama3.1:80b', ...], ...]]

$client->blobs()->exists('blobname'); // bool

$client->blobs()->create('blobname'); // bool

$response = $client->embed()->create([
    'model' => 'llama3.1',
    'input' => [
        "Why is the sky blue?",
    ]
]);

$response->toArray(); // ['model' => 'llama3.1', 'embedding' => [0.1, 0.2, ...], ...]
bash
composer