diff --git a/src/ai-bundle/src/AiBundle.php b/src/ai-bundle/src/AiBundle.php index bbcc69299..c7c6d6b34 100644 --- a/src/ai-bundle/src/AiBundle.php +++ b/src/ai-bundle/src/AiBundle.php @@ -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); diff --git a/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php b/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php index 4eabe71df..038f34dff 100644 --- a/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php +++ b/src/ai-bundle/tests/DependencyInjection/AiBundleTest.php @@ -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; @@ -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([ diff --git a/src/store/src/Document/Vectorizer.php b/src/store/src/Document/Vectorizer.php index 559737257..a3f27b120 100644 --- a/src/store/src/Document/Vectorizer.php +++ b/src/store/src/Document/Vectorizer.php @@ -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; @@ -20,18 +22,26 @@ 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); } @@ -39,6 +49,7 @@ public function __invoke(array $documents): array foreach ($results as $result) { $vectors = array_merge($vectors, $result->asVectors()); } + $this->logger->debug('Sequential vectorization completed', ['vector_count' => \count($vectors)]); } $vectorDocuments = []; @@ -46,6 +57,11 @@ public function __invoke(array $documents): array $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; } }