evowareio / ollama-php

A wrapper library for interacting with Ollama API in a simplified, object driven manner.
10 stars 0 forks source link

'Handling streaming responses.' #3

Open davidbuzz opened 1 week ago

davidbuzz commented 1 week ago

there's an implementation of using json-streaming over here: https://github.com/jdecool/ollama-php-client/blob/040ed0803e2b73bbc5e14b441e59c55bacfd0559/src/Client.php#L33 https://github.com/jdecool/ollama-php-client/blob/040ed0803e2b73bbc5e14b441e59c55bacfd0559/src/Client.php#L147 https://github.com/jdecool/ollama-php-client/blob/040ed0803e2b73bbc5e14b441e59c55bacfd0559/src/Http.php#L33

I'm using their package, but would probably have used yours if it had streaming..

<?php 
require __DIR__.'/vendor/autoload.php';

use JDecool\OllamaClient\ClientBuilder;
use JDecool\OllamaClient\Client\Message;
use JDecool\OllamaClient\Client\Request\ChatRequest;

$builder = new ClientBuilder();
$client = $builder->create('http://localhost:11434');

$request = new ChatRequest('phi3:mini', [
    new Message('user', 'Why is the sky blue?'),
]);

// async
foreach ($client->chatStream($request) as $chunk) {
    //var_dump($chunk);
    $msg = $chunk->message;
    echo $msg->content;
}
print "\n";