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
27 changes: 16 additions & 11 deletions src/ai-bundle/config/options.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,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 @@ -651,7 +653,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 @@ -772,20 +774,15 @@
->end()
->arrayNode('message_store')
->children()
->arrayNode('memory')
->useAttributeAsKey('name')
->arrayPrototype()
->children()
->stringNode('identifier')->cannotBeEmpty()->end()
->end()
->end()
->end()
->arrayNode('cache')
->useAttributeAsKey('name')
->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 All @@ -799,6 +796,14 @@
->end()
->end()
->end()
->arrayNode('memory')
->useAttributeAsKey('name')
->arrayPrototype()
->children()
->stringNode('identifier')->cannotBeEmpty()->end()
->end()
->end()
->end()
->arrayNode('pogocache')
->useAttributeAsKey('name')
->arrayPrototype()
Expand Down
58 changes: 36 additions & 22 deletions src/ai-bundle/src/AiBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,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 @@ -917,10 +918,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 @@ -932,10 +929,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 @@ -1470,32 +1471,22 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
*/
private function processMessageStoreConfig(string $type, array $messageStores, ContainerBuilder $container): void
{
if ('memory' === $type) {
foreach ($messageStores as $name => $messageStore) {
$definition = new Definition(InMemoryStore::class);
$definition
->setArgument(0, $messageStore['identifier'])
->addTag('ai.message_store');

$container->setDefinition('ai.message_store.'.$type.'.'.$name, $definition);
$container->registerAliasForArgument('ai.message_store.'.$type.'.'.$name, MessageStoreInterface::class, $name);
$container->registerAliasForArgument('ai.message_store.'.$type.'.'.$name, MessageStoreInterface::class, $type.'_'.$name);
}
}

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

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

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

$container->setDefinition('ai.message_store.'.$type.'.'.$name, $definition);
Expand All @@ -1508,12 +1499,29 @@ 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);
$container->registerAliasForArgument('ai.message_store.'.$type.'.'.$name, MessageStoreInterface::class, $name);
$container->registerAliasForArgument('ai.message_store.'.$type.'.'.$name, MessageStoreInterface::class, $type.'_'.$name);
}
}

if ('memory' === $type) {
foreach ($messageStores as $name => $messageStore) {
$definition = new Definition(InMemoryStore::class);
$definition
->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 @@ -1526,12 +1534,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 @@ -1551,11 +1561,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 @@ -1568,10 +1580,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
Loading