PHP code example of halilcosdu / laravel-ollama

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

use HalilCosdu\Ollama\Facades\Ollama;

Route::get('/explain', function () {
    return response()->stream(function () {
        foreach (Ollama::prompt('Explain dependency injection.')->streamAsk() as $chunk) {
            echo $chunk['response'] ?? '';
            ob_flush();
            flush();
        }
    }, headers: ['Content-Type' => 'text/plain; charset=UTF-8']);
});

$response = Ollama::model('gemma3')->stream(false)->chat([
    ['role' => 'system', 'content' => 'You are a helpful Laravel mentor.'],
    ['role' => 'user', 'content' => 'What is route model binding?'],
]);

$text = $response['message']['content'];

$schema = [
    'type' => 'object',
    'properties' => [
        'name' => ['type' => 'string'],
        'frameworks' => ['type' => 'array', 'items' => ['type' => 'string']],
    ],
    'n_decode($response['response'], true, flags: JSON_THROW_ON_ERROR);

$tools = [[
    'type' => 'function',
    'function' => [
        'name' => 'get_weather',
        'description' => 'Get the current weather for a city',
        'parameters' => [
            'type' => 'object',
            'properties' => ['city' => ['type' => 'string']],
            '

$response = Ollama::model('gemma3')
    ->prompt('Describe this image.')
    ->image(storage_path('app/photo.jpg'))
    ->stream(false)
    ->ask();

$one = Ollama::model('nomic-embed-text')->embed('Laravel is expressive.');
$batch = Ollama::model('nomic-embed-text')->embed(['first document', 'second document']);

$localModels = Ollama::models();
$runningModels = Ollama::ps();
$serverVersion = Ollama::version();
$details = Ollama::model('gemma3')->show();

Ollama::model('gemma3')->pull();
Ollama::model('gemma3')->copy('gemma3-backup');
Ollama::model('gemma3-backup')->delete();
Ollama::model('my-model')->create("FROM gemma3\nSYSTEM You are concise.");
Ollama::model('myorg/my-model')->push();
bash
php artisan vendor:publish --tag=ollama-config