Skip to content

Commit 6d17be2

Browse files
committed
feature #473 [Store] Add logging to Vectorizer (OskarStark)
This PR was squashed before being merged into the main branch. Discussion ---------- [Store] Add logging to `Vectorizer` | Q | A | ------------- | --- | Bug fix? | no | New feature? | yes | Docs? | no | Issues | -- | License | MIT Commits ------- 1d06a65 [Store] Add logging to `Vectorizer`
2 parents 0491aa3 + 1d06a65 commit 6d17be2

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/ai-bundle/src/AiBundle.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1057,6 +1057,7 @@ private function processVectorizerConfig(string $name, array $config, ContainerB
10571057
$vectorizerDefinition = new Definition(Vectorizer::class, [
10581058
new Reference($config['platform']),
10591059
new Reference('ai.vectorizer.'.$name.'.model'),
1060+
new Reference('logger', ContainerInterface::IGNORE_ON_INVALID_REFERENCE),
10601061
]);
10611062
$vectorizerDefinition->addTag('ai.vectorizer', ['name' => $name]);
10621063
$container->setDefinition('ai.vectorizer.'.$name, $vectorizerDefinition);

src/ai-bundle/tests/DependencyInjection/AiBundleTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use Symfony\AI\Store\Document\Vectorizer;
2323
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
2424
use Symfony\Component\DependencyInjection\ContainerBuilder;
25+
use Symfony\Component\DependencyInjection\ContainerInterface;
2526
use Symfony\Component\DependencyInjection\Definition;
2627
use Symfony\Component\DependencyInjection\Reference;
2728

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

626+
public function testVectorizerWithLoggerInjection()
627+
{
628+
$container = $this->buildContainer([
629+
'ai' => [
630+
'vectorizer' => [
631+
'my_vectorizer' => [
632+
'platform' => 'my_platform_service_id',
633+
'model' => [
634+
'class' => 'Symfony\AI\Platform\Bridge\OpenAi\Embeddings',
635+
'name' => 'text-embedding-3-small',
636+
],
637+
],
638+
],
639+
],
640+
]);
641+
642+
$vectorizerDefinition = $container->getDefinition('ai.vectorizer.my_vectorizer');
643+
$arguments = $vectorizerDefinition->getArguments();
644+
645+
$this->assertCount(3, $arguments, 'Vectorizer should have 3 arguments: platform, model, and logger');
646+
647+
// First argument should be platform reference
648+
$this->assertInstanceOf(Reference::class, $arguments[0]);
649+
$this->assertSame('my_platform_service_id', (string) $arguments[0]);
650+
651+
// Second argument should be model reference
652+
$this->assertInstanceOf(Reference::class, $arguments[1]);
653+
$this->assertSame('ai.vectorizer.my_vectorizer.model', (string) $arguments[1]);
654+
655+
// Third argument should be logger reference with IGNORE_ON_INVALID_REFERENCE
656+
$this->assertInstanceOf(Reference::class, $arguments[2]);
657+
$this->assertSame('logger', (string) $arguments[2]);
658+
$this->assertSame(ContainerInterface::IGNORE_ON_INVALID_REFERENCE, $arguments[2]->getInvalidBehavior());
659+
}
660+
625661
public function testIndexerWithConfiguredVectorizer()
626662
{
627663
$container = $this->buildContainer([

src/store/src/Document/Vectorizer.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\AI\Store\Document;
1313

14+
use Psr\Log\LoggerInterface;
15+
use Psr\Log\NullLogger;
1416
use Symfony\AI\Platform\Capability;
1517
use Symfony\AI\Platform\Model;
1618
use Symfony\AI\Platform\PlatformInterface;
@@ -20,32 +22,46 @@
2022
public function __construct(
2123
private PlatformInterface $platform,
2224
private Model $model,
25+
private LoggerInterface $logger = new NullLogger(),
2326
) {
2427
}
2528

2629
public function __invoke(array $documents): array
2730
{
31+
$documentCount = \count($documents);
32+
$this->logger->info('Starting vectorization process', ['document_count' => $documentCount]);
33+
2834
if ($this->model->supports(Capability::INPUT_MULTIPLE)) {
35+
$this->logger->debug('Using batch vectorization with model that supports multiple inputs');
2936
$result = $this->platform->invoke($this->model, array_map(fn (TextDocument $document) => $document->content, $documents));
3037

3138
$vectors = $result->asVectors();
39+
$this->logger->debug('Batch vectorization completed', ['vector_count' => \count($vectors)]);
3240
} else {
41+
$this->logger->debug('Using sequential vectorization for model without multiple input support');
3342
$results = [];
34-
foreach ($documents as $document) {
43+
foreach ($documents as $i => $document) {
44+
$this->logger->debug('Vectorizing document', ['document_index' => $i, 'document_id' => $document->id]);
3545
$results[] = $this->platform->invoke($this->model, $document->content);
3646
}
3747

3848
$vectors = [];
3949
foreach ($results as $result) {
4050
$vectors = array_merge($vectors, $result->asVectors());
4151
}
52+
$this->logger->debug('Sequential vectorization completed', ['vector_count' => \count($vectors)]);
4253
}
4354

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

60+
$this->logger->info('Vectorization process completed', [
61+
'document_count' => $documentCount,
62+
'vector_document_count' => \count($vectorDocuments),
63+
]);
64+
4965
return $vectorDocuments;
5066
}
5167
}

0 commit comments

Comments
 (0)