Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/store/src/Document/EmbeddableDocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ interface EmbeddableDocumentInterface
{
public function getId(): mixed;

public function getContent(): string;
public function getContent(): mixed;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe string|object is a good alternative if we want to be a bit stricter? Because the platform normalizers also mostly assume that you're passing either strings or data classes.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If possible I would vote to use string|object instead of mixed, WDYT @chr-hertel ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, i would agree, currently the invoke also is explicit on the $input witharray|string|object so it would be more consistent to be explicit here as well => string|object


public function getMetadata(): Metadata;
}
21 changes: 15 additions & 6 deletions src/store/src/Document/Vectorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Exception\ExceptionInterface;
use Symfony\AI\Platform\PlatformInterface;
use Symfony\AI\Platform\Vector\Vector;
use Symfony\AI\Store\Exception\RuntimeException;

final readonly class Vectorizer implements VectorizerInterface
final class Vectorizer implements VectorizerInterface
{
public function __construct(
private PlatformInterface $platform,
private string $model,
private LoggerInterface $logger = new NullLogger(),
private readonly PlatformInterface $platform,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PHPStan complained about the readonly class btw. I guess this is a new rule?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private readonly string $model,
private readonly LoggerInterface $logger = new NullLogger(),
) {
}

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

/**
* @param array<string, mixed> $options
*
* @throws ExceptionInterface
*/
private function vectorizeString(string|\Stringable $string, array $options = []): Vector
{
Expand All @@ -93,14 +96,20 @@ private function vectorizeString(string|\Stringable $string, array $options = []

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

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

return new VectorDocument($document->getId(), $vector, $document->getMetadata());
return new VectorDocument($document->getId(), $vectors[0], $document->getMetadata());
}

/**
Expand Down
Loading