Skip to content
Draft
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
33 changes: 33 additions & 0 deletions examples/ollama/agent-with-token-processor-as-stream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\PlatformFactory;
use Symfony\AI\Platform\Bridge\Ollama\TokenOutputProcessor;
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());

$agent = new Agent($platform, env('OLLAMA_LLM'), outputProcessors: [new TokenOutputProcessor()]);
$answer = $agent->call(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' => true]);

foreach ($answer->getContent() as $content) {
dd($content);
echo $content . \PHP_EOL;
}


28 changes: 28 additions & 0 deletions examples/ollama/agent-with-token-processor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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\PlatformFactory;
use Symfony\AI\Platform\Bridge\Ollama\TokenOutputProcessor;
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());

$agent = new Agent($platform, env('OLLAMA_LLM'), outputProcessors: [new TokenOutputProcessor()]);
$answer = $agent->call(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?'),
));

echo $answer->getContent();
57 changes: 57 additions & 0 deletions src/platform/src/Bridge/Ollama/TokenOutputProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?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;

use Symfony\AI\Agent\Output;
use Symfony\AI\Agent\OutputProcessorInterface;
use Symfony\AI\Platform\Metadata\TokenUsage;
use Symfony\AI\Platform\Result\StreamResult;
use Symfony\Contracts\HttpClient\ResponseInterface;

/**
* @author Guillaume Loulier <[email protected]>
*/
final class TokenOutputProcessor implements OutputProcessorInterface

Check failure on line 23 in src/platform/src/Bridge/Ollama/TokenOutputProcessor.php

View workflow job for this annotation

GitHub Actions / deptrac

Symfony\AI\Platform\Bridge\Ollama\TokenOutputProcessor must not depend on Symfony\AI\Agent\OutputProcessorInterface (PlatformComponent on AgentComponent)
{
public function processOutput(Output $output): void

Check failure on line 25 in src/platform/src/Bridge/Ollama/TokenOutputProcessor.php

View workflow job for this annotation

GitHub Actions / deptrac

Symfony\AI\Platform\Bridge\Ollama\TokenOutputProcessor must not depend on Symfony\AI\Agent\Output (PlatformComponent on AgentComponent)
{
$result = $output->getResult();
$currentOutputMetadata = $result->getMetadata();

if ($result instanceof StreamResult) {
foreach ($result->getContent() as $chunk) {
if ($chunk instanceof OllamaMessageChunk && !$chunk->isDone()) {
continue;
}

$currentOutputMetadata->add('token_usage', new TokenUsage(
promptTokens: $chunk->raw['prompt_eval_count'],
completionTokens: $chunk->raw['eval_count']
));
}

return;
}

$rawResponse = $result->getRawResult()?->getObject();
if (!$rawResponse instanceof ResponseInterface) {
return;
}

$payload = $rawResponse->toArray();

$currentOutputMetadata->add('token_usage', new TokenUsage(
promptTokens: $payload['prompt_eval_count'],
completionTokens: $payload['eval_count']
));
}
}
18 changes: 18 additions & 0 deletions src/platform/tests/Bridge/Ollama/TokenOutputProcessorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?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\Tests\Bridge\Ollama;

use PHPUnit\Framework\TestCase;

final class TokenOutputProcessorTest extends TestCase
{
}
Loading