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'];
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'];
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();
}
// 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'];
}
});
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),
]);
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,
]);