19
19
use Symfony \AI \AiBundle \AiBundle ;
20
20
use Symfony \Component \Config \Definition \Exception \InvalidConfigurationException ;
21
21
use Symfony \Component \DependencyInjection \ContainerBuilder ;
22
+ use Symfony \Component \DependencyInjection \Definition ;
23
+ use Symfony \Component \DependencyInjection \Reference ;
22
24
23
25
#[CoversClass(AiBundle::class)]
24
26
#[UsesClass(ContainerBuilder::class)]
27
+ #[UsesClass(Definition::class)]
28
+ #[UsesClass(Reference::class)]
25
29
class AiBundleTest extends TestCase
26
30
{
27
31
#[DoesNotPerformAssertions]
@@ -120,6 +124,130 @@ public function testAgentsAsToolsCannotDefineService()
120
124
]);
121
125
}
122
126
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
+
123
251
private function buildContainer (array $ configuration ): ContainerBuilder
124
252
{
125
253
$ container = new ContainerBuilder ();
@@ -220,6 +348,19 @@ private function getFullConfig(): array
220
348
'my_cache_store ' => [
221
349
'service ' => 'cache.system ' ,
222
350
],
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
+ ],
223
364
],
224
365
'chroma_db ' => [
225
366
'my_chroma_store ' => [
@@ -245,7 +386,7 @@ private function getFullConfig(): array
245
386
],
246
387
'memory ' => [
247
388
'my_memory_store ' => [
248
- 'distance ' => 'cosine ' ,
389
+ 'strategy ' => 'cosine ' ,
249
390
],
250
391
],
251
392
'mongodb ' => [
0 commit comments