Skip to content
Draft
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
11 changes: 8 additions & 3 deletions src/ai-bundle/config/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@
->arrayPrototype()
->children()
->stringNode('service')->cannotBeEmpty()->defaultValue('cache.app')->end()
->stringNode('cache_key')->end()
->stringNode('cache_key')
->info('The name of the store will be used if the key is not set')
->end()
->stringNode('strategy')->end()
->end()
->end()
Expand Down Expand Up @@ -621,7 +623,7 @@
->stringNode('collection_name')->cannotBeEmpty()->end()
->integerNode('dimensions')->end()
->stringNode('distance')->end()
->booleanNode('async')->defaultFalse()->end()
->booleanNode('async')->end()
->end()
->end()
->end()
Expand Down Expand Up @@ -755,7 +757,10 @@
->arrayPrototype()
->children()
->stringNode('service')->cannotBeEmpty()->defaultValue('cache.app')->end()
->stringNode('key')->end()
->stringNode('key')
->info('The name of the message store will be used if the key is not set')
->end()
->integerNode('ttl')->end()
->end()
->end()
->end()
Expand Down
54 changes: 34 additions & 20 deletions src/ai-bundle/src/AiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use Symfony\AI\AiBundle\Profiler\TraceableToolbox;
use Symfony\AI\AiBundle\Security\Attribute\IsGrantedTool;
use Symfony\AI\Chat\Bridge\HttpFoundation\SessionStore;
use Symfony\AI\Chat\Bridge\Local\CacheStore as CacheMessageStore;
use Symfony\AI\Chat\Bridge\Meilisearch\MessageStore as MeilisearchMessageStore;
use Symfony\AI\Chat\Bridge\Pogocache\MessageStore as PogocacheMessageStore;
use Symfony\AI\Chat\Bridge\Redis\MessageStore as RedisMessageStore;
Expand Down Expand Up @@ -811,10 +812,6 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
new Definition(DistanceCalculator::class),
];

if (\array_key_exists('cache_key', $store) && null !== $store['cache_key']) {
$arguments[2] = $store['cache_key'];
}

