PHP code example of hanwoolderink / ollama-php-client

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

    

hanwoolderink / ollama-php-client example snippets


use Hanwoolderink\Ollama\Ollama;
use Hanwoolderink\Ollama\Dtos\Message;

$ollama = new Ollama();

$response = $ollama->chat()->create(
  model: 'llama3.1:latest', 
  message: new Message('Why is the sky blue?')
);

echo $response->message->content;

use Hanwoolderink\Ollama\Ollama;
use Hanwoolderink\Ollama\Dtos\Message;

$ollama = new Ollama();

$response = $ollama->chat()->stream(
    model: 'llama3.1:latest',
    messages: [
        new Message('Why does the sky appear more blue in the morning and more red in the evening?')
    ],
);

foreach ($response as $streamResponse) {
    // update storage, socket, etc.. This prints to cli
    $stream = fopen('php://stdout', 'w');
    fwrite($stream, $streamResponse->message->content);
    fclose($stream);
}
bash
composer