Skip to content

Commit 47ab79e

Browse files
committed
fix(aibundle): cache store configuration
1 parent a50d26e commit 47ab79e

File tree

4 files changed

+116
-1
lines changed

4 files changed

+116
-1
lines changed

src/ai-bundle/config/options.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@
167167
->arrayPrototype()
168168
->children()
169169
->scalarNode('service')->cannotBeEmpty()->defaultValue('cache.app')->end()
170+
->scalarNode('key')->end()
171+
->scalarNode('strategy')->end()
170172
->end()
171173
->end()
172174
->end()

src/ai-bundle/doc/index.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ Configuration
9191
# multiple collections possible per type
9292
default:
9393
collection: 'my_collection'
94+
cache:
95+
ollama:
96+
cache:
97+
service: 'cache.app'
98+
key: 'ollama'
99+
strategy: 'chebyshev'
94100
indexer:
95101
default:
96102
# platform: 'ai.platform.mistral'

src/ai-bundle/src/AiBundle.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
use Symfony\AI\Store\Bridge\SurrealDb\Store as SurrealDbStore;
5151
use Symfony\AI\Store\Bridge\Typesense\Store as TypesenseStore;
5252
use Symfony\AI\Store\CacheStore;
53+
use Symfony\AI\Store\DistanceCalculator;
54+
use Symfony\AI\Store\DistanceStrategy;
5355
use Symfony\AI\Store\Document\Vectorizer;
5456
use Symfony\AI\Store\Indexer;
5557
use Symfony\AI\Store\InMemoryStore;
@@ -493,9 +495,19 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
493495
if ('cache' === $type) {
494496
foreach ($stores as $name => $store) {
495497
$arguments = [
496-
new Reference($store['service']),
498+
0 => new Reference($store['service']),
499+
2 => $store['key'] ?? '_vectors',
497500
];
498501

502+
if (\array_key_exists('strategy', $store)) {
503+
$distanceCalculatorDefinition = new Definition(DistanceCalculator::class);
504+
$distanceCalculatorDefinition->setArgument(0, DistanceStrategy::from($store['strategy']));
505+
506+
$container->setDefinition('ai.store.distance_calculator.'.$name, $distanceCalculatorDefinition);
507+
508+
$arguments[1] = new Reference('ai.store.distance_calculator.'.$name);
509+
}
510+
499511
$definition = new Definition(CacheStore::class);
500512
$definition
501513
->addTag('ai.store')

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@
1919
use Symfony\AI\AiBundle\AiBundle;
2020
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
2121
use Symfony\Component\DependencyInjection\ContainerBuilder;
22+
use Symfony\Component\DependencyInjection\Definition;
23+
use Symfony\Component\DependencyInjection\Reference;
2224

2325
#[CoversClass(AiBundle::class)]
2426
#[UsesClass(ContainerBuilder::class)]
27+
#[UsesClass(Definition::class)]
28+
#[UsesClass(Reference::class)]
2529
class AiBundleTest extends TestCase
2630
{
2731
#[DoesNotPerformAssertions]
@@ -105,6 +109,89 @@ public function testAgentsAsToolsCannotDefineService()
105109
]);
106110
}
107111

112+
public function testCacheStoreWithCustomKeyCanBeConfigured()
113+
{
114+
$container = $this->buildContainer([
115+
'ai' => [
116+
'store' => [
117+
'cache' => [
118+
'my_cache_store_with_custom_strategy' => [
119+
'service' => 'cache.system',
120+
'key' => 'random',
121+
],
122+
],
123+
],
124+
],
125+
]);
126+
127+
$this->assertTrue($container->hasDefinition('ai.store.cache.my_cache_store_with_custom_strategy'));
128+
$this->assertFalse($container->hasDefinition('ai.store.distance_calculator.my_cache_store_with_custom_strategy'));
129+
130+
$definition = $container->getDefinition('ai.store.cache.my_cache_store_with_custom_strategy');
131+
132+
$this->assertCount(2, $definition->getArguments());
133+
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
134+
$this->assertSame('cache.system', (string) $definition->getArgument(0));
135+
$this->assertSame('random', $definition->getArgument(2));
136+
}
137+
138+
public function testCacheStoreWithCustomStrategyCanBeConfigured()
139+
{
140+
$container = $this->buildContainer([
141+
'ai' => [
142+
'store' => [
143+
'cache' => [
144+
'my_cache_store_with_custom_strategy' => [
145+
'service' => 'cache.system',
146+
'strategy' => 'chebyshev',
147+
],
148+
],
149+
],
150+
],
151+
]);
152+
153+
$this->assertTrue($container->hasDefinition('ai.store.cache.my_cache_store_with_custom_strategy'));
154+
$this->assertTrue($container->hasDefinition('ai.store.distance_calculator.my_cache_store_with_custom_strategy'));
155+
156+
$definition = $container->getDefinition('ai.store.cache.my_cache_store_with_custom_strategy');
157+
158+
$this->assertCount(3, $definition->getArguments());
159+
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
160+
$this->assertSame('cache.system', (string) $definition->getArgument(0));
161+
$this->assertSame('_vectors', $definition->getArgument(2));
162+
$this->assertInstanceOf(Reference::class, $definition->getArgument(1));
163+
$this->assertSame('ai.store.distance_calculator.my_cache_store_with_custom_strategy', (string) $definition->getArgument(1));
164+
}
165+
166+
public function testCacheStoreWithCustomStrategyAndKeyCanBeConfigured()
167+
{
168+
$container = $this->buildContainer([
169+
'ai' => [
170+
'store' => [
171+
'cache' => [
172+
'my_cache_store_with_custom_strategy' => [
173+
'service' => 'cache.system',
174+
'key' => 'random',
175+
'strategy' => 'chebyshev',
176+
],
177+
],
178+
],
179+
],
180+
]);
181+
182+
$this->assertTrue($container->hasDefinition('ai.store.cache.my_cache_store_with_custom_strategy'));
183+
$this->assertTrue($container->hasDefinition('ai.store.distance_calculator.my_cache_store_with_custom_strategy'));
184+
185+
$definition = $container->getDefinition('ai.store.cache.my_cache_store_with_custom_strategy');
186+
187+
$this->assertCount(3, $definition->getArguments());
188+
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
189+
$this->assertSame('cache.system', (string) $definition->getArgument(0));
190+
$this->assertSame('random', $definition->getArgument(2));
191+
$this->assertInstanceOf(Reference::class, $definition->getArgument(1));
192+
$this->assertSame('ai.store.distance_calculator.my_cache_store_with_custom_strategy', (string) $definition->getArgument(1));
193+
}
194+
108195
private function buildContainer(array $configuration): ContainerBuilder
109196
{
110197
$container = new ContainerBuilder();
@@ -205,6 +292,14 @@ private function getFullConfig(): array
205292
'my_cache_store' => [
206293
'service' => 'cache.system',
207294
],
295+
'my_cache_store_with_custom_key' => [
296+
'service' => 'cache.system',
297+
'key' => 'bar',
298+
],
299+
'my_cache_store_with_custom_strategy' => [
300+
'service' => 'cache.system',
301+
'strategy' => 'chebyshev',
302+
],
208303
],
209304
'chroma_db' => [
210305
'my_chroma_store' => [

0 commit comments

Comments
 (0)