|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Http; |
| 6 | +use Prism\Prism\Enums\Provider; |
| 7 | +use Prism\Prism\Facades\Prism; |
| 8 | +use Prism\Prism\ValueObjects\Media\Image; |
| 9 | +use Tests\Fixtures\FixtureResponse; |
| 10 | + |
| 11 | +// Minimal 1x1 red PNG for testing |
| 12 | +$testImageBase64 = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg=='; |
| 13 | + |
| 14 | +it('returns embeddings from an image using the multimodal endpoint', function () use ($testImageBase64): void { |
| 15 | + FixtureResponse::fakeResponseSequence('*', 'voyageai/multimodal-embeddings-image'); |
| 16 | + |
| 17 | + $image = Image::fromBase64($testImageBase64, 'image/png'); |
| 18 | + |
| 19 | + $response = Prism::embeddings() |
| 20 | + ->using(Provider::VoyageAI, 'voyage-multimodal-3') |
| 21 | + ->fromImage($image) |
| 22 | + ->asEmbeddings(); |
| 23 | + |
| 24 | + expect($response->meta->model)->toBe('voyage-multimodal-3'); |
| 25 | + expect($response->embeddings)->toBeArray(); |
| 26 | + expect($response->embeddings)->toHaveCount(1); |
| 27 | + expect($response->usage->tokens)->toBe(215); |
| 28 | + |
| 29 | + Http::assertSent(fn ($request): bool => str_contains((string) $request->url(), 'multimodalembeddings') |
| 30 | + && $request['model'] === 'voyage-multimodal-3' |
| 31 | + && isset($request['inputs'][0]['content'][0]) |
| 32 | + && $request['inputs'][0]['content'][0]['type'] === 'image_base64'); |
| 33 | +}); |
| 34 | + |
| 35 | +it('returns embeddings from an image URL using the multimodal endpoint', function (): void { |
| 36 | + FixtureResponse::fakeResponseSequence('*', 'voyageai/multimodal-embeddings-image-url'); |
| 37 | + |
| 38 | + $image = Image::fromUrl('https://example.com/photo.jpg'); |
| 39 | + |
| 40 | + $response = Prism::embeddings() |
| 41 | + ->using(Provider::VoyageAI, 'voyage-multimodal-3') |
| 42 | + ->fromImage($image) |
| 43 | + ->asEmbeddings(); |
| 44 | + |
| 45 | + expect($response->meta->model)->toBe('voyage-multimodal-3'); |
| 46 | + expect($response->embeddings)->toBeArray(); |
| 47 | + expect($response->embeddings)->toHaveCount(1); |
| 48 | + |
| 49 | + Http::assertSent(fn ($request): bool => str_contains((string) $request->url(), 'multimodalembeddings') |
| 50 | + && $request['inputs'][0]['content'][0]['type'] === 'image_url' |
| 51 | + && $request['inputs'][0]['content'][0]['image_url'] === 'https://example.com/photo.jpg'); |
| 52 | +}); |
| 53 | + |
| 54 | +it('returns embeddings from text and image combined', function () use ($testImageBase64): void { |
| 55 | + FixtureResponse::fakeResponseSequence('*', 'voyageai/multimodal-embeddings-text-and-image'); |
| 56 | + |
| 57 | + $image = Image::fromBase64($testImageBase64, 'image/png'); |
| 58 | + |
| 59 | + $response = Prism::embeddings() |
| 60 | + ->using(Provider::VoyageAI, 'voyage-multimodal-3') |
| 61 | + ->fromInput('A photo of a sunset') |
| 62 | + ->fromImage($image) |
| 63 | + ->asEmbeddings(); |
| 64 | + |
| 65 | + expect($response->meta->model)->toBe('voyage-multimodal-3'); |
| 66 | + expect($response->embeddings)->toBeArray(); |
| 67 | + expect($response->embeddings)->toHaveCount(2); |
| 68 | + expect($response->usage->tokens)->toBe(223); |
| 69 | + |
| 70 | + Http::assertSent(fn ($request): bool => str_contains((string) $request->url(), 'multimodalembeddings') |
| 71 | + && $request['inputs'][0]['content'][0]['type'] === 'text' |
| 72 | + && $request['inputs'][1]['content'][0]['type'] === 'image_base64'); |
| 73 | +}); |
| 74 | + |
| 75 | +it('uses the text-only endpoint when no images are provided', function (): void { |
| 76 | + FixtureResponse::fakeResponseSequence('*', 'voyageai/embeddings-from-input'); |
| 77 | + |
| 78 | + $response = Prism::embeddings() |
| 79 | + ->using(Provider::VoyageAI, 'voyage-3-lite') |
| 80 | + ->fromInput('The food was delicious and the waiter...') |
| 81 | + ->asEmbeddings(); |
| 82 | + |
| 83 | + expect($response->meta->model)->toBe('voyage-3-lite'); |
| 84 | + |
| 85 | + Http::assertSent(fn ($request): bool => str_contains((string) $request->url(), '/embeddings') |
| 86 | + && ! str_contains((string) $request->url(), 'multimodal') |
| 87 | + && isset($request['input'])); |
| 88 | +}); |
| 89 | + |
| 90 | +it('passes inputType provider option to multimodal endpoint', function () use ($testImageBase64): void { |
| 91 | + FixtureResponse::fakeResponseSequence('*', 'voyageai/multimodal-embeddings-image'); |
| 92 | + |
| 93 | + $image = Image::fromBase64($testImageBase64, 'image/png'); |
| 94 | + |
| 95 | + Prism::embeddings() |
| 96 | + ->using(Provider::VoyageAI, 'voyage-multimodal-3') |
| 97 | + ->fromImage($image) |
| 98 | + ->withProviderOptions(['inputType' => 'document']) |
| 99 | + ->asEmbeddings(); |
| 100 | + |
| 101 | + Http::assertSent(fn ($request): bool => str_contains((string) $request->url(), 'multimodalembeddings') |
| 102 | + && $request['input_type'] === 'document'); |
| 103 | +}); |
0 commit comments