|
15 | 15 | use Symfony\AI\Platform\Vector\Vector; |
16 | 16 | use Symfony\AI\Store\Bridge\Meilisearch\Store; |
17 | 17 | use Symfony\AI\Store\Document\VectorDocument; |
| 18 | +use Symfony\AI\Store\Exception\InvalidArgumentException; |
18 | 19 | use Symfony\Component\HttpClient\Exception\ClientException; |
19 | 20 | use Symfony\Component\HttpClient\MockHttpClient; |
20 | 21 | use Symfony\Component\HttpClient\Response\JsonMockResponse; |
| 22 | +use Symfony\Component\HttpClient\Response\MockResponse; |
21 | 23 | use Symfony\Component\Uid\Uuid; |
22 | 24 |
|
23 | 25 | final class StoreTest extends TestCase |
@@ -275,4 +277,148 @@ public function testMetadataWithoutIDRankingandVector() |
275 | 277 |
|
276 | 278 | $this->assertSame($expected, $vectors[0]->metadata->getArrayCopy()); |
277 | 279 | } |
| 280 | + |
| 281 | + public function testConstructorWithValidSemanticRatio() |
| 282 | + { |
| 283 | + $httpClient = new MockHttpClient(); |
| 284 | + |
| 285 | + $store = new Store($httpClient, 'http://localhost:7700', 'key', 'index', semanticRatio: 0.5); |
| 286 | + |
| 287 | + $this->assertInstanceOf(Store::class, $store); |
| 288 | + } |
| 289 | + |
| 290 | + public function testConstructorThrowsExceptionForInvalidSemanticRatio() |
| 291 | + { |
| 292 | + $this->expectException(InvalidArgumentException::class); |
| 293 | + $this->expectExceptionMessage('The semantic ratio must be between 0.0 and 1.0'); |
| 294 | + |
| 295 | + $httpClient = new MockHttpClient(); |
| 296 | + new Store($httpClient, 'http://localhost:7700', 'key', 'index', semanticRatio: 1.5); |
| 297 | + } |
| 298 | + |
| 299 | + public function testConstructorThrowsExceptionForNegativeSemanticRatio() |
| 300 | + { |
| 301 | + $this->expectException(InvalidArgumentException::class); |
| 302 | + $this->expectExceptionMessage('The semantic ratio must be between 0.0 and 1.0'); |
| 303 | + |
| 304 | + $httpClient = new MockHttpClient(); |
| 305 | + new Store($httpClient, 'http://localhost:7700', 'key', 'index', semanticRatio: -0.1); |
| 306 | + } |
| 307 | + |
| 308 | + public function testQueryUsesDefaultSemanticRatio() |
| 309 | + { |
| 310 | + $responses = [ |
| 311 | + new MockResponse(json_encode([ |
| 312 | + 'hits' => [ |
| 313 | + [ |
| 314 | + 'id' => '550e8400-e29b-41d4-a716-446655440000', |
| 315 | + '_vectors' => [ |
| 316 | + 'default' => [ |
| 317 | + 'embeddings' => [0.1, 0.2, 0.3], |
| 318 | + ], |
| 319 | + ], |
| 320 | + '_rankingScore' => 0.95, |
| 321 | + 'content' => 'Test document', |
| 322 | + ], |
| 323 | + ], |
| 324 | + ])), |
| 325 | + ]; |
| 326 | + |
| 327 | + $httpClient = new MockHttpClient($responses); |
| 328 | + $store = new Store($httpClient, 'http://localhost:7700', 'key', 'index', semanticRatio: 0.7); |
| 329 | + |
| 330 | + $vector = new Vector([0.1, 0.2, 0.3]); |
| 331 | + $store->query($vector); |
| 332 | + |
| 333 | + $request = $httpClient->getRequestsCount() > 0 ? $responses[0]->getRequestOptions() : null; |
| 334 | + $this->assertNotNull($request); |
| 335 | + |
| 336 | + $body = json_decode($request['body'], true); |
| 337 | + $this->assertSame(0.7, $body['hybrid']['semanticRatio']); |
| 338 | + } |
| 339 | + |
| 340 | + public function testQueryCanOverrideSemanticRatio() |
| 341 | + { |
| 342 | + $responses = [ |
| 343 | + new MockResponse(json_encode([ |
| 344 | + 'hits' => [], |
| 345 | + ])), |
| 346 | + ]; |
| 347 | + |
| 348 | + $httpClient = new MockHttpClient($responses); |
| 349 | + $store = new Store($httpClient, 'http://localhost:7700', 'key', 'index', semanticRatio: 0.5); |
| 350 | + |
| 351 | + $vector = new Vector([0.1, 0.2, 0.3]); |
| 352 | + $store->query($vector, ['semanticRatio' => 0.2]); |
| 353 | + |
| 354 | + $request = $responses[0]->getRequestOptions(); |
| 355 | + $body = json_decode($request['body'], true); |
| 356 | + |
| 357 | + $this->assertSame(0.2, $body['hybrid']['semanticRatio']); |
| 358 | + } |
| 359 | + |
| 360 | + public function testQueryThrowsExceptionForInvalidSemanticRatioOption() |
| 361 | + { |
| 362 | + $this->expectException(InvalidArgumentException::class); |
| 363 | + $this->expectExceptionMessage('The semantic ratio must be between 0.0 and 1.0'); |
| 364 | + |
| 365 | + $httpClient = new MockHttpClient(); |
| 366 | + $store = new Store($httpClient, 'http://localhost:7700', 'key', 'index'); |
| 367 | + |
| 368 | + $vector = new Vector([0.1, 0.2, 0.3]); |
| 369 | + $store->query($vector, ['semanticRatio' => 2.0]); |
| 370 | + } |
| 371 | + |
| 372 | + public function testQueryWithPureKeywordSearch() |
| 373 | + { |
| 374 | + $responses = [ |
| 375 | + new MockResponse(json_encode([ |
| 376 | + 'hits' => [ |
| 377 | + [ |
| 378 | + 'id' => '550e8400-e29b-41d4-a716-446655440000', |
| 379 | + '_vectors' => [ |
| 380 | + 'default' => [ |
| 381 | + 'embeddings' => [0.1, 0.2, 0.3], |
| 382 | + ], |
| 383 | + ], |
| 384 | + '_rankingScore' => 0.85, |
| 385 | + 'title' => 'Symfony Framework', |
| 386 | + ], |
| 387 | + ], |
| 388 | + ])), |
| 389 | + ]; |
| 390 | + |
| 391 | + $httpClient = new MockHttpClient($responses); |
| 392 | + $store = new Store($httpClient, 'http://localhost:7700', 'key', 'index'); |
| 393 | + |
| 394 | + $vector = new Vector([0.1, 0.2, 0.3]); |
| 395 | + $results = $store->query($vector, ['semanticRatio' => 0.0]); |
| 396 | + |
| 397 | + $this->assertCount(1, $results); |
| 398 | + $this->assertInstanceOf(VectorDocument::class, $results[0]); |
| 399 | + |
| 400 | + $request = $responses[0]->getRequestOptions(); |
| 401 | + $body = json_decode($request['body'], true); |
| 402 | + $this->assertSame(0.0, $body['hybrid']['semanticRatio']); |
| 403 | + } |
| 404 | + |
| 405 | + public function testQueryWithBalancedHybridSearch() |
| 406 | + { |
| 407 | + $responses = [ |
| 408 | + new MockResponse(json_encode([ |
| 409 | + 'hits' => [], |
| 410 | + ])), |
| 411 | + ]; |
| 412 | + |
| 413 | + $httpClient = new MockHttpClient($responses); |
| 414 | + $store = new Store($httpClient, 'http://localhost:7700', 'key', 'index', semanticRatio: 0.5); |
| 415 | + |
| 416 | + $vector = new Vector([0.1, 0.2, 0.3]); |
| 417 | + $store->query($vector); |
| 418 | + |
| 419 | + $request = $responses[0]->getRequestOptions(); |
| 420 | + $body = json_decode($request['body'], true); |
| 421 | + |
| 422 | + $this->assertSame(0.5, $body['hybrid']['semanticRatio']); |
| 423 | + } |
278 | 424 | } |
0 commit comments