|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\AI\Agent\Tests\Toolbox\Tool; |
| 13 | + |
| 14 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 15 | +use PHPUnit\Framework\Attributes\Test; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | +use Symfony\AI\Agent\Toolbox\Tool\SimilaritySearch; |
| 18 | +use Symfony\AI\Platform\Model; |
| 19 | +use Symfony\AI\Platform\PlatformInterface; |
| 20 | +use Symfony\AI\Platform\Result\RawResultInterface; |
| 21 | +use Symfony\AI\Platform\Result\ResultPromise; |
| 22 | +use Symfony\AI\Platform\Result\VectorResult; |
| 23 | +use Symfony\AI\Platform\Vector\Vector; |
| 24 | +use Symfony\AI\Store\Document\Metadata; |
| 25 | +use Symfony\AI\Store\Document\VectorDocument; |
| 26 | +use Symfony\AI\Store\VectorStoreInterface; |
| 27 | +use Symfony\Component\Uid\Uuid; |
| 28 | + |
| 29 | +#[CoversClass(SimilaritySearch::class)] |
| 30 | +final class SimilaritySearchTest extends TestCase |
| 31 | +{ |
| 32 | + #[Test] |
| 33 | + public function searchWithResults(): void |
| 34 | + { |
| 35 | + $searchTerm = 'find similar documents'; |
| 36 | + $vector = new Vector([0.1, 0.2, 0.3]); |
| 37 | + |
| 38 | + $document1 = new VectorDocument( |
| 39 | + Uuid::v4(), |
| 40 | + $vector, |
| 41 | + new Metadata(['title' => 'Document 1', 'content' => 'First document content']), |
| 42 | + ); |
| 43 | + $document2 = new VectorDocument( |
| 44 | + Uuid::v4(), |
| 45 | + $vector, |
| 46 | + new Metadata(['title' => 'Document 2', 'content' => 'Second document content']), |
| 47 | + ); |
| 48 | + |
| 49 | + $rawResult = $this->createMock(RawResultInterface::class); |
| 50 | + $vectorResult = new VectorResult($vector); |
| 51 | + $resultPromise = new ResultPromise( |
| 52 | + fn () => $vectorResult, |
| 53 | + $rawResult |
| 54 | + ); |
| 55 | + |
| 56 | + $platform = $this->createMock(PlatformInterface::class); |
| 57 | + $platform->expects($this->once()) |
| 58 | + ->method('invoke') |
| 59 | + ->with($this->isInstanceOf(Model::class), $searchTerm) |
| 60 | + ->willReturn($resultPromise); |
| 61 | + |
| 62 | + $vectorStore = $this->createMock(VectorStoreInterface::class); |
| 63 | + $vectorStore->expects($this->once()) |
| 64 | + ->method('query') |
| 65 | + ->with($vector) |
| 66 | + ->willReturn([$document1, $document2]); |
| 67 | + |
| 68 | + $model = new Model('test-model'); |
| 69 | + $similaritySearch = new SimilaritySearch($platform, $model, $vectorStore); |
| 70 | + |
| 71 | + $result = $similaritySearch($searchTerm); |
| 72 | + |
| 73 | + $this->assertSame('Found documents with following information:'.\PHP_EOL.'{"title":"Document 1","content":"First document content"}{"title":"Document 2","content":"Second document content"}', $result); |
| 74 | + $this->assertSame([$document1, $document2], $similaritySearch->usedDocuments); |
| 75 | + } |
| 76 | + |
| 77 | + #[Test] |
| 78 | + public function searchWithoutResults(): void |
| 79 | + { |
| 80 | + $searchTerm = 'find nothing'; |
| 81 | + $vector = new Vector([0.1, 0.2, 0.3]); |
| 82 | + |
| 83 | + $rawResult = $this->createMock(RawResultInterface::class); |
| 84 | + $vectorResult = new VectorResult($vector); |
| 85 | + $resultPromise = new ResultPromise( |
| 86 | + fn () => $vectorResult, |
| 87 | + $rawResult |
| 88 | + ); |
| 89 | + |
| 90 | + $platform = $this->createMock(PlatformInterface::class); |
| 91 | + $platform->expects($this->once()) |
| 92 | + ->method('invoke') |
| 93 | + ->with($this->isInstanceOf(Model::class), $searchTerm) |
| 94 | + ->willReturn($resultPromise); |
| 95 | + |
| 96 | + $vectorStore = $this->createMock(VectorStoreInterface::class); |
| 97 | + $vectorStore->expects($this->once()) |
| 98 | + ->method('query') |
| 99 | + ->with($vector) |
| 100 | + ->willReturn([]); |
| 101 | + |
| 102 | + $model = new Model('test-model'); |
| 103 | + $similaritySearch = new SimilaritySearch($platform, $model, $vectorStore); |
| 104 | + |
| 105 | + $result = $similaritySearch($searchTerm); |
| 106 | + |
| 107 | + $this->assertSame('No results found', $result); |
| 108 | + $this->assertSame([], $similaritySearch->usedDocuments); |
| 109 | + } |
| 110 | + |
| 111 | + #[Test] |
| 112 | + public function searchWithSingleResult(): void |
| 113 | + { |
| 114 | + $searchTerm = 'specific query'; |
| 115 | + $vector = new Vector([0.5, 0.6, 0.7]); |
| 116 | + |
| 117 | + $document = new VectorDocument( |
| 118 | + Uuid::v4(), |
| 119 | + $vector, |
| 120 | + new Metadata(['title' => 'Single Document', 'description' => 'Only one match']), |
| 121 | + ); |
| 122 | + |
| 123 | + $rawResult = $this->createMock(RawResultInterface::class); |
| 124 | + $vectorResult = new VectorResult($vector); |
| 125 | + $resultPromise = new ResultPromise( |
| 126 | + fn () => $vectorResult, |
| 127 | + $rawResult |
| 128 | + ); |
| 129 | + |
| 130 | + $platform = $this->createMock(PlatformInterface::class); |
| 131 | + $platform->expects($this->once()) |
| 132 | + ->method('invoke') |
| 133 | + ->with($this->isInstanceOf(Model::class), $searchTerm) |
| 134 | + ->willReturn($resultPromise); |
| 135 | + |
| 136 | + $vectorStore = $this->createMock(VectorStoreInterface::class); |
| 137 | + $vectorStore->expects($this->once()) |
| 138 | + ->method('query') |
| 139 | + ->with($vector) |
| 140 | + ->willReturn([$document]); |
| 141 | + |
| 142 | + $model = new Model('test-model'); |
| 143 | + $similaritySearch = new SimilaritySearch($platform, $model, $vectorStore); |
| 144 | + |
| 145 | + $result = $similaritySearch($searchTerm); |
| 146 | + |
| 147 | + $this->assertSame('Found documents with following information:'.\PHP_EOL.'{"title":"Single Document","description":"Only one match"}', $result); |
| 148 | + $this->assertSame([$document], $similaritySearch->usedDocuments); |
| 149 | + } |
| 150 | +} |
0 commit comments