-
-
Notifications
You must be signed in to change notification settings - Fork 58
[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
Open
logicalor
wants to merge
23
commits into
symfony:main
Choose a base branch
from
logicalor:338-ollama-streaming
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+201
−0
Open
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
b1fee7c
Support streaming output from Ollama
logicalor f7939d6
Changelog
logicalor 13236b8
phpstan fix
logicalor 689b5d3
phpcs fixes
logicalor f4eecf7
phpcs fix
logicalor 40fdf79
phpcs fix
logicalor 115fb0d
phpcs fix
logicalor 60c313a
getters
logicalor 1f30115
getters
logicalor 7e6797f
streaming example
logicalor ddb7970
final readonly
logicalor 726286a
cleanup
logicalor 807b7ec
cleanup
logicalor b044ea1
cleanup
logicalor cd1123e
tests
logicalor 4cc0bac
phpcs
logicalor 1e62063
phpcs
logicalor 438534b
phpcs
logicalor a1e3188
Update examples/ollama/chat-ollama-streaming.php
logicalor 26aa68d
cleanup
logicalor a0c9c31
cleanup
logicalor 42d002b
cleanup
logicalor e5ce09a
revert
logicalor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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->getContent() as $chunk) { | ||
echo $chunk->getContent(); | ||
} | ||
echo \PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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 <shaun@snj.au> | ||
*/ | ||
final readonly class OllamaMessageChunk | ||
{ | ||
/** | ||
* @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 function getContent(): ?string | ||
{ | ||
return $this->message['content'] ?? null; | ||
} | ||
|
||
public function getRole(): ?string | ||
{ | ||
return $this->message['role'] ?? null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.