Skip to content

Commit 435d8f6

Browse files
committed
Use JSON Path to convert responses
1 parent 61548ce commit 435d8f6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1978
-1211
lines changed

examples/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"symfony/event-dispatcher": "^6.4|^7.0",
2323
"symfony/filesystem": "^6.4|^7.0",
2424
"symfony/finder": "^6.4|^7.0",
25+
"symfony/json-path": "7.3.*",
2526
"symfony/process": "^6.4|^7.0",
2627
"symfony/var-dumper": "^6.4|^7.0"
2728
},

examples/openrouter/stream-gemini.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\AI\Agent\Agent;
13+
use Symfony\AI\Platform\Bridge\OpenRouter\PlatformFactory;
14+
use Symfony\AI\Platform\Message\Message;
15+
use Symfony\AI\Platform\Message\MessageBag;
16+
use Symfony\AI\Platform\Model;
17+
18+
require_once dirname(__DIR__).'/bootstrap.php';
19+
20+
$platform = PlatformFactory::create(env('OPENROUTER_KEY'), http_client());
21+
// In case free is running into 429 rate limit errors, you can use the paid model:
22+
// $model = new Model('google/gemini-2.0-flash-lite-001');
23+
$model = new Model('google/gemini-2.0-flash-exp:free');
24+
25+
$agent = new Agent($platform, $model, logger: logger());
26+
$messages = new MessageBag(
27+
Message::forSystem('You are a helpful assistant and explain your answer lengthy.'),
28+
Message::ofUser('Tina has one brother and one sister. How many sisters do Tina\'s siblings have?'),
29+
);
30+
$result = $agent->call($messages, [
31+
'stream' => true,
32+
]);
33+
34+
foreach ($result->getContent() as $word) {
35+
echo $word;
36+
}
37+
echo \PHP_EOL;

src/platform/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"psr/log": "^3.0",
2929
"symfony/clock": "^6.4 || ^7.1",
3030
"symfony/http-client": "^6.4 || ^7.1",
31+
"symfony/json-path": "7.3.*",
3132
"symfony/property-access": "^6.4 || ^7.1",
3233
"symfony/property-info": "^6.4 || ^7.1",
3334
"symfony/serializer": "^6.4 || ^7.1",

src/platform/phpstan.dist.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ parameters:
77
paths:
88
- src/
99
- tests/
10+
treatPhpDocTypesAsCertain: false
1011
ignoreErrors:
1112
-
1213
message: "#^Method .*::test.*\\(\\) has no return type specified\\.$#"

src/platform/src/Bridge/Albert/PlatformFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public static function create(
4040
new GptModelClient($httpClient, $apiKey, $baseUrl),
4141
new EmbeddingsModelClient($httpClient, $apiKey, $baseUrl),
4242
],
43-
[new Gpt\ResultConverter(), new Embeddings\ResultConverter()],
43+
[Contract\ResultConverter::create()],
4444
Contract::create(),
4545
);
4646
}

src/platform/src/Bridge/Azure/Meta/LlamaResultConverter.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/platform/src/Bridge/Azure/Meta/PlatformFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@ public static function create(
3030
): Platform {
3131
$modelClient = new LlamaModelClient($httpClient ?? HttpClient::create(), $baseUrl, $apiKey);
3232

33-
return new Platform([$modelClient], [new LlamaResultConverter()], $contract);
33+
return new Platform([$modelClient], [Contract\ResultConverter::create()], $contract);
3434
}
3535
}

src/platform/src/Bridge/Azure/OpenAi/PlatformFactory.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

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

14-
use Symfony\AI\Platform\Bridge\OpenAi\Embeddings;
15-
use Symfony\AI\Platform\Bridge\OpenAi\Gpt;
1614
use Symfony\AI\Platform\Bridge\OpenAi\Whisper;
1715
use Symfony\AI\Platform\Bridge\OpenAi\Whisper\AudioNormalizer;
1816
use Symfony\AI\Platform\Contract;
@@ -41,7 +39,7 @@ public static function create(
4139

4240
return new Platform(
4341
[$gptModelClient, $embeddingsModelClient, $whisperModelClient],
44-
[new Gpt\ResultConverter(), new Embeddings\ResultConverter(), new Whisper\ResultConverter()],
42+
[new Whisper\ResultConverter(), Contract\ResultConverter::create()],
4543
$contract ?? Contract::create(new AudioNormalizer()),
4644
);
4745
}

src/platform/src/Bridge/Gemini/Embeddings/ResultConverter.php

Lines changed: 0 additions & 47 deletions
This file was deleted.

src/platform/src/Bridge/Gemini/PlatformFactory.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
use Symfony\AI\Platform\Bridge\Gemini\Contract\GeminiContract;
1515
use Symfony\AI\Platform\Bridge\Gemini\Embeddings\ModelClient as EmbeddingsModelClient;
16-
use Symfony\AI\Platform\Bridge\Gemini\Embeddings\ResultConverter as EmbeddingsResultConverter;
1716
use Symfony\AI\Platform\Bridge\Gemini\Gemini\ModelClient as GeminiModelClient;
18-
use Symfony\AI\Platform\Bridge\Gemini\Gemini\ResultConverter as GeminiResultConverter;
1917
use Symfony\AI\Platform\Contract;
18+
use Symfony\AI\Platform\Contract\ResultConverter;
19+
use Symfony\AI\Platform\Contract\ResultExtractor\VectorResultExtractor;
2020
use Symfony\AI\Platform\Platform;
2121
use Symfony\Component\HttpClient\EventSourceHttpClient;
2222
use Symfony\Contracts\HttpClient\HttpClientInterface;
@@ -36,7 +36,9 @@ public static function create(
3636

3737
return new Platform(
3838
[new EmbeddingsModelClient($httpClient, $apiKey), new GeminiModelClient($httpClient, $apiKey)],
39-
[new EmbeddingsResultConverter(), new GeminiResultConverter()],
39+
[new Gemini\ResultConverter(), ResultConverter::create([
40+
new VectorResultExtractor('$.embeddings[*].values'),
41+
])],
4042
$contract ?? GeminiContract::create(),
4143
);
4244
}

0 commit comments

Comments
 (0)