PHP code example of vluzrmos / ollama

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

    

vluzrmos / ollama example snippets




luzrmos\Ollama\Ollama;
use Vluzrmos\Ollama\Models\Message;

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

$response = $ollama->chat([
    'model' => 'llama3.2',
    'messages' => [
        Message::system('You are a helpful assistant.'),
        Message::user('Hello!'),
    ],
]);

echo $response['message']['content'];



luzrmos\Ollama\OpenAI;
use Vluzrmos\Ollama\Models\Message;

$openai = new OpenAI('http://localhost:11434/v1', 'ollama');

$response = $openai->chat('llama3.2', [
    Message::system('You are a helpful assistant.'),
    Message::user('Hello!'),
]);

echo $response['choices'][0]['message']['content'];



use Vluzrmos\Ollama\Models\Model;

$model = (new Model('llama3.2'))
    ->setTemperature(0.8)
    ->setTopP(0.9)
    ->setNumCtx(4096)
    ->setNumPredict(512)
    ->setSeed(42)
    ->setStop(['END']);



use Vluzrmos\Ollama\Models\Message;

$messages = [
    Message::system('You are a helpful assistant.'),
    Message::user('Explain embeddings in one paragraph.'),
    Message::assistant('Embeddings are vector representations...'),
    Message::tool('{"status":"ok"}', 'my_tool'),
];



use Vluzrmos\Ollama\Ollama;

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

$response = $client->generate([
    'model' => 'llama3.2',
    'prompt' => 'Why is the sky blue?',
]);

echo $response['response'];

$response = $client->chat([
    'model' => 'llama3.2',
    'messages' => [
        Message::user('Tell me a short joke'),
    ],
]);

echo $response['message']['content'];

$response = $client->embeddings([
    'model' => 'all-minilm',
    'input' => 'Text for embedding',
]);

$vector = $response['embeddings'][0];

$models = $client->listModels();
$info = $client->showModel('llama3.2');
$version = $client->version();

$client->setApiToken('your-token');



use Vluzrmos\Ollama\OpenAI;

$client = new OpenAI('http://localhost:11434/v1', 'ollama');



use Vluzrmos\Ollama\Models\Message;
use Vluzrmos\Ollama\Models\Model;

$model = (new Model('qwen2.5:3b'))->setTemperature(0.6);

$response = $client->chat($model, [
    Message::system('You are a helpful assistant that replies in English.'),
    Message::user('Summarize the advantages of vector databases.'),
]);

echo $response['choices'][0]['message']['content'];

$response = $client->complete('llama3.2', 'Write a tagline for an AI product.', [
    'max_tokens' => 60,
    'temperature' => 0.7,
]);

echo $response['choices'][0]['text'];

$response = $client->embed('all-minilm', [
    'First text',
    'Second text',
]);

echo count($response['data'][0]['embedding']);



use Vluzrmos\Ollama\OpenAI;

$client = new OpenAI('http://localhost:11434/v1', 'ollama');

$response = $client->respond('qwen2.5:3b', 'What is the capital of France?', [
    'instructions' => 'You are a helpful assistant. Answer concisely.',
    'temperature' => 0.6,
]);

echo $response->getOutputText();

$response = $client->respond('qwen2.5:3b', 'My name is Alice.');

$followUp = $client->respond('qwen2.5:3b', 'What is my name?', [
    'previous_response_id' => $response->getId(),
]);

echo $followUp->getOutputText();

$client->respondStream('qwen2.5:3b', 'Tell me a joke.', function ($event) {
    if ($event->isTextDelta()) {
        echo $event->getText();
    }
});

use Vluzrmos\Ollama\Tools\ToolManager;

$tools = new ToolManager();
$tools->registerTool(new WeatherTool());

$response = $client->respond('qwen2.5:3b', 'What is the weather in São Paulo?', [
    'tools' => $tools->jsonSerialize(),
]);

if ($response->hasFunctionCalls()) {
    $results = $tools->executeToolCalls($response->getFunctionCalls());
    $followUpInput = [];

    foreach ($results as $result) {
        $followUpInput[] = [
            'type' => 'function_call_output',
            'call_id' => $result->getToolCallId(),
            'output' => json_encode($result->getResult()),
        ];
    }

    $finalResponse = $client->respond('qwen2.5:3b', $followUpInput, [
        'previous_response_id' => $response->getId(),
    ]);

    echo $finalResponse->getOutputText();
}