if (\array_key_exists('strategy', $store) && null !== $store['strategy']) {
if (!$container->hasDefinition('ai.store.distance_calculator.'.$name)) {
$distanceCalculatorDefinition = new Definition(DistanceCalculator::class);
Expand All @@ -826,10 +823,14 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
$arguments[1] = new Reference('ai.store.distance_calculator.'.$name);
}

$arguments[2] = \array_key_exists('cache_key', $store) && null !== $store['cache_key']
? $store['cache_key']
: $name;

$definition = new Definition(CacheStore::class);
$definition
->addTag('ai.store')
->setArguments($arguments);
->setArguments($arguments)
->addTag('ai.store');

$container->setDefinition('ai.store.'.$type.'.'.$name, $definition);
$container->registerAliasForArgument('ai.store.'.$type.'.'.$name, StoreInterface::class, $name);
Expand Down Expand Up @@ -1364,11 +1365,22 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
*/
private function processMessageStoreConfig(string $type, array $messageStores, ContainerBuilder $container): void
{
if ('memory' === $type) {
if ('cache' === $type) {
foreach ($messageStores as $name => $messageStore) {
$definition = new Definition(InMemoryStore::class);
$arguments = [
new Reference($messageStore['service']),
$messageStore['key'] ?? $name,
];

if (\array_key_exists('ttl', $messageStore)) {
$arguments[2] = $messageStore['ttl'];
}

$definition = new Definition(CacheMessageStore::class);
$definition
->setArgument(0, $messageStore['identifier'])
->setLazy(true)
->setArguments($arguments)
->addTag('proxy', ['interface' => MessageStoreInterface::class])
->addTag('ai.message_store');

$container->setDefinition('ai.message_store.'.$type.'.'.$name, $definition);
Expand All @@ -1377,19 +1389,13 @@ private function processMessageStoreConfig(string $type, array $messageStores, C
}
}

if ('cache' === $type) {
if ('memory' === $type) {
foreach ($messageStores as $name => $messageStore) {
$arguments = [
new Reference($messageStore['service']),
];

if (\array_key_exists('key', $messageStore)) {
$arguments['key'] = $messageStore['key'];
}

$definition = new Definition(CacheStore::class);
$definition = new Definition(InMemoryStore::class);
$definition
->setArguments($arguments)
->setLazy(true)
->setArgument(0, $messageStore['identifier'])
->addTag('proxy', ['interface' => MessageStoreInterface::class])
->addTag('ai.message_store');

$container->setDefinition('ai.message_store.'.$type.'.'.$name, $definition);
Expand All @@ -1402,12 +1408,14 @@ private function processMessageStoreConfig(string $type, array $messageStores, C
foreach ($messageStores as $name => $messageStore) {
$definition = new Definition(MeilisearchMessageStore::class);
$definition
->setLazy(true)
->setArguments([
$messageStore['endpoint'],
$messageStore['api_key'],
new Reference(ClockInterface::class),
$messageStore['index_name'],
])
->addTag('proxy', ['interface' => MessageStoreInterface::class])
->addTag('ai.message_store');

$container->setDefinition('ai.message_store.'.$type.'.'.$name, $definition);
Expand All @@ -1420,12 +1428,14 @@ private function processMessageStoreConfig(string $type, array $messageStores, C
foreach ($messageStores as $name => $messageStore) {
$definition = new Definition(PogocacheMessageStore::class);
$definition
->setLazy(true)
->setArguments([
new Reference('http_client'),
$messageStore['endpoint'],
$messageStore['password'],
$messageStore['key'],
])
->addTag('proxy', ['interface' => MessageStoreInterface::class])
->addTag('ai.message_store');

$container->setDefinition('ai.message_store.'.$type.'.'.$name, $definition);
Expand All @@ -1445,11 +1455,13 @@ private function processMessageStoreConfig(string $type, array $messageStores, C

$definition = new Definition(RedisMessageStore::class);
$definition
->setLazy(true)
->setArguments([
$redisClient,
$messageStore['index_name'],
new Reference('serializer'),
])
->addTag('proxy', ['interface' => MessageStoreInterface::class])
->addTag('ai.message_store');

$container->setDefinition('ai.message_store.'.$type.'.'.$name, $definition);
Expand All @@ -1462,10 +1474,12 @@ private function processMessageStoreConfig(string $type, array $messageStores, C
foreach ($messageStores as $name => $messageStore) {
$definition = new Definition(SessionStore::class);
$definition
->setLazy(true)
->setArguments([
new Reference('request_stack'),
$messageStore['identifier'],
])
->addTag('proxy', ['interface' => MessageStoreInterface::class])
->addTag('ai.message_store');

$container->setDefinition('ai.message_store.'.$type.'.'.$name, $definition);
Expand Down
134 changes: 132 additions & 2 deletions src/ai-bundle/tests/DependencyInjection/AiBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Symfony\AI\Store\Document\Transformer\TextTrimTransformer;
use Symfony\AI\Store\Document\Vectorizer;
use Symfony\AI\Store\StoreInterface;
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface;
Expand Down Expand Up @@ -387,11 +388,12 @@ public function testCacheStoreWithCustomStrategyCanBeConfigured()

$definition = $container->getDefinition('ai.store.cache.my_cache_store_with_custom_strategy');

$this->assertCount(2, $definition->getArguments());
$this->assertCount(3, $definition->getArguments());
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
$this->assertSame('cache.system', (string) $definition->getArgument(0));
$this->assertInstanceOf(Reference::class, $definition->getArgument(1));
$this->assertSame('ai.store.distance_calculator.my_cache_store_with_custom_strategy', (string) $definition->getArgument(1));
$this->assertSame('my_cache_store_with_custom_strategy', $definition->getArgument(2));
}

public function testCacheStoreWithCustomStrategyAndKeyCanBeConfigured()
Expand All @@ -418,9 +420,9 @@ public function testCacheStoreWithCustomStrategyAndKeyCanBeConfigured()
$this->assertCount(3, $definition->getArguments());
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
$this->assertSame('cache.system', (string) $definition->getArgument(0));
$this->assertSame('random', $definition->getArgument(2));
$this->assertInstanceOf(Reference::class, $definition->getArgument(1));
$this->assertSame('ai.store.distance_calculator.my_cache_store_with_custom_strategy', (string) $definition->getArgument(1));
$this->assertSame('random', $definition->getArgument(2));
}

public function testInMemoryStoreWithoutCustomStrategyCanBeConfigured()
Expand Down Expand Up @@ -2757,6 +2759,116 @@ public function testVectorizerModelBooleanOptionsArePreserved()
$this->assertSame('text-embedding-3-small?normalize=false&cache=true&nested%5Bbool%5D=false', $vectorizerDefinition->getArgument(1));
}

public function testCacheMessageStoreCanBeConfiguredWithCustomKey()
{
$container = $this->buildContainer([
'ai' => [
'message_store' => [
'cache' => [
'custom' => [
'service' => 'cache.app',
'key' => 'custom',
],
],
],
],
]);

$cacheMessageStoreDefinition = $container->getDefinition('ai.message_store.cache.custom');

$this->assertInstanceOf(Reference::class, $cacheMessageStoreDefinition->getArgument(0));
$this->assertSame('cache.app', (string) $cacheMessageStoreDefinition->getArgument(0));

$this->assertSame('custom', (string) $cacheMessageStoreDefinition->getArgument(1));

$this->assertTrue($cacheMessageStoreDefinition->hasTag('proxy'));
$this->assertSame([['interface' => MessageStoreInterface::class]], $cacheMessageStoreDefinition->getTag('proxy'));
$this->assertTrue($cacheMessageStoreDefinition->hasTag('ai.message_store'));
}

public function testCacheMessageStoreCanBeConfiguredWithCustomTtl()
{
$container = $this->buildContainer([
'ai' => [
'message_store' => [
'cache' => [
'custom' => [
'service' => 'cache.app',
'ttl' => 3600,
],
],
],
],
]);

$cacheMessageStoreDefinition = $container->getDefinition('ai.message_store.cache.custom');

$this->assertTrue($cacheMessageStoreDefinition->isLazy());
$this->assertInstanceOf(Reference::class, $cacheMessageStoreDefinition->getArgument(0));
$this->assertSame('cache.app', (string) $cacheMessageStoreDefinition->getArgument(0));

$this->assertSame('custom', (string) $cacheMessageStoreDefinition->getArgument(1));
$this->assertSame(3600, (int) $cacheMessageStoreDefinition->getArgument(2));

$this->assertTrue($cacheMessageStoreDefinition->hasTag('proxy'));
$this->assertSame([['interface' => MessageStoreInterface::class]], $cacheMessageStoreDefinition->getTag('proxy'));
$this->assertTrue($cacheMessageStoreDefinition->hasTag('ai.message_store'));
}

public function testMemoryMessageStoreCanBeConfiguredWithCustomKey()
{
$container = $this->buildContainer([
'ai' => [
'message_store' => [
'memory' => [
'custom' => [
'identifier' => 'foo',
],
],
],
],
]);

$memoryMessageStoreDefinition = $container->getDefinition('ai.message_store.memory.custom');

$this->assertTrue($memoryMessageStoreDefinition->isLazy());
$this->assertSame('foo', $memoryMessageStoreDefinition->getArgument(0));

$this->assertTrue($memoryMessageStoreDefinition->hasTag('proxy'));
$this->assertSame([['interface' => MessageStoreInterface::class]], $memoryMessageStoreDefinition->getTag('proxy'));
$this->assertTrue($memoryMessageStoreDefinition->hasTag('ai.message_store'));
}

public function testMeilisearchMessageStoreIsConfigured()
{
$container = $this->buildContainer([
'ai' => [
'message_store' => [
'meilisearch' => [
'custom' => [
'endpoint' => 'http://127.0.0.1:7700',
'api_key' => 'foo',
'index_name' => 'test',
],
],
],
],
]);

$meilisearchMessageStoreDefinition = $container->getDefinition('ai.message_store.meilisearch.custom');

$this->assertTrue($meilisearchMessageStoreDefinition->isLazy());
$this->assertSame('http://127.0.0.1:7700', $meilisearchMessageStoreDefinition->getArgument(0));
$this->assertSame('foo', $meilisearchMessageStoreDefinition->getArgument(1));
$this->assertInstanceOf(Reference::class, $meilisearchMessageStoreDefinition->getArgument(2));
$this->assertSame(ClockInterface::class, (string) $meilisearchMessageStoreDefinition->getArgument(2));
$this->assertSame('test', $meilisearchMessageStoreDefinition->getArgument(3));

$this->assertTrue($meilisearchMessageStoreDefinition->hasTag('proxy'));
$this->assertSame([['interface' => MessageStoreInterface::class]], $meilisearchMessageStoreDefinition->getTag('proxy'));
$this->assertTrue($meilisearchMessageStoreDefinition->hasTag('ai.message_store'));
}

private function buildContainer(array $configuration): ContainerBuilder
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -2987,6 +3099,24 @@ private function getFullConfig(): array
'distance' => 'Cosine',
'async' => false,
],
'my_custom_dimensions_qdrant_store' => [
'endpoint' => 'http://127.0.0.1:8000',
'api_key' => 'test',
'collection_name' => 'foo',
'dimensions' => 768,
],
'my_custom_distance_qdrant_store' => [
'endpoint' => 'http://127.0.0.1:8000',
'api_key' => 'test',
'collection_name' => 'foo',
'distance' => 'Cosine',
],
'my_async_qdrant_store' => [
'endpoint' => 'http://127.0.0.1:8000',
'api_key' => 'test',
'collection_name' => 'foo',
'async' => false,
],
],
'redis' => [
'my_redis_store' => [
Expand Down
2 changes: 1 addition & 1 deletion src/chat/src/Bridge/Local/CacheStore.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ final class CacheStore implements ManagedStoreInterface, MessageStoreInterface
{
public function __construct(
private readonly CacheItemPoolInterface $cache,
private readonly string $cacheKey,
private readonly string $cacheKey = '_message_store_cache',
private readonly int $ttl = 86400,
) {
if (!interface_exists(CacheItemPoolInterface::class)) {
Expand Down