|
| 1 | +--- |
| 2 | +title: How to generate image embeddings with Azure AI model inference |
| 3 | +titleSuffix: Azure AI Foundry |
| 4 | +description: Learn how to generate embeddings with Azure AI model inference |
| 5 | +manager: scottpolly |
| 6 | +author: msakande |
| 7 | +reviewer: santiagxf |
| 8 | +ms.service: azure-ai-model-inference |
| 9 | +ms.topic: how-to |
| 10 | +ms.date: 01/22/2025 |
| 11 | +ms.author: mopeakande |
| 12 | +ms.reviewer: fasantia |
| 13 | +ms.custom: generated |
| 14 | +zone_pivot_groups: azure-ai-inference-samples |
| 15 | +--- |
| 16 | + |
| 17 | +[!INCLUDE [Feature preview](~/reusable-content/ce-skilling/azure/includes/ai-studio/includes/feature-preview.md)] |
| 18 | + |
| 19 | +This article explains how to use image embeddings API with models deployed to Azure AI model inference in Azure AI Foundry. |
| 20 | + |
| 21 | +## Prerequisites |
| 22 | + |
| 23 | +To use embedding models in your application, you need: |
| 24 | + |
| 25 | +[!INCLUDE [how-to-prerequisites](../how-to-prerequisites.md)] |
| 26 | + |
| 27 | +* An image embeddings model deployment. If you don't have one read [Add and configure models to Azure AI services](../../how-to/create-model-deployments.md) to add an embeddings model to your resource. |
| 28 | + |
| 29 | + * This example uses `Cohere-embed-v3-english` from Cohere. |
| 30 | + |
| 31 | +## Use image embeddings |
| 32 | + |
| 33 | +To use the text embeddings, use the route `/images/embeddings` appended to your base URL along with your credential indicated in `api-key`. `Authorization` header is also supported with the format `Bearer <key>`. |
| 34 | + |
| 35 | +```http |
| 36 | +POST https://<resource>.services.ai.azure.com/models/images/embeddings?api-version=2024-05-01-preview |
| 37 | +Content-Type: application/json |
| 38 | +api-key: <key> |
| 39 | +``` |
| 40 | + |
| 41 | +If you configured the resource with **Microsoft Entra ID** support, pass you token in the `Authorization` header: |
| 42 | + |
| 43 | +```http |
| 44 | +POST https://<resource>.services.ai.azure.com/models/images/embeddings?api-version=2024-05-01-preview |
| 45 | +Content-Type: application/json |
| 46 | +Authorization: Bearer <token> |
| 47 | +``` |
| 48 | + |
| 49 | +### Create embeddings |
| 50 | + |
| 51 | +To create image embeddings, you need to pass the image data as part of your request. Image data should be in PNG format and encoded as base64. |
| 52 | + |
| 53 | +```json |
| 54 | +{ |
| 55 | + "model": "Cohere-embed-v3-english", |
| 56 | + "input": [ |
| 57 | + { |
| 58 | + "image": "data:image/png;base64,iVBORw0KGgoAAAANSUh..." |
| 59 | + } |
| 60 | + ] |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +> [!TIP] |
| 65 | +> When creating a request, take into account the token's input limit for the model. If you need to embed larger portions of text, you would need a chunking strategy. |
| 66 | +
|
| 67 | +The response is as follows, where you can see the model's usage statistics: |
| 68 | + |
| 69 | + |
| 70 | +```json |
| 71 | +{ |
| 72 | + "id": "0ab1234c-d5e6-7fgh-i890-j1234k123456", |
| 73 | + "object": "list", |
| 74 | + "data": [ |
| 75 | + { |
| 76 | + "index": 0, |
| 77 | + "object": "embedding", |
| 78 | + "embedding": [ |
| 79 | + 0.017196655, |
| 80 | + // ... |
| 81 | + -0.000687122, |
| 82 | + -0.025054932, |
| 83 | + -0.015777588 |
| 84 | + ] |
| 85 | + } |
| 86 | + ], |
| 87 | + "model": "Cohere-embed-v3-english", |
| 88 | + "usage": { |
| 89 | + "prompt_tokens": 9, |
| 90 | + "completion_tokens": 0, |
| 91 | + "total_tokens": 9 |
| 92 | + } |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +> [!IMPORTANT] |
| 97 | +> Computing embeddings in batches may not be supported for all the models. For example, for `Cohere-embed-v3-english` model, you need to send one image at a time. |
| 98 | +
|
| 99 | +#### Embedding images and text pairs |
| 100 | + |
| 101 | +Some models can generate embeddings from images and text pairs. In this case, you can use the `image` and `text` fields in the request to pass the image and text to the model. The following example shows how to create embeddings for images and text pairs: |
| 102 | + |
| 103 | + |
| 104 | +```json |
| 105 | +{ |
| 106 | + "model": "Cohere-embed-v3-english", |
| 107 | + "input": [ |
| 108 | + { |
| 109 | + "image": "data:image/png;base64,iVBORw0KGgoAAAANSUh...", |
| 110 | + "text": "A photo of a cat" |
| 111 | + } |
| 112 | + ] |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +#### Create different types of embeddings |
| 117 | + |
| 118 | +Some models can generate multiple embeddings for the same input depending on how you plan to use them. This capability allows you to retrieve more accurate embeddings for RAG patterns. |
| 119 | + |
| 120 | +The following example shows how to create embeddings that are used to create an embedding for a document that will be stored in a vector database: |
| 121 | + |
| 122 | + |
| 123 | +```json |
| 124 | +{ |
| 125 | + "model": "Cohere-embed-v3-english", |
| 126 | + "input": [ |
| 127 | + { |
| 128 | + "image": "data:image/png;base64,iVBORw0KGgoAAAANSUh..." |
| 129 | + } |
| 130 | + ], |
| 131 | + "input_type": "document" |
| 132 | +} |
| 133 | +``` |
| 134 | + |
| 135 | +When you work on a query to retrieve such a document, you can use the following code snippet to create the embeddings for the query and maximize the retrieval performance. |
| 136 | + |
| 137 | + |
| 138 | +```json |
| 139 | +{ |
| 140 | + "model": "Cohere-embed-v3-english", |
| 141 | + "input": [ |
| 142 | + { |
| 143 | + "image": "data:image/png;base64,iVBORw0KGgoAAAANSUh..." |
| 144 | + } |
| 145 | + ], |
| 146 | + "input_type": "query" |
| 147 | +} |
| 148 | +``` |
| 149 | + |
| 150 | +Notice that not all the embedding models support indicating the input type in the request and on those cases a 422 error is returned. |
0 commit comments