-
-
Notifications
You must be signed in to change notification settings - Fork 57
[Platform][Ollama] Support streaming output #339
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b1fee7c
f7939d6
13236b8
689b5d3
f4eecf7
40fdf79
115fb0d
60c313a
1f30115
7e6797f
ddb7970
726286a
807b7ec
b044ea1
cd1123e
4cc0bac
1e62063
438534b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,35 @@ | ||||||||
<?php | ||||||||
|
||||||||
/* | ||||||||
* This file is part of the Symfony package. | ||||||||
* | ||||||||
* (c) Fabien Potencier <[email protected]> | ||||||||
* | ||||||||
* For the full copyright and license information, please view the LICENSE | ||||||||
* file that was distributed with this source code. | ||||||||
*/ | ||||||||
|
||||||||
use Symfony\AI\Agent\Agent; | ||||||||
use Symfony\AI\Platform\Bridge\Ollama\Ollama; | ||||||||
use Symfony\AI\Platform\Bridge\Ollama\PlatformFactory; | ||||||||
use Symfony\AI\Platform\Message\Message; | ||||||||
use Symfony\AI\Platform\Message\MessageBag; | ||||||||
|
||||||||
require_once dirname(__DIR__).'/bootstrap.php'; | ||||||||
|
||||||||
$platform = PlatformFactory::create(env('OLLAMA_HOST_URL'), http_client()); | ||||||||
$model = new Ollama(); | ||||||||
|
||||||||
$agent = new Agent($platform, $model, logger: logger()); | ||||||||
$messages = new MessageBag( | ||||||||
Message::forSystem('You are a helpful assistant.'), | ||||||||
Message::ofUser('Tina has one brother and one sister. How many sisters do Tina\'s siblings have?'), | ||||||||
); | ||||||||
|
||||||||
// Stream the response | ||||||||
$result = $agent->call($messages, ['stream' => true]); | ||||||||
|
||||||||
// Emit each chunk as it is received | ||||||||
foreach ($result as $chunk) { | ||||||||
echo $chunk->getContent(); | ||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\AI\Platform\Bridge\Ollama; | ||
|
||
/** | ||
* @author Shaun Johnston <[email protected]> | ||
*/ | ||
final readonly class OllamaMessageChunk | ||
logicalor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/** | ||
* @param array<string, mixed> $message | ||
*/ | ||
public function __construct( | ||
public readonly string $model, | ||
public readonly \DateTimeImmutable $created_at, | ||
public readonly array $message, | ||
public readonly bool $done, | ||
) { | ||
} | ||
|
||
public function __toString(): string | ||
{ | ||
// Return the assistant's message content if available | ||
return $this->message['content'] ?? ''; | ||
} | ||
|
||
public static function fromJsonString(string $json): ?self | ||
{ | ||
$data = json_decode($json, true); | ||
if (!\is_array($data)) { | ||
return null; | ||
} | ||
Comment on lines
+38
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please move this to the result converter - it's their job to convert to data. fine with having the value object, but let's keep it simple than 👍 |
||
|
||
return new self( | ||
$data['model'] ?? '', | ||
new \DateTimeImmutable($data['created_at'] ?? ''), | ||
$data['message'] ?? [], | ||
$data['done'] ?? false | ||
); | ||
} | ||
logicalor marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public function getContent(): ?string | ||
{ | ||
return $this->message['content'] ?? null; | ||
} | ||
|
||
public function getRole(): ?string | ||
{ | ||
return $this->message['role'] ?? null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,12 +15,17 @@ | |
use Symfony\AI\Platform\Model; | ||
use Symfony\AI\Platform\Result\RawResultInterface; | ||
use Symfony\AI\Platform\Result\ResultInterface; | ||
use Symfony\AI\Platform\Result\StreamResult; | ||
use Symfony\AI\Platform\Result\TextResult; | ||
use Symfony\AI\Platform\Result\ToolCall; | ||
use Symfony\AI\Platform\Result\ToolCallResult; | ||
use Symfony\AI\Platform\Result\VectorResult; | ||
use Symfony\AI\Platform\ResultConverterInterface; | ||
use Symfony\AI\Platform\Vector\Vector; | ||
use Symfony\Component\HttpClient\Chunk\FirstChunk; | ||
use Symfony\Component\HttpClient\Chunk\LastChunk; | ||
use Symfony\Component\HttpClient\EventSourceHttpClient; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
/** | ||
* @author Christopher Hertel <[email protected]> | ||
|
@@ -34,6 +39,10 @@ public function supports(Model $model): bool | |
|
||
public function convert(RawResultInterface $result, array $options = []): ResultInterface | ||
{ | ||
if ($options['stream'] ?? false) { | ||
return new StreamResult($this->convertStream($result->getObject())); | ||
} | ||
|
||
$data = $result->getData(); | ||
|
||
return \array_key_exists('embeddings', $data) | ||
|
@@ -83,4 +92,18 @@ public function doConvertEmbeddings(array $data): ResultInterface | |
), | ||
); | ||
} | ||
|
||
private function convertStream(ResponseInterface $result): \Generator | ||
{ | ||
foreach ((new EventSourceHttpClient())->stream($result) as $chunk) { | ||
if ($chunk instanceof FirstChunk || $chunk instanceof LastChunk) { | ||
continue; | ||
} | ||
|
||
$msg = OllamaMessageChunk::fromJsonString($chunk->getContent()); | ||
if ($msg) { | ||
yield $msg; | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Question for @chr-hertel (not relevant for the review), isn't the same situation that we're facing in #324? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not since this is only working on the response in the ResultConverter, which is only called when finally fetching the content. In #324 the |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's call the file only
stream.php
- it is more consistent with the other stream examples, andollama
is basically the folder already