Skip to content
Merged
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
1 change: 1 addition & 0 deletions src/ai-bundle/src/AiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,7 @@ private function processVectorizerConfig(string $name, array $config, ContainerB
$vectorizerDefinition = new Definition(Vectorizer::class, [
new Reference($config['platform']),
new Reference('ai.vectorizer.'.$name.'.model'),
new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
]);
$vectorizerDefinition->addTag('ai.vectorizer', ['name' => $name]);
$container->setDefinition('ai.vectorizer.'.$name, $vectorizerDefinition);
Expand Down
36 changes: 36 additions & 0 deletions src/ai-bundle/tests/DependencyInjection/AiBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\AI\Store\Document\Vectorizer;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

Expand Down Expand Up @@ -622,6 +623,41 @@ public function testVectorizerConfiguration()
$this->assertTrue($modelDefinition->hasTag('ai.model.embeddings_model'));
}

public function testVectorizerWithLoggerInjection()
{
$container = $this->buildContainer([
'ai' => [
'vectorizer' => [
'my_vectorizer' => [
'platform' => 'my_platform_service_id',
'model' => [
'class' => 'Symfony\AI\Platform\Bridge\OpenAi\Embeddings',
'name' => 'text-embedding-3-small',
],
],
],
],
]);

$vectorizerDefinition = $container->getDefinition('ai.vectorizer.my_vectorizer');
$arguments = $vectorizerDefinition->getArguments();

$this->assertCount(3, $arguments, 'Vectorizer should have 3 arguments: platform, model, and logger');

// First argument should be platform reference
$this->assertInstanceOf(Reference::class, $arguments[0]);
$this->assertSame('my_platform_service_id', (string) $arguments[0]);

// Second argument should be model reference
$this->assertInstanceOf(Reference::class, $arguments[1]);
$this->assertSame('ai.vectorizer.my_vectorizer.model', (string) $arguments[1]);

// Third argument should be logger reference with IGNORE_ON_INVALID_REFERENCE
$this->assertInstanceOf(Reference::class, $arguments[2]);
$this->assertSame('logger', (string) $arguments[2]);
$this->assertSame(ContainerInterface::IGNORE_ON_INVALID_REFERENCE, $arguments[2]->getInvalidBehavior());
}

public function testIndexerWithConfiguredVectorizer()
{
$container = $this->buildContainer([
Expand Down
18 changes: 17 additions & 1 deletion src/store/src/Document/Vectorizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\AI\Store\Document;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Symfony\AI\Platform\Capability;
use Symfony\AI\Platform\Model;
use Symfony\AI\Platform\PlatformInterface;
Expand All @@ -20,32 +22,46 @@
public function __construct(
private PlatformInterface $platform,
private Model $model,
private LoggerInterface $logger = new NullLogger(),
) {
}

public function __invoke(array $documents): array
{
$documentCount = \count($documents);
$this->logger->info('Starting vectorization process', ['document_count' => $documentCount]);

if ($this->model->supports(Capability::INPUT_MULTIPLE)) {
$this->logger->debug('Using batch vectorization with model that supports multiple inputs');
$result = $this->platform->invoke($this->model, array_map(fn (TextDocument $document) => $document->content, $documents));

$vectors = $result->asVectors();
$this->logger->debug('Batch vectorization completed', ['vector_count' => \count($vectors)]);
} else {
$this->logger->debug('Using sequential vectorization for model without multiple input support');
$results = [];
foreach ($documents as $document) {
foreach ($documents as $i => $document) {
$this->logger->debug('Vectorizing document', ['document_index' => $i, 'document_id' => $document->id]);
$results[] = $this->platform->invoke($this->model, $document->content);
}

$vectors = [];
foreach ($results as $result) {
$vectors = array_merge($vectors, $result->asVectors());
}
$this->logger->debug('Sequential vectorization completed', ['vector_count' => \count($vectors)]);
}

$vectorDocuments = [];
foreach ($documents as $i => $document) {
$vectorDocuments[] = new VectorDocument($document->id, $vectors[$i], $document->metadata);
}

$this->logger->info('Vectorization process completed', [
'document_count' => $documentCount,
'vector_document_count' => \count($vectorDocuments),
]);

return $vectorDocuments;
}
}