|
21 | 21 | use Symfony\AI\Platform\Model;
|
22 | 22 | use Symfony\AI\Platform\ModelClientInterface;
|
23 | 23 | use Symfony\AI\Platform\Platform;
|
| 24 | +use Symfony\AI\Platform\PlatformInterface; |
24 | 25 | use Symfony\AI\Platform\Result\RawHttpResult;
|
25 | 26 | use Symfony\AI\Platform\Result\RawResultInterface;
|
26 | 27 | use Symfony\AI\Platform\Result\ResultInterface;
|
@@ -379,4 +380,151 @@ public function testVectorizeStringThrowsExceptionWhenNoVectorReturned()
|
379 | 380 |
|
380 | 381 | $vectorizer->vectorize($text);
|
381 | 382 | }
|
| 383 | + |
| 384 | + public function testVectorizeTextDocumentsPassesOptionsToInvoke() |
| 385 | + { |
| 386 | + $documents = [ |
| 387 | + new TextDocument(Uuid::v4(), 'Test document', new Metadata(['source' => 'test'])), |
| 388 | + ]; |
| 389 | + |
| 390 | + $vector = new Vector([0.1, 0.2, 0.3]); |
| 391 | + $options = ['max_tokens' => 1000, 'temperature' => 0.5]; |
| 392 | + |
| 393 | + $platform = $this->createMock(PlatformInterface::class); |
| 394 | + $platform->expects($this->once()) |
| 395 | + ->method('invoke') |
| 396 | + ->with( |
| 397 | + $this->isInstanceOf(Model::class), |
| 398 | + $this->equalTo('Test document'), |
| 399 | + $this->equalTo($options) |
| 400 | + ) |
| 401 | + ->willReturn(new ResultPromise(fn () => new VectorResult($vector), $this->createMock(RawResultInterface::class))); |
| 402 | + |
| 403 | + $model = new Embeddings(); |
| 404 | + |
| 405 | + $vectorizer = new Vectorizer($platform, $model); |
| 406 | + $result = $vectorizer->vectorizeTextDocuments($documents, $options); |
| 407 | + |
| 408 | + $this->assertCount(1, $result); |
| 409 | + $this->assertEquals($vector, $result[0]->vector); |
| 410 | + } |
| 411 | + |
| 412 | + public function testVectorizeTextDocumentsWithEmptyOptions() |
| 413 | + { |
| 414 | + $documents = [ |
| 415 | + new TextDocument(Uuid::v4(), 'Test document'), |
| 416 | + ]; |
| 417 | + |
| 418 | + $vector = new Vector([0.1, 0.2, 0.3]); |
| 419 | + |
| 420 | + $platform = $this->createMock(PlatformInterface::class); |
| 421 | + $platform->expects($this->once()) |
| 422 | + ->method('invoke') |
| 423 | + ->with( |
| 424 | + $this->isInstanceOf(Model::class), |
| 425 | + $this->equalTo('Test document'), |
| 426 | + $this->equalTo([]) |
| 427 | + ) |
| 428 | + ->willReturn(new ResultPromise(fn () => new VectorResult($vector), $this->createMock(RawResultInterface::class))); |
| 429 | + |
| 430 | + $model = new Embeddings(); |
| 431 | + |
| 432 | + $vectorizer = new Vectorizer($platform, $model); |
| 433 | + $result = $vectorizer->vectorizeTextDocuments($documents); |
| 434 | + |
| 435 | + $this->assertCount(1, $result); |
| 436 | + $this->assertEquals($vector, $result[0]->vector); |
| 437 | + } |
| 438 | + |
| 439 | + public function testVectorizeStringPassesOptionsToInvoke() |
| 440 | + { |
| 441 | + $text = 'Test string'; |
| 442 | + $vector = new Vector([0.1, 0.2, 0.3]); |
| 443 | + $options = ['temperature' => 0.7, 'max_tokens' => 500]; |
| 444 | + |
| 445 | + $platform = $this->createMock(PlatformInterface::class); |
| 446 | + $platform->expects($this->once()) |
| 447 | + ->method('invoke') |
| 448 | + ->with( |
| 449 | + $this->isInstanceOf(Model::class), |
| 450 | + $this->equalTo($text), |
| 451 | + $this->equalTo($options) |
| 452 | + ) |
| 453 | + ->willReturn(new ResultPromise(fn () => new VectorResult($vector), $this->createMock(RawResultInterface::class))); |
| 454 | + |
| 455 | + $model = new Embeddings(); |
| 456 | + |
| 457 | + $vectorizer = new Vectorizer($platform, $model); |
| 458 | + $result = $vectorizer->vectorize($text, $options); |
| 459 | + |
| 460 | + $this->assertEquals($vector, $result); |
| 461 | + } |
| 462 | + |
| 463 | + public function testVectorizeStringWithEmptyOptions() |
| 464 | + { |
| 465 | + $text = 'Test string'; |
| 466 | + $vector = new Vector([0.1, 0.2, 0.3]); |
| 467 | + |
| 468 | + $platform = $this->createMock(PlatformInterface::class); |
| 469 | + $platform->expects($this->once()) |
| 470 | + ->method('invoke') |
| 471 | + ->with( |
| 472 | + $this->isInstanceOf(Model::class), |
| 473 | + $this->equalTo($text), |
| 474 | + $this->equalTo([]) |
| 475 | + ) |
| 476 | + ->willReturn(new ResultPromise(fn () => new VectorResult($vector), $this->createMock(RawResultInterface::class))); |
| 477 | + |
| 478 | + $model = new Embeddings(); |
| 479 | + |
| 480 | + $vectorizer = new Vectorizer($platform, $model); |
| 481 | + $result = $vectorizer->vectorize($text); |
| 482 | + |
| 483 | + $this->assertEquals($vector, $result); |
| 484 | + } |
| 485 | + |
| 486 | + public function testVectorizeTextDocumentsWithoutBatchSupportPassesOptions() |
| 487 | + { |
| 488 | + $model = $this->createMock(Model::class); |
| 489 | + $model->expects($this->once()) |
| 490 | + ->method('supports') |
| 491 | + ->with(Capability::INPUT_MULTIPLE) |
| 492 | + ->willReturn(false); |
| 493 | + |
| 494 | + $documents = [ |
| 495 | + new TextDocument(Uuid::v4(), 'Document 1'), |
| 496 | + new TextDocument(Uuid::v4(), 'Document 2'), |
| 497 | + ]; |
| 498 | + |
| 499 | + $vectors = [ |
| 500 | + new Vector([0.1, 0.2]), |
| 501 | + new Vector([0.3, 0.4]), |
| 502 | + ]; |
| 503 | + |
| 504 | + $options = ['max_tokens' => 2000]; |
| 505 | + |
| 506 | + $platform = $this->createMock(PlatformInterface::class); |
| 507 | + |
| 508 | + $invokeCallCount = 0; |
| 509 | + $platform->expects($this->exactly(2)) |
| 510 | + ->method('invoke') |
| 511 | + ->willReturnCallback(function ($passedModel, $passedContent, $passedOptions) use ($options, $vectors, &$invokeCallCount) { |
| 512 | + $this->assertInstanceOf(Model::class, $passedModel); |
| 513 | + $this->assertEquals($options, $passedOptions); |
| 514 | + |
| 515 | + $expectedContent = 0 === $invokeCallCount ? 'Document 1' : 'Document 2'; |
| 516 | + $this->assertEquals($expectedContent, $passedContent); |
| 517 | + |
| 518 | + $vector = $vectors[$invokeCallCount++]; |
| 519 | + |
| 520 | + return new ResultPromise(fn () => new VectorResult($vector), $this->createMock(RawResultInterface::class)); |
| 521 | + }); |
| 522 | + |
| 523 | + $vectorizer = new Vectorizer($platform, $model); |
| 524 | + $result = $vectorizer->vectorizeTextDocuments($documents, $options); |
| 525 | + |
| 526 | + $this->assertCount(2, $result); |
| 527 | + $this->assertEquals($vectors[0], $result[0]->vector); |
| 528 | + $this->assertEquals($vectors[1], $result[1]->vector); |
| 529 | + } |
382 | 530 | }
|
0 commit comments