Skip to content

Commit 220e3b6

Browse files
committed
fix(aibundle): cache store configuration
1 parent 1336ce7 commit 220e3b6

File tree

6 files changed

+190
-12
lines changed

6 files changed

+190
-12
lines changed

src/ai-bundle/config/options.php

Lines changed: 3 additions & 1 deletion
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('cache_key')->end()
171+
->scalarNode('strategy')->end()
170172
->end()
171173
->end()
172174
->end()
@@ -215,7 +217,7 @@
215217
->useAttributeAsKey('name')
216218
->arrayPrototype()
217219
->children()
218-
->scalarNode('distance')->cannotBeEmpty()->end()
220+
->scalarNode('strategy')->cannotBeEmpty()->end()
219221
->end()
220222
->end()
221223
->end()

src/ai-bundle/doc/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ Configuration
9191
# multiple collections possible per type
9292
default:
9393
collection: 'my_collection'
94+
cache:
95+
research:
96+
service: 'cache.app'
97+
cache_key: 'research'
98+
strategy: 'chebyshev'
99+
memory:
100+
ollama:
101+
strategy: 'manhattan'
94102
indexer:
95103
default:
96104
# platform: 'ai.platform.mistral'

src/ai-bundle/src/AiBundle.php

Lines changed: 30 additions & 3 deletions
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,8 +495,24 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
493495
foreach ($stores as $name => $store) {
494496
$arguments = [
495497
new Reference($store['service']),
498+
new Definition(DistanceCalculator::class),
496499
];
497500

501+
if (\array_key_exists('cache_key', $store) && null !== $store['cache_key']) {
502+
$arguments[2] = $store['cache_key'];
503+
}
504+
505+
if (\array_key_exists('strategy', $store) && null !== $store['strategy']) {
506+
if (!$container->hasDefinition('ai.store.distance_calculator.'.$name)) {
507+
$distanceCalculatorDefinition = new Definition(DistanceCalculator::class);
508+
$distanceCalculatorDefinition->setArgument(0, DistanceStrategy::from($store['strategy']));
509+
510+
$container->setDefinition('ai.store.distance_calculator.'.$name, $distanceCalculatorDefinition);
511+
}
512+
513+
$arguments[1] = new Reference('ai.store.distance_calculator.'.$name);
514+
}
515+
498516
$definition = new Definition(CacheStore::class);
499517
$definition
500518
->addTag('ai.store')
@@ -576,9 +594,18 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
576594

577595
if ('memory' === $type) {
578596
foreach ($stores as $name => $store) {
579-
$arguments = [
580-
$store['distance'],
581-
];
597+
$arguments = [];
598+
599+
if (\array_key_exists('strategy', $store) && null !== $store['strategy']) {
600+
if (!$container->hasDefinition('ai.store.distance_calculator.'.$name)) {
601+
$distanceCalculatorDefinition = new Definition(DistanceCalculator::class);
602+
$distanceCalculatorDefinition->setArgument(0, DistanceStrategy::from($store['strategy']));
603+
604+
$container->setDefinition('ai.store.distance_calculator.'.$name, $distanceCalculatorDefinition);
605+
}
606+
607+
$arguments[0] = new Reference('ai.store.distance_calculator.'.$name);
608+
}
582609

583610
$definition = new Definition(InMemoryStore::class);
584611
$definition

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

Lines changed: 142 additions & 1 deletion
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]
@@ -120,6 +124,130 @@ public function testAgentsAsToolsCannotDefineService()
120124
]);
121125
}
122126

