Skip to content

Commit 2287f60

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 2287f60

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-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: 13 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,7 @@ private function validateArray(array $values, string $expectedType): void
7576

7677
/**
7778
* @param array<string, mixed> $options
79+
* @throws ExceptionInterface
7880
*/
7981
private function vectorizeString(string|\Stringable $string, array $options = []): Vector
8082
{
@@ -93,14 +95,19 @@ private function vectorizeString(string|\Stringable $string, array $options = []
9395

9496
/**
9597
* @param array<string, mixed> $options
98+
* @throws ExceptionInterface
9699
*/
97100
private function vectorizeEmbeddableDocument(EmbeddableDocumentInterface $document, array $options = []): VectorDocument
98101
{
99102
$this->logger->debug('Vectorizing embeddable document', ['document_id' => $document->getId()]);
103+
$result = $this->platform->invoke($this->model, $document->getContent(), $options);
104+
$vectors = $result->asVectors();
100105

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

103-
return new VectorDocument($document->getId(), $vector, $document->getMetadata());
110+
return new VectorDocument($document->getId(), $vectors[0], $document->getMetadata());
104111
}
105112

106113
/**

0 commit comments

Comments
 (0)