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]
@@ -105,6 +109,130 @@ public function testAgentsAsToolsCannotDefineService()
105
109
]);
106
110
}
107
111
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
+ 'cache_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 (3 , $ 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 (2 , $ definition ->getArguments ());
159
+ $ this ->assertInstanceOf (Reference::class, $ definition ->getArgument (0 ));
160
+ $ this ->assertSame ('cache.system ' , (string ) $ definition ->getArgument (0 ));
161
+ $ this ->assertInstanceOf (Reference::class, $ definition ->getArgument (1 ));
162
+ $ this ->assertSame ('ai.store.distance_calculator.my_cache_store_with_custom_strategy ' , (string ) $ definition ->getArgument (1 ));
163
+ }
164
+
165
+ public function testCacheStoreWithCustomStrategyAndKeyCanBeConfigured ()
166
+ {
167
+ $ container = $ this ->buildContainer ([
168
+ 'ai ' => [
169
+ 'store ' => [
170
+ 'cache ' => [
171
+ 'my_cache_store_with_custom_strategy ' => [
172
+ 'service ' => 'cache.system ' ,
173
+ 'cache_key ' => 'random ' ,
174
+ 'strategy ' => 'chebyshev ' ,
175
+ ],
176
+ ],
177
+ ],
178
+ ],
179
+ ]);
180
+
181
+ $ this ->assertTrue ($ container ->hasDefinition ('ai.store.cache.my_cache_store_with_custom_strategy ' ));
182
+ $ this ->assertTrue ($ container ->hasDefinition ('ai.store.distance_calculator.my_cache_store_with_custom_strategy ' ));
183
+
184
+ $ definition = $ container ->getDefinition ('ai.store.cache.my_cache_store_with_custom_strategy ' );
185
+
186
+ $ this ->assertCount (3 , $ definition ->getArguments ());
187
+ $ this ->assertInstanceOf (Reference::class, $ definition ->getArgument (0 ));
188
+ $ this ->assertSame ('cache.system ' , (string ) $ definition ->getArgument (0 ));
189
+ $ this ->assertSame ('random ' , $ definition ->getArgument (2 ));
190
+ $ this ->assertInstanceOf (Reference::class, $ definition ->getArgument (1 ));
191
+ $ this ->assertSame ('ai.store.distance_calculator.my_cache_store_with_custom_strategy ' , (string ) $ definition ->getArgument (1 ));
192
+ }
193
+
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
+
108
236
private function buildContainer (array $ configuration ): ContainerBuilder
109
237
{
110
238
$ container = new ContainerBuilder ();
@@ -205,6 +333,19 @@ private function getFullConfig(): array
205
333
'my_cache_store ' => [
206
334
'service ' => 'cache.system ' ,
207
335
],
336
+ 'my_cache_store_with_custom_key ' => [
337
+ 'service ' => 'cache.system ' ,
338
+ 'cache_key ' => 'bar ' ,
339
+ ],
340
+ 'my_cache_store_with_custom_strategy ' => [
341
+ 'service ' => 'cache.system ' ,
342
+ 'strategy ' => 'chebyshev ' ,
343
+ ],
344
+ 'my_cache_store_with_custom_strategy_and_custom_key ' => [
345
+ 'service ' => 'cache.system ' ,
346
+ 'cache_key ' => 'bar ' ,
347
+ 'strategy ' => 'chebyshev ' ,
348
+ ],
208
349
],
209
350
'chroma_db ' => [
210
351
'my_chroma_store ' => [
@@ -230,7 +371,7 @@ private function getFullConfig(): array
230
371
],
231
372
'memory ' => [
232
373
'my_memory_store ' => [
233
- 'distance ' => 'cosine ' ,
374
+ 'strategy ' => 'cosine ' ,
234
375
],
235
376
],
236
377
'mongodb ' => [
0 commit comments