Skip to content

Commit d3c8093

Browse files
committed
Support mixed for getContent method
As the Vectorizer currently makes assumptions about having to pass string data to `PlatformInterface::invoke`, when that method can actually take about any sort of input. `Platform` implementations decide what data types they can handle through their normalizers. Therefore, it should be up to `platform` to handle types and not up to `store` to decide that it should only pass strings.
1 parent 2ddd2b3 commit d3c8093

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/store/src/Document/EmbeddableDocumentInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface EmbeddableDocumentInterface
1515
{
1616
public function getId(): mixed;
1717

18-
public function getContent(): string;
18+
public function getContent(): mixed;
1919

2020
public function getMetadata(): Metadata;
2121
}

src/store/src/Document/Vectorizer.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,17 @@
1414
use Psr\Log\LoggerInterface;
1515
use Psr\Log\NullLogger;
1616
use Symfony\AI\Platform\Capability;
17+
use Symfony\AI\Platform\Exception\ExceptionInterface;
1718
use Symfony\AI\Platform\PlatformInterface;
1819
use Symfony\AI\Platform\Vector\Vector;
1920
use Symfony\AI\Store\Exception\RuntimeException;
2021

21-
final readonly class Vectorizer implements VectorizerInterface
22+
final class Vectorizer implements VectorizerInterface
2223
{
2324
public function __construct(
24-
private PlatformInterface $platform,
25-
private string $model,
26-
private LoggerInterface $logger = new NullLogger(),
25+
private readonly PlatformInterface $platform,
26+
private readonly string $model,
27+
private readonly LoggerInterface $logger = new NullLogger(),
2728
) {
2829
}
2930

@@ -75,6 +76,8 @@ private function validateArray(array $values, string $expectedType): void
7576

7677
/**
7778
* @param array<string, mixed> $options
79+
*
80+
* @throws ExceptionInterface
7881
*/
7982
private function vectorizeString(string|\Stringable $string, array $options = []): Vector
8083
{
@@ -93,14 +96,20 @@ private function vectorizeString(string|\Stringable $string, array $options = []
9396

9497
/**
9598
* @param array<string, mixed> $options
99+
*
100+
* @throws ExceptionInterface
96101
*/
97102
private function vectorizeEmbeddableDocument(EmbeddableDocumentInterface $document, array $options = []): VectorDocument
98103
{
99104
$this->logger->debug('Vectorizing embeddable document', ['document_id' => $document->getId()]);
105+
$result = $this->platform->invoke($this->model, $document->getContent(), $options);
106+
$vectors = $result->asVectors();
100107

101-
$vector = $this->vectorizeString($document->getContent(), $options);
108+
if (!isset($vectors[0])) {
109+
throw new RuntimeException('No vector returned for vectorization.');
110+
}
102111

103-
return new VectorDocument($document->getId(), $vector, $document->getMetadata());
112+
return new VectorDocument($document->getId(), $vectors[0], $document->getMetadata());
104113
}
105114

106115
/**

0 commit comments

Comments
 (0)