Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions examples/ollama/stream-toolcall.php
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\Agent\Toolbox\AgentProcessor;
use Symfony\AI\Agent\Toolbox\Tool\Clock;
use Symfony\AI\Agent\Toolbox\Toolbox;
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());

$toolbox = new Toolbox([new Clock()], logger: logger());
$processor = new AgentProcessor($toolbox);
$agent = new Agent($platform, env('OLLAMA_LLM'), [$processor], [$processor]);

$messages = new MessageBag(Message::ofUser('What time is it?'));

$result = $agent->call($messages, ['stream' => true]);

foreach ($result->getContent() as $chunk) {
echo $chunk->getContent();
}

echo \PHP_EOL;
44 changes: 44 additions & 0 deletions src/platform/src/Bridge/Ollama/OllamaResultConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public function doConvertEmbeddings(array $data): ResultInterface

private function convertStream(ResponseInterface $result): \Generator
{
$toolCalls = [];
foreach ((new EventSourceHttpClient())->stream($result) as $chunk) {
if ($chunk instanceof FirstChunk || $chunk instanceof LastChunk) {
continue;
Expand All @@ -106,6 +107,14 @@ private function convertStream(ResponseInterface $result): \Generator
throw new RuntimeException('Failed to decode JSON: '.$e->getMessage());
}

if ($this->streamIsToolCall($data)) {
$toolCalls = $this->convertStreamToToolCalls($toolCalls, $data);
}

if ([] !== $toolCalls && $this->isToolCallsStreamFinished($data)) {
yield new ToolCallResult(...$toolCalls);
}

yield new OllamaMessageChunk(
$data['model'],
new \DateTimeImmutable($data['created_at']),
Expand All @@ -114,4 +123,39 @@ private function convertStream(ResponseInterface $result): \Generator
);
}
}

/**
* @param array<string, mixed> $toolCalls
* @param array<string, mixed> $data
*
* @return array<ToolCall>
*/
private function convertStreamToToolCalls(array $toolCalls, array $data): array
{
if (!isset($data['message']['tool_calls'])) {
return $toolCalls;
}

foreach ($data['message']['tool_calls'] ?? [] as $id => $toolCall) {
$toolCalls[] = new ToolCall($id, $toolCall['function']['name'], $toolCall['function']['arguments']);
}

return $toolCalls;
}

/**
* @param array<string, mixed> $data
*/
private function streamIsToolCall(array $data): bool
{
return isset($data['message']['tool_calls']);
}

/**
* @param array<string, mixed> $data^
*/
private function isToolCallsStreamFinished(array $data): bool
{
return isset($data['done']) && true === $data['done'];
}
}