|
1 | 1 | # Embeddings |
2 | 2 |
|
3 | | -Transform your text into powerful vector representations! Embeddings let you add semantic search, recommendation systems, and other advanced natural language features to your applications. |
| 3 | +Transform your content into powerful vector representations! Embeddings let you add semantic search, recommendation systems, and other advanced features to your applications - whether you're working with text or images. |
4 | 4 |
|
5 | 5 | ## Quick Start |
6 | 6 |
|
@@ -86,6 +86,71 @@ $response = Prism::embeddings() |
86 | 86 | > [!NOTE] |
87 | 87 | > Make sure your file exists and is readable. The generator will throw a helpful `PrismException` if there's any issue accessing the file. |
88 | 88 |
|
| 89 | +## Image Embeddings |
| 90 | + |
| 91 | +Some providers support image embeddings, enabling powerful use cases like visual similarity search, cross-modal retrieval, and multimodal applications. Prism makes it easy to generate embeddings from images using the same fluent API. |
| 92 | + |
| 93 | +> [!IMPORTANT] |
| 94 | +> Image embeddings require a provider and model that supports image input (such as CLIP-based models or multimodal embedding models like BGE-VL). Check your provider's documentation to confirm image embedding support. |
| 95 | +
|
| 96 | +### Single Image |
| 97 | + |
| 98 | +Generate an embedding from a single image: |
| 99 | + |
| 100 | +```php |
| 101 | +use Prism\Prism\Facades\Prism; |
| 102 | +use Prism\Prism\ValueObjects\Media\Image; |
| 103 | + |
| 104 | +$response = Prism::embeddings() |
| 105 | + ->using('provider', 'model') |
| 106 | + ->fromImage(Image::fromLocalPath('/path/to/product.jpg')) |
| 107 | + ->asEmbeddings(); |
| 108 | + |
| 109 | +$embedding = $response->embeddings[0]->embedding; |
| 110 | +``` |
| 111 | + |
| 112 | +### Multiple Images |
| 113 | + |
| 114 | +Process multiple images in a single request: |
| 115 | + |
| 116 | +```php |
| 117 | +use Prism\Prism\Facades\Prism; |
| 118 | +use Prism\Prism\ValueObjects\Media\Image; |
| 119 | + |
| 120 | +$response = Prism::embeddings() |
| 121 | + ->using('provider', 'model') |
| 122 | + ->fromImages([ |
| 123 | + Image::fromLocalPath('/path/to/image1.jpg'), |
| 124 | + Image::fromUrl('https://example.com/image2.png'), |
| 125 | + ]) |
| 126 | + ->asEmbeddings(); |
| 127 | + |
| 128 | +foreach ($response->embeddings as $embedding) { |
| 129 | + // Process each image embedding |
| 130 | + $vector = $embedding->embedding; |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +### Multimodal: Text + Image |
| 135 | + |
| 136 | +Combine text and images for cross-modal search scenarios. This is particularly useful for applications like "find products similar to this image that match this description": |
| 137 | + |
| 138 | +```php |
| 139 | +use Prism\Prism\Facades\Prism; |
| 140 | +use Prism\Prism\ValueObjects\Media\Image; |
| 141 | + |
| 142 | +$response = Prism::embeddings() |
| 143 | + ->using('provider', 'model') |
| 144 | + ->fromInput('Find similar products in red') |
| 145 | + ->fromImage(Image::fromBase64($productImage, 'image/png')) |
| 146 | + ->asEmbeddings(); |
| 147 | +``` |
| 148 | + |
| 149 | +You can chain `fromImage()` and `fromInput()` in any order - Prism handles both gracefully. |
| 150 | + |
| 151 | +> [!TIP] |
| 152 | +> The `Image` class supports multiple input sources: `fromLocalPath()`, `fromUrl()`, `fromBase64()`, `fromStoragePath()`, and `fromRawContent()`. See the [Images documentation](/input-modalities/images.html) for details. |
| 153 | +
|
89 | 154 | ## Common Settings |
90 | 155 |
|
91 | 156 | Just like with text generation, you can fine-tune your embeddings requests: |
@@ -146,7 +211,7 @@ try { |
146 | 211 | } |
147 | 212 | ``` |
148 | 213 |
|
149 | | -## Pro Tips 🌟 |
| 214 | +## Pro Tips |
150 | 215 |
|
151 | 216 | **Vector Storage**: Consider using a vector database like Milvus, Qdrant, or pgvector to store and query your embeddings efficiently. |
152 | 217 |
|
|
0 commit comments