Skip to content

Commit 828635c

Browse files
committed
fix
1 parent 979983e commit 828635c

File tree

4 files changed

+80
-16
lines changed

4 files changed

+80
-16
lines changed

src/ai-bundle/config/options.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@
167167
->arrayPrototype()
168168
->children()
169169
->scalarNode('service')->cannotBeEmpty()->defaultValue('cache.app')->end()
170-
->scalarNode('key')->end()
170+
->scalarNode('cache_key')->end()
171171
->scalarNode('strategy')->end()
172172
->end()
173173
->end()
@@ -217,7 +217,7 @@
217217
->useAttributeAsKey('name')
218218
->arrayPrototype()
219219
->children()
220-
->scalarNode('distance')->cannotBeEmpty()->end()
220+
->scalarNode('strategy')->cannotBeEmpty()->end()
221221
->end()
222222
->end()
223223
->end()

src/ai-bundle/src/AiBundle.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -497,14 +497,19 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
497497
$arguments = [
498498
new Reference($store['service']),
499499
new Definition(DistanceCalculator::class),
500-
$store['key'] ?? '_vectors',
501500
];
502501

502+
if (\array_key_exists('cache_key', $store) && null !== $store['cache_key']) {
503+
$arguments[2] = $store['cache_key'];
504+
}
505+
503506
if (\array_key_exists('strategy', $store)) {
504-
$distanceCalculatorDefinition = new Definition(DistanceCalculator::class);
505-
$distanceCalculatorDefinition->setArgument(0, DistanceStrategy::from($store['strategy']));
507+
if (!$container->hasDefinition('ai.store.distance_calculator.'.$name)) {
508+
$distanceCalculatorDefinition = new Definition(DistanceCalculator::class);
509+
$distanceCalculatorDefinition->setArgument(0, DistanceStrategy::from($store['strategy']));
506510

507-
$container->setDefinition('ai.store.distance_calculator.'.$name, $distanceCalculatorDefinition);
511+
$container->setDefinition('ai.store.distance_calculator.'.$name, $distanceCalculatorDefinition);
512+
}
508513

509514
$arguments[1] = new Reference('ai.store.distance_calculator.'.$name);
510515
}
@@ -590,9 +595,18 @@ private function processStoreConfig(string $type, array $stores, ContainerBuilde
590595

591596
if ('memory' === $type) {
592597
foreach ($stores as $name => $store) {
593-
$arguments = [
594-
$store['distance'],
595-
];
598+
$arguments = [];
599+
600+
if (\array_key_exists('strategy', $store)) {
601+
if (!$container->hasDefinition('ai.store.distance_calculator.'.$name)) {
602+
$distanceCalculatorDefinition = new Definition(DistanceCalculator::class);
603+
$distanceCalculatorDefinition->setArgument(0, DistanceStrategy::from($store['strategy']));
604+
605+
$container->setDefinition('ai.store.distance_calculator.'.$name, $distanceCalculatorDefinition);
606+
}
607+
608+
$arguments[0] = new Reference('ai.store.distance_calculator.'.$name);
609+
}
596610

597611
$definition = new Definition(InMemoryStore::class);
598612
$definition

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

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ public function testCacheStoreWithCustomKeyCanBeConfigured()
117117
'cache' => [
118118
'my_cache_store_with_custom_strategy' => [
119119
'service' => 'cache.system',
120-
'key' => 'random',
120+
'cache_key' => 'random',
121121
],
122122
],
123123
],
@@ -129,7 +129,7 @@ public function testCacheStoreWithCustomKeyCanBeConfigured()
129129

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

132-
$this->assertCount(2, $definition->getArguments());
132+
$this->assertCount(3, $definition->getArguments());
133133
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
134134
$this->assertSame('cache.system', (string) $definition->getArgument(0));
135135
$this->assertSame('random', $definition->getArgument(2));
@@ -155,10 +155,9 @@ public function testCacheStoreWithCustomStrategyCanBeConfigured()
155155

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

158-
$this->assertCount(3, $definition->getArguments());
158+
$this->assertCount(2, $definition->getArguments());
159159
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
160160
$this->assertSame('cache.system', (string) $definition->getArgument(0));
161-
$this->assertSame('_vectors', $definition->getArgument(2));
162161
$this->assertInstanceOf(Reference::class, $definition->getArgument(1));
163162
$this->assertSame('ai.store.distance_calculator.my_cache_store_with_custom_strategy', (string) $definition->getArgument(1));
164163
}
@@ -171,7 +170,7 @@ public function testCacheStoreWithCustomStrategyAndKeyCanBeConfigured()
171170
'cache' => [
172171
'my_cache_store_with_custom_strategy' => [
173172
'service' => 'cache.system',
174-
'key' => 'random',
173+
'cache_key' => 'random',
175174
'strategy' => 'chebyshev',
176175
],
177176
],
@@ -192,6 +191,48 @@ public function testCacheStoreWithCustomStrategyAndKeyCanBeConfigured()
192191
$this->assertSame('ai.store.distance_calculator.my_cache_store_with_custom_strategy', (string) $definition->getArgument(1));
193192
}
194193

