Skip to content

Commit a5e2e28

Browse files
committed
Use JSON Path to convert responses
1 parent 4b469a1 commit a5e2e28

File tree

22 files changed

+133
-282
lines changed

22 files changed

+133
-282
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()],
4443
Contract::create(),
4544
);
4645
}

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

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

4242
return new Platform(
4343
[$GPTModelClient, $embeddingsModelClient, $whisperModelClient],
44-
[new GPT\ResultConverter(), new Embeddings\ResultConverter(), new Whisper\ResultConverter()],
44+
[new GPT\ResultConverter(), new Whisper\ResultConverter()],
4545
$contract ?? Contract::create(new AudioNormalizer()),
4646
);
4747
}

src/platform/src/Bridge/Gemini/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/Gemini/Embeddings/ResultConverter.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\Gemini\Embeddings;
1313

14-
use Symfony\AI\Platform\Bridge\Gemini\Embeddings;
15-
use Symfony\AI\Platform\Exception\RuntimeException;
16-
use Symfony\AI\Platform\Model;
17-
use Symfony\AI\Platform\Result\RawResultInterface;
18-
use Symfony\AI\Platform\Result\VectorResult;
19-
use Symfony\AI\Platform\ResultConverterInterface;
20-
use Symfony\AI\Platform\Vector\Vector;
14+
use Symfony\AI\Platform\Contract\ResultConverter\VectorResultConverter;
2115

2216
/**
2317
* @author Valtteri R <[email protected]>
2418
*/
25-
final readonly class ResultConverter implements ResultConverterInterface
19+
final readonly class ResultConverter extends VectorResultConverter
2620
{
27-
public function supports(Model $model): bool
21+
public function __construct()
2822
{
29-
return $model instanceof Embeddings;
30-
}
31-
32-
public function convert(RawResultInterface $result, array $options = []): VectorResult
33-
{
34-
$data = $result->getData();
35-
36-
if (!isset($data['embeddings'])) {
37-
throw new RuntimeException('Response does not contain data');
38-
}
39-
40-
return new VectorResult(
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/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()],
3636
$contract ?? Contract::create(new ToolNormalizer()),
3737
);
3838
}

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/ResultConverter.php

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

0 commit comments

Comments
 (0)