Skip to content

Commit 459b7a3

Browse files
committed
Use JSON Path to convert responses
1 parent 2b6f483 commit 459b7a3

33 files changed

+1794
-342
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
},

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/src/Bridge/Albert/PlatformFactory.php

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

1212
namespace Symfony\AI\Platform\Bridge\Albert;
1313

14-
use Symfony\AI\Platform\Bridge\OpenAI\Embeddings;
1514
use Symfony\AI\Platform\Bridge\OpenAI\GPT;
1615
use Symfony\AI\Platform\Contract;
1716
use Symfony\AI\Platform\Exception\InvalidArgumentException;
@@ -40,7 +39,7 @@ public static function create(
4039
new GPTModelClient($httpClient, $apiKey, $baseUrl),
4140
new EmbeddingsModelClient($httpClient, $apiKey, $baseUrl),
4241
],
43-
[new GPT\ResultConverter(), new Embeddings\ResultConverter()],
42+
[new GPT\ResultConverter(), Contract\ResultConverter::create()],
4443
Contract::create(),
4544
);
4645
}

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

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

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

14-
use Symfony\AI\Platform\Bridge\OpenAI\Embeddings;
1514
use Symfony\AI\Platform\Bridge\OpenAI\GPT;
1615
use Symfony\AI\Platform\Bridge\OpenAI\Whisper;
1716
use Symfony\AI\Platform\Bridge\OpenAI\Whisper\AudioNormalizer;
@@ -41,7 +40,7 @@ public static function create(
4140

4241
return new Platform(
4342
[$GPTModelClient, $embeddingsModelClient, $whisperModelClient],
44-
[new GPT\ResultConverter(), new Embeddings\ResultConverter(), new Whisper\ResultConverter()],
43+
[new GPT\ResultConverter(), new Whisper\ResultConverter(), Contract\ResultConverter::create()],
4544
$contract ?? Contract::create(new AudioNormalizer()),
4645
);
4746
}

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\JsonPathConverter\VectorResultExtractor;
19+
use Symfony\AI\Platform\Contract\ResultConverter;
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
}

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

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

src/platform/src/Bridge/Mistral/Llm/ResultConverter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,14 @@ private function isToolCallsStreamFinished(array $data): bool
170170
* finish_reason: 'stop'|'length'|'tool_calls'|'content_filter',
171171
* } $choice
172172
*/
173-
private function convertChoice(array $choice): Choice
173+
private function convertChoice(array $choice): TextResult|ToolCallResult
174174
{
175175
if ('tool_calls' === $choice['finish_reason']) {
176-
return new Choice(toolCalls: array_map([$this, 'convertToolCall'], $choice['message']['tool_calls']));
176+
return new ToolCallResult(...array_map([$this, 'convertToolCall'], $choice['message']['tool_calls']));
177177
}
178178

179179
if ('stop' === $choice['finish_reason']) {
180-
return new Choice($choice['message']['content']);
180+
return new TextResult($choice['message']['content']);
181181
}
182182

183183
throw new RuntimeException(\sprintf('Unsupported finish reason "%s".', $choice['finish_reason']));

src/platform/src/Bridge/Mistral/PlatformFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static function create(
3232

3333
return new Platform(
3434
[new Embeddings\ModelClient($httpClient, $apiKey), new Llm\ModelClient($httpClient, $apiKey)],
35-
[new Embeddings\ResultConverter(), new Llm\ResultConverter()],
35+
[new Llm\ResultConverter(), Contract\ResultConverter::create()],
3636
$contract ?? Contract::create(new ToolNormalizer()),
3737
);
3838
}

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

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

0 commit comments

Comments
 (0)