194+
public function testInMemoryStoreWithoutCustomStrategyCanBeConfigured()
195+
{
196+
$container = $this->buildContainer([
197+
'ai' => [
198+
'store' => [
199+
'memory' => [
200+
'my_memory_store_with_custom_strategy' => [],
201+
],
202+
],
203+
],
204+
]);
205+
206+
$this->assertTrue($container->hasDefinition('ai.store.memory.my_memory_store_with_custom_strategy'));
207+
208+
$definition = $container->getDefinition('ai.store.memory.my_memory_store_with_custom_strategy');
209+
$this->assertCount(0, $definition->getArguments());
210+
}
211+
212+
public function testInMemoryStoreWithCustomStrategyCanBeConfigured()
213+
{
214+
$container = $this->buildContainer([
215+
'ai' => [
216+
'store' => [
217+
'memory' => [
218+
'my_memory_store_with_custom_strategy' => [
219+
'strategy' => 'chebyshev',
220+
],
221+
],
222+
],
223+
],
224+
]);
225+
226+
$this->assertTrue($container->hasDefinition('ai.store.memory.my_memory_store_with_custom_strategy'));
227+
$this->assertTrue($container->hasDefinition('ai.store.distance_calculator.my_memory_store_with_custom_strategy'));
228+
229+
$definition = $container->getDefinition('ai.store.memory.my_memory_store_with_custom_strategy');
230+
231+
$this->assertCount(1, $definition->getArguments());
232+
$this->assertInstanceOf(Reference::class, $definition->getArgument(0));
233+
$this->assertSame('ai.store.distance_calculator.my_memory_store_with_custom_strategy', (string) $definition->getArgument(0));
234+
}
235+
195236
private function buildContainer(array $configuration): ContainerBuilder
196237
{
197238
$container = new ContainerBuilder();
@@ -294,12 +335,17 @@ private function getFullConfig(): array
294335
],
295336
'my_cache_store_with_custom_key' => [
296337
'service' => 'cache.system',
297-
'key' => 'bar',
338+
'cache_key' => 'bar',
298339
],
299340
'my_cache_store_with_custom_strategy' => [
300341
'service' => 'cache.system',
301342
'strategy' => 'chebyshev',
302343
],
344+
'my_cache_store_with_custom_strategy_and_custom_key' => [
345+
'service' => 'cache.system',
346+
'cache_key' => 'bar',
347+
'strategy' => 'chebyshev',
348+
],
303349
],
304350
'chroma_db' => [
305351
'my_chroma_store' => [
@@ -325,7 +371,7 @@ private function getFullConfig(): array
325371
],
326372
'memory' => [
327373
'my_memory_store' => [
328-
'distance' => 'cosine',
374+
'strategy' => 'cosine',
329375
],
330376
],
331377
'mongodb' => [

src/store/src/CacheStore.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function __construct(
3232
if (!interface_exists(CacheItemPoolInterface::class)) {
3333
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.');
3434
}
35+
36+
if (!interface_exists(CacheInterface::class)) {
37+
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.');
38+
}
3539
}
3640

3741
public function add(VectorDocument ...$documents): void

0 commit comments

Comments
 (0)