Skip to content

[Platform] Use JSON Path to convert responses #136

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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
1 change: 1 addition & 0 deletions examples/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"symfony/event-dispatcher": "^6.4|^7.0",
"symfony/filesystem": "^6.4|^7.0",
"symfony/finder": "^6.4|^7.0",
"symfony/json-path": "7.3.*",
"symfony/process": "^6.4|^7.0",
"symfony/var-dumper": "^6.4|^7.0"
},
Expand Down
4 changes: 2 additions & 2 deletions examples/gemini/toolcall.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('GEMINI_API_KEY'), http_client());
$llm = new Gemini(Gemini::GEMINI_2_FLASH);
$model = new Gemini(Gemini::GEMINI_2_FLASH);

$toolbox = new Toolbox([new Clock()], logger: logger());
$processor = new AgentProcessor($toolbox);
$agent = new Agent($platform, $llm, [$processor], [$processor], logger());
$agent = new Agent($platform, $model, [$processor], [$processor], logger());

$messages = new MessageBag(Message::ofUser('What time is it?'));
$result = $agent->call($messages);
Expand Down
35 changes: 35 additions & 0 deletions examples/ollama/stream.php
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?'),
);
$result = $agent->call($messages, [
'stream' => true,
]);

foreach ($result->getContent() as $word) {
echo $word;
}
echo \PHP_EOL;
37 changes: 37 additions & 0 deletions examples/openrouter/stream-gemini.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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\OpenRouter\PlatformFactory;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Message\MessageBag;
use Symfony\AI\Platform\Model;

require_once dirname(__DIR__).'/bootstrap.php';

$platform = PlatformFactory::create(env('OPENROUTER_KEY'), http_client());
// In case free is running into 429 rate limit errors, you can use the paid model:
// $model = new Model('google/gemini-2.0-flash-lite-001');
$model = new Model('google/gemini-2.0-flash-exp:free');

$agent = new Agent($platform, $model, logger: logger());
$messages = new MessageBag(
Message::forSystem('You are a helpful assistant and explain your answer lengthy.'),
Message::ofUser('Tina has one brother and one sister. How many sisters do Tina\'s siblings have?'),
);
$result = $agent->call($messages, [
'stream' => true,
]);

foreach ($result->getContent() as $word) {
echo $word;
}
echo \PHP_EOL;
10 changes: 10 additions & 0 deletions src/agent/src/Toolbox/AgentProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Message\AssistantMessage;
use Symfony\AI\Platform\Message\Message;
use Symfony\AI\Platform\Result\ChoiceResult;
use Symfony\AI\Platform\Result\ResultInterface;
use Symfony\AI\Platform\Result\StreamResult as GenericStreamResponse;
use Symfony\AI\Platform\Result\ToolCallResult;
Expand Down Expand Up @@ -76,6 +77,15 @@ public function processOutput(Output $output): void
return;
}

// Promote ToolCallResult from collection of choices
if ($output->result instanceof ChoiceResult) {
foreach ($output->result->getContent() as $result) {
if ($result instanceof ToolCallResult) {
$output->result = $result;
}
}
}

if (!$output->result instanceof ToolCallResult) {
return;
}
Expand Down
1 change: 1 addition & 0 deletions src/platform/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"psr/log": "^3.0",
"symfony/clock": "^6.4 || ^7.1",
"symfony/http-client": "^6.4 || ^7.1",
"symfony/json-path": "7.3.*",
"symfony/property-access": "^6.4 || ^7.1",
"symfony/property-info": "^6.4 || ^7.1",
"symfony/serializer": "^6.4 || ^7.1",
Expand Down
1 change: 1 addition & 0 deletions src/platform/phpstan.dist.neon
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ parameters:
paths:
- src/
- tests/
treatPhpDocTypesAsCertain: false
ignoreErrors:
-
message: "#^Method .*::test.*\\(\\) has no return type specified\\.$#"
Expand Down
4 changes: 1 addition & 3 deletions src/platform/src/Bridge/Albert/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\AI\Platform\Bridge\Albert;

use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Exception\InvalidArgumentException;
use Symfony\AI\Platform\Platform;
Expand Down Expand Up @@ -40,7 +38,7 @@ public static function create(
new GptModelClient($httpClient, $apiKey, $baseUrl),
new EmbeddingsModelClient($httpClient, $apiKey, $baseUrl),
],
[new Gpt\ResultConverter(), new Embeddings\ResultConverter()],
[Contract\ResultConverter::create()],
Contract::create(),
);
}
Expand Down
18 changes: 17 additions & 1 deletion src/platform/src/Bridge/Anthropic/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

use Symfony\AI\Platform\Bridge\Anthropic\Contract\AnthropicContract;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Contract\ResultExtractor\StreamResultExtractor;
use Symfony\AI\Platform\Contract\ResultExtractor\TextResultExtractor;
use Symfony\AI\Platform\Contract\ResultExtractor\ToolCallResultExtractor;
use Symfony\AI\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -33,7 +36,20 @@ public static function create(

return new Platform(
[new ModelClient($httpClient, $apiKey, $version)],
[new ResultConverter()],
[Contract\ResultConverter::create([
new TextResultExtractor('$.content[[email protected] == "text"].text'),
new ToolCallResultExtractor(
'$.content[[email protected] == "tool_use"]',
'$.content[[email protected] == "tool_use"].id',
'$.content[[email protected] == "tool_use"].name',
'$.content[[email protected] == "tool_use"].input',
),
new StreamResultExtractor(
'$.delta.text',
'$.delta[[email protected] == "tool_use"]',
'$.delta.stop_reason',
),
])],
$contract ?? AnthropicContract::create(),
);
}
Expand Down
90 changes: 0 additions & 90 deletions src/platform/src/Bridge/Anthropic/ResultConverter.php

This file was deleted.

41 changes: 0 additions & 41 deletions src/platform/src/Bridge/Azure/Meta/LlamaResultConverter.php

This file was deleted.

2 changes: 1 addition & 1 deletion src/platform/src/Bridge/Azure/Meta/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public static function create(
): Platform {
$modelClient = new LlamaModelClient($httpClient ?? HttpClient::create(), $baseUrl, $apiKey);

return new Platform([$modelClient], [new LlamaResultConverter()], $contract);
return new Platform([$modelClient], [Contract\ResultConverter::create()], $contract);
}
}
4 changes: 1 addition & 3 deletions src/platform/src/Bridge/Azure/OpenAi/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@

namespace Symfony\AI\Platform\Bridge\Azure\OpenAi;

use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
use Symfony\AI\Platform\Bridge\OpenAi\Whisper;
use Symfony\AI\Platform\Bridge\OpenAi\Whisper\AudioNormalizer;
use Symfony\AI\Platform\Contract;
Expand Down Expand Up @@ -41,7 +39,7 @@ public static function create(

return new Platform(
[$gptModelClient, $embeddingsModelClient, $whisperModelClient],
[new Gpt\ResultConverter(), new Embeddings\ResultConverter(), new Whisper\ResultConverter()],
[new Whisper\ResultConverter(), Contract\ResultConverter::create()],
$contract ?? Contract::create(new AudioNormalizer()),
);
}
Expand Down
47 changes: 0 additions & 47 deletions src/platform/src/Bridge/Gemini/Embeddings/ResultConverter.php

This file was deleted.

Loading