127+
public function testCacheStoreWithCustomKeyCanBeConfigured()
128+
{
129+
$container = $this->buildContainer([
130+
'ai' => [
131+
'store' => [
132+
'cache' => [
133+
'my_cache_store_with_custom_strategy' => [
134+
'service' => 'cache.system',
135+
'cache_key' => 'random',
136+
],
137+
],
138+
],
139+
],
140+
]);
141+
142+
$this->assertTrue($container->hasDefinition('ai.store.cache.my_cache_store_with_custom_strategy'));
143+
$this->assertFalse($container->hasDefinition('ai.store.distance_calculator.my_cache_store_with_custom_strategy'));
144+
145+
$definition = $container->getDefinition('ai.store.cache.my_cache_store_with_custom_strategy');
146+
147+
$this->assertCount(3, $definition->getArguments());
148+
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
149+
$this->assertSame('cache.system', (string) $definition->getArgument(0));
150+
$this->assertSame('random', $definition->getArgument(2));
151+
}
152+
153+
public function testCacheStoreWithCustomStrategyCanBeConfigured()
154+
{
155+
$container = $this->buildContainer([
156+
'ai' => [
157+
'store' => [
158+
'cache' => [
159+
'my_cache_store_with_custom_strategy' => [
160+
'service' => 'cache.system',
161+
'strategy' => 'chebyshev',
162+
],
163+
],
164+
],
165+
],
166+
]);
167+
168+
$this->assertTrue($container->hasDefinition('ai.store.cache.my_cache_store_with_custom_strategy'));
169+
$this->assertTrue($container->hasDefinition('ai.store.distance_calculator.my_cache_store_with_custom_strategy'));
170+
171+
$definition = $container->getDefinition('ai.store.cache.my_cache_store_with_custom_strategy');
172+
173+
$this->assertCount(2, $definition->getArguments());
174+
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
175+
$this->assertSame('cache.system', (string) $definition->getArgument(0));
176+
$this->assertInstanceOf(Reference::class, $definition->getArgument(1));
177+
$this->assertSame('ai.store.distance_calculator.my_cache_store_with_custom_strategy', (string) $definition->getArgument(1));
178+
}
179+
180+
public function testCacheStoreWithCustomStrategyAndKeyCanBeConfigured()
181+
{
182+
$container = $this->buildContainer([
183+
'ai' => [
184+
'store' => [
185+
'cache' => [
186+
'my_cache_store_with_custom_strategy' => [
187+
'service' => 'cache.system',
188+
'cache_key' => 'random',
189+
'strategy' => 'chebyshev',
190+
],
191+
],
192+
],
193+
],
194+
]);
195+
196+
$this->assertTrue($container->hasDefinition('ai.store.cache.my_cache_store_with_custom_strategy'));
197+
$this->assertTrue($container->hasDefinition('ai.store.distance_calculator.my_cache_store_with_custom_strategy'));
198+
199+
$definition = $container->getDefinition('ai.store.cache.my_cache_store_with_custom_strategy');
200+
201+
$this->assertCount(3, $definition->getArguments());
202+
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
203+
$this->assertSame('cache.system', (string) $definition->getArgument(0));
204+
$this->assertSame('random', $definition->getArgument(2));
205+
$this->assertInstanceOf(Reference::class, $definition->getArgument(1));
206+
$this->assertSame('ai.store.distance_calculator.my_cache_store_with_custom_strategy', (string) $definition->getArgument(1));
207+
}
208+
209+
public function testInMemoryStoreWithoutCustomStrategyCanBeConfigured()
210+
{
211+
$container = $this->buildContainer([
212+
'ai' => [
213+
'store' => [
214+
'memory' => [
215+
'my_memory_store_with_custom_strategy' => [],
216+
],
217+
],
218+
],
219+
]);
220+
221+
$this->assertTrue($container->hasDefinition('ai.store.memory.my_memory_store_with_custom_strategy'));
222+
223+
$definition = $container->getDefinition('ai.store.memory.my_memory_store_with_custom_strategy');
224+
$this->assertCount(0, $definition->getArguments());
225+
}
226+
227+
public function testInMemoryStoreWithCustomStrategyCanBeConfigured()
228+
{
229+
$container = $this->buildContainer([
230+
'ai' => [
231+
'store' => [
232+
'memory' => [
233+
'my_memory_store_with_custom_strategy' => [
234+
'strategy' => 'chebyshev',
235+
],
236+
],
237+
],
238+
],
239+
]);
240+
241+
$this->assertTrue($container->hasDefinition('ai.store.memory.my_memory_store_with_custom_strategy'));
242+
$this->assertTrue($container->hasDefinition('ai.store.distance_calculator.my_memory_store_with_custom_strategy'));
243+
244+
$definition = $container->getDefinition('ai.store.memory.my_memory_store_with_custom_strategy');
245+
246+
$this->assertCount(1, $definition->getArguments());
247+
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
248+
$this->assertSame('ai.store.distance_calculator.my_memory_store_with_custom_strategy', (string) $definition->getArgument(0));
249+
}
250+
123251
private function buildContainer(array $configuration): ContainerBuilder
124252
{
125253
$container = new ContainerBuilder();
@@ -220,6 +348,19 @@ private function getFullConfig(): array
220348
'my_cache_store' => [
221349
'service' => 'cache.system',
222350
],
351+
'my_cache_store_with_custom_key' => [
352+
'service' => 'cache.system',
353+
'cache_key' => 'bar',
354+
],
355+
'my_cache_store_with_custom_strategy' => [
356+
'service' => 'cache.system',
357+
'strategy' => 'chebyshev',
358+
],
359+
'my_cache_store_with_custom_strategy_and_custom_key' => [
360+
'service' => 'cache.system',
361+
'cache_key' => 'bar',
362+
'strategy' => 'chebyshev',
363+
],
223364
],
224365
'chroma_db' => [
225366
'my_chroma_store' => [
@@ -245,7 +386,7 @@ private function getFullConfig(): array
245386
],
246387
'memory' => [
247388
'my_memory_store' => [
248-
'distance' => 'cosine',
389+
'strategy' => 'cosine',
249390
],
250391
],
251392
'mongodb' => [

src/store/doc/index.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ You can find more advanced usage in combination with an Agent using the store fo
4343
* `Similarity Search with MongoDB (RAG)`_
4444
* `Similarity Search with Neo4j (RAG)`_
4545
* `Similarity Search with Pinecone (RAG)`_
46-
* `Similarity Search with PSR-6 Cache (RAG)`_
4746
* `Similarity Search with Qdrant (RAG)`_
4847
* `Similarity Search with SurrealDB (RAG)`_
48+
* `Similarity Search with Symfony Cache (RAG)`_
4949
* `Similarity Search with Typesense (RAG)`_
5050

5151
.. note::
@@ -66,9 +66,9 @@ Supported Stores
6666
* `Neo4j`_
6767
* `Pinecone`_ (requires `probots-io/pinecone-php` as additional dependency)
6868
* `Postgres`_ (requires `ext-pdo`)
69-
* `PSR-6 Cache`_
7069
* `Qdrant`_
7170
* `SurrealDB`_
71+
* `Symfony Cache`_
7272
* `Typesense`_
7373

7474
.. note::
@@ -109,7 +109,7 @@ This leads to a store implementing two methods::
109109
.. _`Similarity Search with memory storage (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/in-memory.php
110110
.. _`Similarity Search with Neo4j (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/neo4j.php
111111
.. _`Similarity Search with Pinecone (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/pinecone.php
112-
.. _`Similarity Search with PSR-6 Cache (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/cache.php
112+
.. _`Similarity Search with Symfony Cache (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/cache.php
113113
.. _`Similarity Search with Qdrant (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/qdrant.php
114114
.. _`Similarity Search with SurrealDB (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/surrealdb.php
115115
.. _`Similarity Search with Typesense (RAG)`: https://github.com/symfony/ai/blob/main/examples/rag/typesense.php
@@ -126,4 +126,4 @@ This leads to a store implementing two methods::
126126
.. _`Neo4j`: https://neo4j.com/
127127
.. _`Typesense`: https://typesense.org/
128128
.. _`GitHub`: https://github.com/symfony/ai/issues/16
129-
.. _`PSR-6 Cache`: https://www.php-fig.org/psr/psr-6/
129+
.. _`Symfony Cache`: https://symfony.com/doc/current/components/cache.html

src/store/src/CacheStore.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ public function __construct(
2929
private DistanceCalculator $distanceCalculator = new DistanceCalculator(),
3030
private string $cacheKey = '_vectors',
3131
) {
32-
if (!interface_exists(CacheItemPoolInterface::class)) {
33-
throw new RuntimeException('For using the CacheStore as vector store, a PSR-6 cache implementation is required. Try running "composer require symfony/cache" or another PSR-6 compatible cache.');
32+
if (!interface_exists(CacheInterface::class)) {
33+
throw new RuntimeException('For using the CacheStore as vector store, a symfony/contracts cache implementation is required. Try running "composer require symfony/cache" or another symfony/contracts compatible cache.');
3434
}
3535
}
3636

@@ -61,7 +61,7 @@ public function add(VectorDocument ...$documents): void
6161
*/
6262
public function query(Vector $vector, array $options = []): array
6363
{
64-
$documents = $this->cache->getItem($this->cacheKey)->get() ?? [];
64+
$documents = $this->cache->get($this->cacheKey, static fn (): array => []);
6565

6666
$vectorDocuments = array_map(static fn (array $document): VectorDocument => new VectorDocument(
6767
id: Uuid::fromString($document['id']),

0 commit comments

Comments
 (0)