Skip to content

Commit 7ec4f22

Browse files
committed
Use JSON Path to convert responses
1 parent f1870bc commit 7ec4f22

File tree

17 files changed

+71
-180
lines changed

17 files changed

+71
-180
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/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\ResponseConverter;
1615
use Symfony\AI\Platform\Bridge\OpenAI\Whisper\AudioNormalizer;
1716
use Symfony\AI\Platform\Contract;
@@ -40,7 +39,7 @@ public static function create(
4039

4140
return new Platform(
4241
[$GPTResponseFactory, $embeddingsResponseFactory, $whisperResponseFactory],
43-
[new ResponseConverter(), new Embeddings\ResponseConverter(), new \Symfony\AI\Platform\Bridge\OpenAI\Whisper\ResponseConverter()],
42+
[new ResponseConverter(), new \Symfony\AI\Platform\Bridge\OpenAI\Whisper\ResponseConverter()],
4443
$contract ?? Contract::create(new AudioNormalizer()),
4544
);
4645
}

src/platform/src/Bridge/Google/Embeddings.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ class Embeddings extends Model
3232
*/
3333
public function __construct(string $name = self::GEMINI_EMBEDDING_EXP_03_07, array $options = [])
3434
{
35-
parent::__construct($name, [Capability::INPUT_MULTIPLE], $options);
35+
parent::__construct($name, [Capability::INPUT_MULTIPLE, Capability::OUTPUT_VECTOR], $options);
3636
}
3737
}

src/platform/src/Bridge/Google/Embeddings/ResponseConverter.php

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,15 @@
1111

1212
namespace Symfony\AI\Platform\Bridge\Google\Embeddings;
1313

14-
use Symfony\AI\Platform\Bridge\Google\Embeddings;
15-
use Symfony\AI\Platform\Exception\RuntimeException;
16-
use Symfony\AI\Platform\Model;
17-
use Symfony\AI\Platform\Response\VectorResponse;
18-
use Symfony\AI\Platform\ResponseConverterInterface;
19-
use Symfony\AI\Platform\Vector\Vector;
20-
use Symfony\Contracts\HttpClient\ResponseInterface;
14+
use Symfony\AI\Platform\Contract\ResponseConverter\VectorResponseConverter;
2115

2216
/**
2317
* @author Valtteri R <[email protected]>
2418
*/
25-
final readonly class ResponseConverter implements ResponseConverterInterface
19+
final readonly class ResponseConverter extends VectorResponseConverter
2620
{
27-
public function supports(Model $model): bool
21+
public function __construct()
2822
{
29-
return $model instanceof Embeddings;
30-
}
31-
32-
public function convert(ResponseInterface $response, array $options = []): VectorResponse
33-
{
34-
$data = $response->toArray();
35-
36-
if (!isset($data['embeddings'])) {
37-
throw new RuntimeException('Response does not contain data');
38-
}
39-
40-
return new VectorResponse(
41-
...array_map(
42-
static fn (array $item): Vector => new Vector($item['values']),
43-
$data['embeddings'],
44-
),
45-
);
23+
parent::__construct('$.embeddings[*].values');
4624
}
4725
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,6 @@ public function __construct(
2828
string $name = self::MISTRAL_EMBED,
2929
array $options = [],
3030
) {
31-
parent::__construct($name, [Capability::INPUT_MULTIPLE], $options);
31+
parent::__construct($name, [Capability::INPUT_MULTIPLE, Capability::OUTPUT_VECTOR], $options);
3232
}
3333
}

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

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

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

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

1414
use Symfony\AI\Platform\Bridge\Mistral\Contract\ToolNormalizer;
1515
use Symfony\AI\Platform\Bridge\Mistral\Embeddings\ModelClient as EmbeddingsModelClient;
16-
use Symfony\AI\Platform\Bridge\Mistral\Embeddings\ResponseConverter as EmbeddingsResponseConverter;
1716
use Symfony\AI\Platform\Bridge\Mistral\Llm\ModelClient as MistralModelClient;
1817
use Symfony\AI\Platform\Bridge\Mistral\Llm\ResponseConverter as MistralResponseConverter;
1918
use Symfony\AI\Platform\Contract;
@@ -36,7 +35,7 @@ public static function create(
3635

3736
return new Platform(
3837
[new EmbeddingsModelClient($httpClient, $apiKey), new MistralModelClient($httpClient, $apiKey)],
39-
[new EmbeddingsResponseConverter(), new MistralResponseConverter()],
38+
[new MistralResponseConverter()],
4039
$contract ?? Contract::create(new ToolNormalizer()),
4140
);
4241
}

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

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

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

14+
use Symfony\AI\Platform\Capability;
1415
use Symfony\AI\Platform\Model;
1516

1617
/**
@@ -27,6 +28,6 @@ class Embeddings extends Model
2728
*/
2829
public function __construct(string $name = self::TEXT_3_SMALL, array $options = [])
2930
{
30-
parent::__construct($name, [], $options);
31+
parent::__construct($name, [Capability::OUTPUT_VECTOR], $options);
3132
}
3233
}

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

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

0 commit comments

Comments
 (0)