$response = $client->respond('qwen2.5:3b', 'List 3 primary colors.', [
    'text' => [
        'format' => [
            'type' => 'json_schema',
            'name' => 'primary_colors',
            'strict' => true,
            'schema' => [
                'type' => 'object',
                'properties' => [
                    'colors' => [
                        'type' => 'array',
                        'items' => ['type' => 'string'],
                    ],
                ],
                '

// Retrieve a stored response by ID
$response = $client->getResponse($responseId);
echo $response->getOutputText();

// List input items of a response
$inputItems = $client->listResponseInputItems($responseId);
echo count($inputItems->getData()) . " input items\n";

// Delete a stored response
$client->deleteResponse($responseId);

// Cancel an in-progress response
$client->cancelResponse($responseId);

$client->generate([
    'model' => 'llama3.2',
    'prompt' => 'Tell me a short story',
    'stream' => true,
], function ($chunk) {
    if (isset($chunk['response'])) {
        echo $chunk['response'];
    }
});

$client->chatStream('llama3.2', [
    Message::user('Tell me a short story'),
], function ($chunk) {
    if (isset($chunk['choices'][0]['delta']['content'])) {
        echo $chunk['choices'][0]['delta']['content'];
    }
});

$promise = $client->chatAsync('llama3.2', [
    Message::user('Hello'),
]);

$promise->then(function ($response) {
    echo $response['choices'][0]['message']['content'];
});



use Vluzrmos\Ollama\OpenAI;
use Vluzrmos\Ollama\Models\Model;

$openai = new OpenAI('http://localhost:11434/v1', 'ollama');
$model = new Model('llama3.2');

$prompts = [
    'What is the capital of France?',
    'Who won the FIFA World Cup in 2018?',
    'What is the tallest mountain in the world?',
];

function make_requests(OpenAI $openai, $model, array $prompts)
{
    foreach ($prompts as $index => $prompt) {
        yield $index => function () use ($openai, $model, $prompt) {
            return $openai->completeAsync($model, $prompt, [
                'max_tokens' => 100,
            ]);
        };
    }
}

$pool = $openai->pool(make_requests($openai, $model, $prompts), [
    'concurrency' => 4,
    'fulfilled' => function ($response, $index) use ($prompts) {
        echo "Prompt: '{$prompts[$index]}'\n";
        echo "Answer: " . $response['choices'][0]['text'] . "\n\n";
    },
    'rejected' => function ($reason, $index) use ($prompts) {
        echo "Error for: '{$prompts[$index]}'\n";
        echo "Reason: " . $reason->getMessage() . "\n\n";
    },
]);

$pool->promise()->wait();



use Vluzrmos\Ollama\Models\Message;
use Vluzrmos\Ollama\Utils\ImageHelper;

$image = ImageHelper::encodeImageUrl(__DIR__ . '/sample.jpg');

$response = $client->chat('qwen2.5vl:3b', [
    Message::image('Describe this image.', $image),
]);

$response = $client->chat('llama3.2', [
    Message::system('Always respond with valid JSON.'),
    Message::user('List 3 primary colors'),
], [
    'response_format' => [
        'type' => 'json_schema',
        'json_schema' => [
            'name' => 'primary_colors',
            'strict' => true,
            'schema' => [
                'type' => 'object',
                'properties' => [
                    'colors' => [
                        'type' => 'array',
                        'items' => ['type' => 'string'],
                    ],
                ],
                '



use Vluzrmos\Ollama\Exceptions\ToolExecutionException;
use Vluzrmos\Ollama\Tools\AbstractTool;

class CalculatorTool extends AbstractTool
{
    public function getName()
    {
        return 'calculator';
    }

    public function getDescription()
    {
        return 'Performs basic mathematical operations';
    }

    public function getParametersSchema()
    {
        return [
            'type' => 'object',
            'properties' => [
                'operation' => [
                    'type' => 'string',
                    'enum' => ['add', 'subtract', 'multiply', 'divide'],
                ],
                'a' => ['type' => 'number'],
                'b' => ['type' => 'number'],
            ],
            '



use Vluzrmos\Ollama\Tools\ToolManager;

$toolManager = new ToolManager();
$toolManager->registerTool(new CalculatorTool());

$toolManager = new ToolManager();
$toolManager->registerTool(new CalculatorTool());

$messages = [
    Message::user('What is 15 + 27?'),
];

$response = $client->chatCompletions([
    'model' => 'llama3.2',
    'messages' => $messages,
    'tools' => $toolManager,
]);

$toolCalls = $response['choices'][0]['message']['tool_calls'];
$results = $toolManager->executeToolCalls($toolCalls);
$toolMessages = $toolManager->toolCallResultsToMessages($results);

$finalResponse = $client->chatCompletions([
    'model' => 'llama3.2',
    'messages' => array_merge($messages, [$response['choices'][0]['message']], $toolMessages),
]);



use Vluzrmos\Ollama\Chat\Chat;
use Vluzrmos\Ollama\Chat\OpenAIClientAdapter;
use Vluzrmos\Ollama\Models\Message;
use Vluzrmos\Ollama\Models\Model;
use Vluzrmos\Ollama\OpenAI;

$chat = new Chat(new OpenAIClientAdapter(new OpenAI()));

$chat->withModel(new Model('qwen2.5:3b'));
$chat->addMessage(Message::system('You are a helpful assistant.'));

$response = $chat->chat([
    Message::user('What time is it in UTC?'),
]);



use Vluzrmos\Ollama\Agents\Agent;
use Vluzrmos\Ollama\Agents\OpenAIClientAdapter;
use Vluzrmos\Ollama\OpenAI;
use Vluzrmos\Ollama\Tools\TimeTool;

$agent = new Agent(
    'Assistant',
    new OpenAIClientAdapter(new OpenAI()),
    'qwen2.5:3b',
    'You are a helpful assistant.',
    'General purpose assistant',
    [new TimeTool()],
    ['temperature' => 0.4]
);

$response = $agent->processQuery('What time is it in UTC?');



use Vluzrmos\Ollama\Ollama;
use Vluzrmos\Ollama\OpenAI;

$ollama = new Ollama('http://localhost:11434', [
    'timeout' => 60,
    'connect_timeout' => 10,
    'verify_ssl' => false,
]);

$openai = new OpenAI('http://localhost:11434/v1', 'ollama', [
    'timeout' => 120,
]);



use Vluzrmos\Ollama\Exceptions\OllamaException;
use Vluzrmos\Ollama\Models\Message;

try {
    $response = $ollama->chat([
        'model' => 'non-existent-model',
        'messages' => [Message::user('Hello')],
    ]);
} catch (OllamaException $e) {
    echo $e->getMessage();
}
bash
docker build -t ollama-php .
docker run -it --rm ollama-php
bash
docker run -it --rm ollama-php php examples/basic_usage.php
bash
docker run -it --rm -v /path/to/your/project:/app ollama-php php your-script.php