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: simplify-choice-handling
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
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 <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\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;
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
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.

8 changes: 5 additions & 3 deletions src/platform/src/Bridge/Gemini/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

use Symfony\AI\Platform\Bridge\Gemini\Contract\GeminiContract;
use Symfony\AI\Platform\Bridge\Gemini\Embeddings\ModelClient as EmbeddingsModelClient;
use Symfony\AI\Platform\Bridge\Gemini\Embeddings\ResultConverter as EmbeddingsResultConverter;
use Symfony\AI\Platform\Bridge\Gemini\Gemini\ModelClient as GeminiModelClient;
use Symfony\AI\Platform\Bridge\Gemini\Gemini\ResultConverter as GeminiResultConverter;
use Symfony\AI\Platform\Contract;
use Symfony\AI\Platform\Contract\ResultConverter;
use Symfony\AI\Platform\Contract\ResultExtractor\VectorResultExtractor;
use Symfony\AI\Platform\Platform;
use Symfony\Component\HttpClient\EventSourceHttpClient;
use Symfony\Contracts\HttpClient\HttpClientInterface;
Expand All @@ -36,7 +36,9 @@ public static function create(

return new Platform(
[new EmbeddingsModelClient($httpClient, $apiKey), new GeminiModelClient($httpClient, $apiKey)],
[new EmbeddingsResultConverter(), new GeminiResultConverter()],
[new Gemini\ResultConverter(), ResultConverter::create([
new VectorResultExtractor('$.embeddings[*].values'),
])],
$contract ?? GeminiContract::create(),
);
}
Expand Down

This file was deleted.

48 changes: 0 additions & 48 deletions src/platform/src/Bridge/LmStudio/Embeddings/ResultConverter.php

This file was deleted.

12 changes: 4 additions & 8 deletions src/platform/src/Bridge/LmStudio/PlatformFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,9 @@ public static function create(
$httpClient = $httpClient instanceof EventSourceHttpClient ? $httpClient : new EventSourceHttpClient($httpClient);

return new Platform(
[
new ModelClient($httpClient, $hostUrl),
new Completions\ModelClient($httpClient, $hostUrl),
],
[
new Embeddings\ResultConverter(),
new Completions\ResultConverter(),
], $contract);
[new ModelClient($httpClient, $hostUrl), new Completions\ModelClient($httpClient, $hostUrl)],
[Contract\ResultConverter::create()],
$contract,
);
}
}
54 changes: 0 additions & 54 deletions src/platform/src/Bridge/Mistral/Embeddings/ResultConverter.php

This file was deleted.

Loading
Loading