PHP code example of lewenbraun / ollama-php-sdk

1. Go to this page and download the library: Download lewenbraun/ollama-php-sdk 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/ */

    

lewenbraun / ollama-php-sdk example snippets


use Lewenbraun\Ollama\Ollama;

$client = Ollama::client('http://localhost:11434');

$completion = $client->completion()->create([
    'model'  => 'llama3.2',
    'prompt' => 'Hello',
    // 'stream' is false by default
]);

echo $completion->response;

$completion = $client->completion()->create([
    'model'  => 'llama3.2',
    'prompt' => 'Hello',
    'stream' => true,
]);

foreach ($completion as $chunk) {
    echo $chunk->response;
}

$chat = $client->chatCompletion()->create([
    'model'    => 'llama3.2',
    'messages' => [
        ['role' => 'user', 'content' => 'why is the sky blue?']
    ],
    'stream' => false,
]);

echo $chat->message->content;

$chat = $client->chatCompletion()->create([
    'model'    => 'llama3.2',
    'messages' => [
        ['role' => 'user', 'content' => 'why is the sky blue?']
    ],
    'stream' => true,
]);

foreach ($chat as $chunk) {
    echo $chunk->message->content;
}

$newModel = $client->models()->create([
    'model'  => 'mario',
    'from'   => 'llama3.2',
    'system' => 'You are Mario from Super Mario Bros.',
]);

$modelList = $client->models()->list();
print_r($modelList);

$runningModels = $client->models()->listRunning();
print_r($runningModels);

$modelInfo = $client->models()->show(['model' => 'llama3.2']);
print_r($modelInfo);

$copied = $client->models()->copy([
    'source'      => 'llama3.2',
    'destination' => 'llama3-backup',
]);

$deleted = $client->models()->delete(['model' => 'llama3:13b']);

$pull = $client->models()->pull(['model' => 'llama3.2']);

$push = $client->models()->push(['model' => 'your_namespace/your_model:tag']);

$exists = $client->blobs()->exists(['digest' => 'sha256:your_digest_here']);

$blobPushed = $client->blobs()->push([
    'digest' => 'sha256:your_digest_here',
    // Additional parameters as 

$embedding = $client->embed()->create([
    'model' => 'all-minilm',
    'input' => 'Why is the sky blue?',
]);

print_r($embedding);

$version = $client->version()->show();
echo "Ollama version: " . $version->version;
bash
composer