13
13
14
14
use PHPUnit \Framework \Attributes \CoversClass ;
15
15
use PHPUnit \Framework \TestCase ;
16
+ use Symfony \AI \Platform \Vector \Vector ;
16
17
use Symfony \AI \Store \Bridge \Neo4j \Store ;
18
+ use Symfony \AI \Store \Document \VectorDocument ;
17
19
use Symfony \Component \HttpClient \Exception \ClientException ;
18
20
use Symfony \Component \HttpClient \MockHttpClient ;
19
21
use Symfony \Component \HttpClient \Response \JsonMockResponse ;
22
+ use Symfony \Component \Uid \Uuid ;
20
23
21
24
#[CoversClass(Store::class)]
22
25
final class StoreTest extends TestCase
@@ -36,4 +39,229 @@ public function testStoreCannotInitializeOnInvalidResponse(): void
36
39
self ::expectExceptionCode (400 );
37
40
$ store ->initialize ();
38
41
}
42
+
43
+ public function testStoreCanInitialize (): void
44
+ {
45
+ $ httpClient = new MockHttpClient ([
46
+ new JsonMockResponse ([
47
+ 'data ' => [
48
+ 'fields ' => [],
49
+ 'values ' => [],
50
+ ],
51
+ 'bookmarks ' => [
52
+ 'FB:kcwQ5zbxUD1ESXmS6UjG2xKCZMkAoJB= ' ,
53
+ ],
54
+ ], [
55
+ 'http_code ' => 200 ,
56
+ ]),
57
+ new JsonMockResponse ([
58
+ 'data ' => [
59
+ 'fields ' => [],
60
+ 'values ' => [],
61
+ ],
62
+ 'notifications ' => [
63
+ [
64
+ 'code ' => 'Neo.ClientNotification.Schema.IndexOrConstraintAlreadyExists ' ,
65
+ 'description ' => '`VECTOR INDEX movies FOR (e:symfony) ON (e.symfony)` already exists. ' ,
66
+ 'severity ' => 'INFORMATION ' ,
67
+ 'title ' => '"`CREATE VECTOR INDEX movies IF NOT EXISTS FOR (symfony:symfony) ON (symfony.symfony) OPTIONS {indexConfig: {`vector.dimensions`: 1536, `vector.similarity_function`: "cosine", `vector.quantization.enabled`: false}}` has no effect. ' ,
68
+ 'position ' => null ,
69
+ 'category ' => 'SCHEMA ' ,
70
+ ],
71
+ ],
72
+ 'bookmarks ' => [
73
+ 'FB:kcwQ5zbxUD1ESXmS6UjG2xKCZMkAoJA= ' ,
74
+ ],
75
+ ], [
76
+ 'http_code ' => 202 ,
77
+ ]),
78
+ ], 'http://localhost:7474 ' );
79
+
80
+ $ store = new Store ($ httpClient , 'http://localhost:7474 ' , 'symfony ' , 'symfony ' , 'symfony ' , 'symfony ' , 'symfony ' );
81
+
82
+ $ store ->initialize ();
83
+ $ store ->initialize ();
84
+
85
+ self ::assertSame (2 , $ httpClient ->getRequestsCount ());
86
+ }
87
+
88
+ public function testStoreCanAdd (): void
89
+ {
90
+ $ httpClient = new MockHttpClient ([
91
+ new JsonMockResponse ([
92
+ 'data ' => [
93
+ 'fields ' => [],
94
+ 'values ' => [],
95
+ ],
96
+ 'bookmarks ' => [
97
+ 'FB:kcwQ5zbxUD1ESXmS6UjG2xKCZMkAoJB= ' ,
98
+ ],
99
+ ], [
100
+ 'http_code ' => 200 ,
101
+ ]),
102
+ new JsonMockResponse ([
103
+ 'data ' => [
104
+ 'fields ' => [
105
+ 'n ' ,
106
+ ],
107
+ 'values ' => [
108
+ [
109
+ [
110
+ 'elementId ' => '4: ' .Uuid::v4 ()->toRfc4122 (),
111
+ 'labels ' => [
112
+ 'symfony ' ,
113
+ ],
114
+ 'properties ' => [
115
+ 'embeddings ' => [0.1 , 0.2 , 0.3 ],
116
+ 'metadata ' => [],
117
+ 'id ' => Uuid::v4 ()->toRfc4122 (),
118
+ ],
119
+ ],
120
+ ],
121
+ ],
122
+ ],
123
+ 'bookmarks ' => [
124
+ 'FB:kcwQdJAosIhGT0yRm+Na1gMjaQqQ ' ,
125
+ ],
126
+ ], [
127
+ 'http_code ' => 200 ,
128
+ ]),
129
+ new JsonMockResponse ([
130
+ 'data ' => [
131
+ 'fields ' => [
132
+ 'n ' ,
133
+ ],
134
+ 'values ' => [
135
+ [
136
+ [
137
+ 'elementId ' => '4: ' .Uuid::v4 ()->toRfc4122 (),
138
+ 'labels ' => [
139
+ 'symfony ' ,
140
+ ],
141
+ 'properties ' => [
142
+ 'embeddings ' => [0.1 , 0.2 , 0.3 ],
143
+ 'metadata ' => [],
144
+ 'id ' => Uuid::v4 ()->toRfc4122 (),
145
+ ],
146
+ ],
147
+ ],
148
+ ],
149
+ ],
150
+ 'bookmarks ' => [
151
+ 'FB:kcwQdJAosIhGT0yRm+Na1gMjaQqS ' ,
152
+ ],
153
+ ], [
154
+ 'http_code ' => 200 ,
155
+ ]),
156
+ ], 'http://localhost:7474 ' );
157
+
158
+ $ store = new Store ($ httpClient , 'http://localhost:7474 ' , 'symfony ' , 'symfony ' , 'symfony ' , 'symfony ' , 'symfony ' );
159
+
160
+ $ store ->initialize ();
161
+ $ store ->add (new VectorDocument (Uuid::v4 (), new Vector ([0.1 , 0.2 , 0.3 ])));
162
+ $ store ->add (new VectorDocument (Uuid::v4 (), new Vector ([0.1 , 0.2 , 0.3 ])));
163
+
164
+ self ::assertSame (3 , $ httpClient ->getRequestsCount ());
165
+ }
166
+
167
+ public function testStoreCanQuery (): void
168
+ {
169
+ $ httpClient = new MockHttpClient ([
170
+ new JsonMockResponse ([
171
+ 'data ' => [
172
+ 'fields ' => [],
173
+ 'values ' => [],
174
+ ],
175
+ 'bookmarks ' => [
176
+ 'FB:kcwQ5zbxUD1ESXmS6UjG2xKCZMkAoJB= ' ,
177
+ ],
178
+ ], [
179
+ 'http_code ' => 200 ,
180
+ ]),
181
+ new JsonMockResponse ([
182
+ 'data ' => [
183
+ 'fields ' => [
184
+ 'n ' ,
185
+ ],
186
+ 'values ' => [
187
+ [
188
+ [
189
+ 'elementId ' => '4: ' .Uuid::v4 ()->toRfc4122 (),
190
+ 'labels ' => [
191
+ 'symfony ' ,
192
+ ],
193
+ 'properties ' => [
194
+ 'embeddings ' => [0.1 , 0.2 , 0.3 ],
195
+ 'metadata ' => [],
196
+ 'id ' => Uuid::v4 ()->toRfc4122 (),
197
+ ],
198
+ ],
199
+ ],
200
+ ],
201
+ ],
202
+ 'bookmarks ' => [
203
+ 'FB:kcwQdJAosIhGT0yRm+Na1gMjaQqR ' ,
204
+ ],
205
+ ], [
206
+ 'http_code ' => 200 ,
207
+ ]),
208
+ new JsonMockResponse ([
209
+ 'data ' => [
210
+ 'fields ' => [
211
+ 'node ' ,
212
+ 'score ' ,
213
+ ],
214
+ 'values ' => [
215
+ [
216
+ [
217
+ 'elementId ' => '4: ' .Uuid::v4 ()->toRfc4122 (),
218
+ 'labels ' => [
219
+ 'symfony ' ,
220
+ ],
221
+ 'properties ' => [
222
+ 'embeddings ' => [0.1 , 0.2 , 0.3 ],
223
+ 'metadata ' => json_encode ([
224
+ 'foo ' => 'bar ' ,
225
+ ]),
226
+ 'id ' => Uuid::v4 ()->toRfc4122 (),
227
+ ],
228
+ ],
229
+ 0.1 ,
230
+ ],
231
+ [
232
+ [
233
+ 'elementId ' => '4: ' .Uuid::v4 ()->toRfc4122 (),
234
+ 'labels ' => [
235
+ 'symfony ' ,
236
+ ],
237
+ 'properties ' => [
238
+ 'embeddings ' => [0.1 , 0.2 , 0.3 ],
239
+ 'metadata ' => json_encode ([
240
+ 'foo ' => 'bar ' ,
241
+ ]),
242
+ 'id ' => Uuid::v4 ()->toRfc4122 (),
243
+ ],
244
+ ],
245
+ 0.1 ,
246
+ ],
247
+ ],
248
+ ],
249
+ 'bookmarks ' => [
250
+ 'FB:kcwQdJAosIhGT0yRm+Na1gMjaQqT ' ,
251
+ ],
252
+ ], [
253
+ 'http_code ' => 200 ,
254
+ ]),
255
+ ], 'http://localhost:7474 ' );
256
+
257
+ $ store = new Store ($ httpClient , 'http://localhost:7474 ' , 'symfony ' , 'symfony ' , 'symfony ' , 'symfony ' , 'symfony ' );
258
+
259
+ $ store ->initialize ();
260
+ $ store ->add (new VectorDocument (Uuid::v4 (), new Vector ([0.1 , 0.2 , 0.3 ])));
261
+
262
+ $ results = $ store ->query (new Vector ([0.1 , 0.2 , 0.3 ]));
263
+
264
+ self ::assertCount (2 , $ results );
265
+ self ::assertSame (3 , $ httpClient ->getRequestsCount ());
266
+ }
39
267
}
0 commit comments