|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Endpoints; |
| 6 | + |
| 7 | +use Meilisearch\Endpoints\Indexes; |
| 8 | +use Meilisearch\Http\Client; |
| 9 | +use Tests\TestCase; |
| 10 | + |
| 11 | +final class MultiModalSearchTest extends TestCase |
| 12 | +{ |
| 13 | + private Indexes $index; |
| 14 | + private array $documents; |
| 15 | + |
| 16 | + protected function setUp(): void |
| 17 | + { |
| 18 | + parent::setUp(); |
| 19 | + |
| 20 | + $http = new Client($this->host, getenv('MEILISEARCH_API_KEY')); |
| 21 | + $http->patch('/experimental-features', ['multimodal' => true]); |
| 22 | + |
| 23 | + $voyageApiKey = getenv('VOYAGE_API_KEY'); |
| 24 | + if (false === $voyageApiKey || '' === $voyageApiKey) { |
| 25 | + self::markTestSkipped('Missing `VOYAGE_API_KEY` environment variable'); |
| 26 | + } |
| 27 | + |
| 28 | + $this->index = $this->createEmptyIndex($this->safeIndexName()); |
| 29 | + $updateSettingsPromise = $this->index->updateSettings([ |
| 30 | + 'searchableAttributes' => ['title', 'overview'], |
| 31 | + 'embedders' => [ |
| 32 | + 'multimodal' => self::getVoyageEmbedderConfig($voyageApiKey), |
| 33 | + ], |
| 34 | + ]); |
| 35 | + $this->index->waitForTask($updateSettingsPromise['taskUid']); |
| 36 | + |
| 37 | + // Load the movies.json dataset |
| 38 | + $documentsJson = file_get_contents('./tests/datasets/movies.json'); |
| 39 | + $this->documents = json_decode($documentsJson, true, 512, JSON_THROW_ON_ERROR); |
| 40 | + $addDocumentsPromise = $this->index->addDocuments($this->documents); |
| 41 | + $this->index->waitForTask($addDocumentsPromise['taskUid']); |
| 42 | + } |
| 43 | + |
| 44 | + public function testTextOnlySearch(): void |
| 45 | + { |
| 46 | + $query = 'A movie with lightsabers in space'; |
| 47 | + $response = $this->index->search($query, [ |
| 48 | + 'media' => [ |
| 49 | + 'text' => ['text' => $query], |
| 50 | + ], |
| 51 | + 'hybrid' => [ |
| 52 | + 'embedder' => 'multimodal', |
| 53 | + 'semanticRatio' => 1, |
| 54 | + ], |
| 55 | + ]); |
| 56 | + self::assertSame('Star Wars', $response->getHits()[0]['title']); |
| 57 | + } |
| 58 | + |
| 59 | + public function testImageOnlySearch(): void |
| 60 | + { |
| 61 | + $theFifthElementPoster = $this->documents[3]['poster']; |
| 62 | + $response = $this->index->search(null, [ |
| 63 | + 'media' => [ |
| 64 | + 'poster' => [ |
| 65 | + 'poster' => $theFifthElementPoster, |
| 66 | + ], |
| 67 | + ], |
| 68 | + 'hybrid' => [ |
| 69 | + 'embedder' => 'multimodal', |
| 70 | + 'semanticRatio' => 1, |
| 71 | + ], |
| 72 | + ]); |
| 73 | + self::assertSame('The Fifth Element', $response->getHits()[0]['title']); |
| 74 | + } |
| 75 | + |
| 76 | + public function testTextAndImageSearch(): void |
| 77 | + { |
| 78 | + $query = 'a futuristic movie'; |
| 79 | + $masterYodaBase64 = base64_encode(file_get_contents('./tests/assets/master-yoda.jpeg')); |
| 80 | + $response = $this->index->search(null, [ |
| 81 | + 'media' => [ |
| 82 | + 'textAndPoster' => [ |
| 83 | + 'text' => $query, |
| 84 | + 'image' => [ |
| 85 | + 'mime' => 'image/jpeg', |
| 86 | + 'data' => $masterYodaBase64, |
| 87 | + ], |
| 88 | + ], |
| 89 | + ], |
| 90 | + 'hybrid' => [ |
| 91 | + 'embedder' => 'multimodal', |
| 92 | + 'semanticRatio' => 1, |
| 93 | + ], |
| 94 | + ]); |
| 95 | + self::assertSame('Star Wars', $response->getHits()[0]['title']); |
| 96 | + } |
| 97 | + |
| 98 | + private static function getVoyageEmbedderConfig(string $voyageApiKey): array |
| 99 | + { |
| 100 | + return [ |
| 101 | + 'source' => 'rest', |
| 102 | + 'url' => 'https://api.voyageai.com/v1/multimodalembeddings', |
| 103 | + 'apiKey' => $voyageApiKey, |
| 104 | + 'dimensions' => 1024, |
| 105 | + 'indexingFragments' => [ |
| 106 | + 'textAndPoster' => [ |
| 107 | + // the shape of the data here depends on the model used |
| 108 | + 'value' => [ |
| 109 | + 'content' => [ |
| 110 | + [ |
| 111 | + 'type' => 'text', |
| 112 | + 'text' => 'A movie titled {{doc.title}} whose description starts with {{doc.overview|truncatewords:20}}.', |
| 113 | + ], |
| 114 | + [ |
| 115 | + 'type' => 'image_url', |
| 116 | + 'image_url' => '{{doc.poster}}', |
| 117 | + ], |
| 118 | + ], |
| 119 | + ], |
| 120 | + ], |
| 121 | + 'text' => [ |
| 122 | + 'value' => [ |
| 123 | + // The shape of the data here depends on the model used |
| 124 | + 'content' => [ |
| 125 | + [ |
| 126 | + 'type' => 'text', |
| 127 | + 'text' => 'A movie titled {{doc.title}} whose description starts with {{doc.overview|truncatewords:20}}.', |
| 128 | + ], |
| 129 | + ], |
| 130 | + ], |
| 131 | + ], |
| 132 | + 'poster' => [ |
| 133 | + 'value' => [ |
| 134 | + // The shape of the data here depends on the model used |
| 135 | + 'content' => [ |
| 136 | + [ |
| 137 | + 'type' => 'image_url', |
| 138 | + 'image_url' => '{{doc.poster}}', |
| 139 | + ], |
| 140 | + ], |
| 141 | + ], |
| 142 | + ], |
| 143 | + ], |
| 144 | + 'searchFragments' => [ |
| 145 | + 'textAndPoster' => [ |
| 146 | + 'value' => [ |
| 147 | + 'content' => [ |
| 148 | + [ |
| 149 | + 'type' => 'text', |
| 150 | + 'text' => '{{media.textAndPoster.text}}', |
| 151 | + ], |
| 152 | + [ |
| 153 | + 'type' => 'image_base64', |
| 154 | + 'image_base64' => 'data:{{media.textAndPoster.image.mime}};base64,{{media.textAndPoster.image.data}}', |
| 155 | + ], |
| 156 | + ], |
| 157 | + ], |
| 158 | + ], |
| 159 | + 'text' => [ |
| 160 | + 'value' => [ |
| 161 | + 'content' => [ |
| 162 | + [ |
| 163 | + 'type' => 'text', |
| 164 | + 'text' => '{{media.text.text}}', |
| 165 | + ], |
| 166 | + ], |
| 167 | + ], |
| 168 | + ], |
| 169 | + 'poster' => [ |
| 170 | + 'value' => [ |
| 171 | + 'content' => [ |
| 172 | + [ |
| 173 | + 'type' => 'image_url', |
| 174 | + 'image_url' => '{{media.poster.poster}}', |
| 175 | + ], |
| 176 | + ], |
| 177 | + ], |
| 178 | + ], |
| 179 | + ], |
| 180 | + 'request' => [ |
| 181 | + // This request object matches the Voyage API request object |
| 182 | + 'inputs' => ['{{fragment}}', '{{..}}'], |
| 183 | + 'model' => 'voyage-multimodal-3', |
| 184 | + ], |
| 185 | + 'response' => [ |
| 186 | + // This response object matches the Voyage API response object |
| 187 | + 'data' => [ |
| 188 | + [ |
| 189 | + 'embedding' => '{{embedding}}', |
| 190 | + ], |
| 191 | + '{{..}}', |
| 192 | + ], |
| 193 | + ], |
| 194 | + ]; |
| 195 | + } |
| 196 | +} |
0 commit comments