1. Go to this page and download the library: Download halilcosdu/laravel-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/ */
halilcosdu / laravel-ollama example snippets
use HalilCosdu\Ollama\Facades\Ollama;
$answer = Ollama::model('gemma3')
->agent('You are a concise Laravel expert.')
->prompt('Explain service containers in one sentence.')
->stream(false)
->ask();
$response = Ollama::model('gemma3')
->agent('Answer as a senior PHP engineer.')
->prompt('When should I use a readonly class?')
->options(['temperature' => 0.2])
->keepAlive('10m')
->stream(false)
->ask();
$text = $response['response'];
foreach (Ollama::model('gemma3')->prompt('Write a short story.')->streamAsk() as $chunk) {
echo $chunk['response'] ?? '';
}
$messages = [['role' => 'user', 'content' => 'Teach me about Laravel queues.']];
foreach (Ollama::model('gemma3')->streamChat($messages) as $chunk) {
echo data_get($chunk, 'message.content', '');
}