Skip to content

Commit 13eb4b3

Browse files
committed
Edit content
1 parent 983d112 commit 13eb4b3

File tree

3 files changed

+73
-88
lines changed

3 files changed

+73
-88
lines changed

articles/machine-learning/how-to-use-batch-model-openai-embeddings.md

Lines changed: 72 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
---
22
title: Run OpenAI models in batch endpoints
33
titleSuffix: Azure Machine Learning
4-
description: In this article, learn how to use batch endpoints with OpenAI models.
4+
description: Find out how to compute embeddings by running OpenAI models in batch endpoints. See how to deploy an OpenAI ADA-002 model in MLflow format to a batch endpoint.
55
services: machine-learning
66
ms.service: azure-machine-learning
77
ms.subservice: inferencing
88
ms.topic: how-to
99
author: msakande
1010
ms.author: mopeakande
1111
ms.reviewer: cacrest
12-
ms.date: 11/13/2024
12+
ms.date: 11/19/2024
1313
ms.custom: how-to, devplatv2, update-code
14+
# customer intent: As a developer, I want to deploy an OpenAI ADA-002 model to a batch endpoint so I can compute embeddings at scale.
1415
---
1516

1617
# Run OpenAI models in batch endpoints to compute embeddings
@@ -19,7 +20,7 @@ ms.custom: how-to, devplatv2, update-code
1920

2021
To run inference over large amounts of data, you can use batch endpoints to deploy models, including OpenAI models. In this article, you see how to create a batch endpoint to deploy an ADA-002 model from OpenAI to compute embeddings at scale. You can use the same approach for completions and chat completions models.
2122

22-
The examples in this article use Microsoft Entra authentication to grant access to the Azure OpenAI resource. The model is registered in MLflow format. It uses the OpenAI flavor, which provides support for calling the OpenAI service at scale.
23+
The example in this article uses Microsoft Entra authentication to grant access to an Azure OpenAI Service resource. The model is registered in MLflow format. It uses the OpenAI flavor, which provides support for calling the OpenAI service at scale.
2324

2425
To follow along with the example steps, see the Jupyter notebook [Score OpenAI models in batch using Batch Endpoints](https://github.com/Azure/azureml-examples/blob/main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb).
2526

@@ -47,13 +48,13 @@ cd endpoints/batch/deploy-models/openai-embeddings
4748

4849
---
4950

50-
## Create an OpenAI resource
51+
## Create an Azure OpenAI resource
5152

52-
This article shows you how to run OpenAI models hosted in Azure OpenAI Service. To follow the steps, you need an OpenAI resource that's deployed in Azure. For information about creating an Azure OpenAI Service resource, see [Create a resource](../ai-services/openai/how-to/create-resource.md#create-a-resource).
53+
This article shows you how to run OpenAI models hosted in Azure OpenAI. To follow the steps, you need an OpenAI resource that's deployed in Azure. For information about creating an Azure OpenAI resource, see [Create a resource](../ai-services/openai/how-to/create-resource.md#create-a-resource).
5354

54-
:::image type="content" source="./media/how-to-use-batch-model-openai-embeddings/aoai-deployments.png" alt-text="An screenshot showing the Azure OpenAI studio with the list of model deployments available.":::
55+
:::image type="content" source="./media/how-to-use-batch-model-openai-embeddings/azure-openai-deployments.png" alt-text="Screenshot of Azure OpenAI Studio that shows a list of available model deployments.":::
5556

56-
The name of your OpenAI resource forms part of the resource URL. Use the following command to save that URL for use in later steps.
57+
The name of your Azure OpenAI resource forms part of the resource URL. Use the following command to save that URL for use in later steps.
5758

5859
# [Azure CLI](#tab/cli)
5960

@@ -97,7 +98,7 @@ Using Microsoft Entra is recommended because it helps you avoid managing secrets
9798

9899
# [Microsoft Entra authentication](#tab/ad)
99100

100-
You can configure the identity of the compute instance to have access to the Azure OpenAI deployment to get predictions. In this way, you don't need to manage permissions for each endpoint user. To give the identity of the compute cluster access to the Azure OpenAI resource, follow these steps:
101+
You can configure the identity of the compute cluster to have access to the Azure OpenAI deployment to get predictions. In this way, you don't need to manage permissions for each endpoint user. To give the identity of the compute cluster access to the Azure OpenAI resource, follow these steps:
101102

102103
1. Assign an identity to the compute cluster that your deployment uses. This example uses a compute cluster called **batch-cluster-lp** and a system-assigned managed identity, but you can use other alternatives. If your compute cluster already has an assigned identity, you can skip this step.
103104

@@ -106,7 +107,7 @@ You can configure the identity of the compute instance to have access to the Azu
106107
az ml compute update --name $COMPUTE_NAME --identity-type system_assigned
107108
```
108109
109-
1. Get the managed identity principal ID assigned to the compute cluster you plan to use.
110+
1. Get the managed identity principal ID that's assigned to the compute cluster you plan to use.
110111
111112
```azurecli
112113
PRINCIPAL_ID=$(az ml compute show -n $COMPUTE_NAME --query identity.principal_id)
@@ -119,39 +120,39 @@ You can configure the identity of the compute instance to have access to the Azu
119120
RESOURCE_ID=$(az group show -g $RG --query "id" -o tsv)
120121
```
121122
122-
1. Grant the role **Cognitive Services User** to the managed identity:
123+
1. Assign the **Cognitive Services User** role to the managed identity:
123124
124125
```azurecli
125126
az role assignment create --role "Cognitive Services User" --assignee $PRINCIPAL_ID --scope $RESOURCE_ID
126127
```
127128
128-
If you get an error message about not finding a user or service principal in the graph database for your principal, check your role assignments. You might need to assign yourself a Global Administrator or Application Administrator role.
129-
130129
# [Access keys](#tab/keys)
131130
132-
You can configure the batch deployment to use the access key of your OpenAI resource to get predictions. Copy the access key from your account, and keep it for later steps.
131+
You can configure the batch deployment to use the access key of your Azure OpenAI resource to get predictions. Copy the access key from your account, and keep it for later steps.
133132
134133
---
135134
136135
## Register the OpenAI model
137136
138137
Model deployments in batch endpoints can deploy only registered models. You can use MLflow models with the flavor OpenAI to create a model in your workspace that references a deployment in Azure OpenAI.
139138
140-
In the cloned repository, the **model** folder contains an MLflow model that generates embeddings based on the ADA-002 model.
139+
In the cloned repository, the *model* folder contains an MLflow model that generates embeddings based on the ADA-002 model.
141140
142-
1. Register the model in the workspace:
141+
Register the model in the workspace:
143142
144-
# [Azure CLI](#tab/cli)
143+
# [Azure CLI](#tab/cli)
145144
146-
:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/openai-embeddings/deploy-and-run.sh" ID="register_model" :::
145+
:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/openai-embeddings/deploy-and-run.sh" ID="register_model" :::
147146
148-
# [Python SDK](#tab/python)
147+
# [Python SDK](#tab/python)
149148
150-
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=register_model)]
149+
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=register_model)]
150+
151+
---
151152
152153
## Create a deployment for an OpenAI model
153154
154-
To deploy the OpenAI model, you need to create an endpoint, an environment, a scoring script, and a batch deployment. The following sections show you how to create these components.
155+
To deploy the OpenAI model, you need to create an endpoint, an environment, a scoring script, and a batch deployment. The following sections show you how to set up these components.
155156
156157
### Create an endpoint
157158
@@ -167,6 +168,8 @@ An endpoint is needed to host the model. Take the following steps to create an e
167168
168169
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=name_endpoint)]
169170
171+
---
172+
170173
1. Configure the endpoint:
171174
172175
# [Azure CLI](#tab/cli)
@@ -179,6 +182,8 @@ An endpoint is needed to host the model. Take the following steps to create an e
179182
180183
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=configure_endpoint)]
181184
185+
---
186+
182187
1. Create the endpoint resource:
183188
184189
# [Azure CLI](#tab/cli)
@@ -189,13 +194,15 @@ An endpoint is needed to host the model. Take the following steps to create an e
189194
190195
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=create_endpoint)]
191196
197+
---
198+
192199
### Configure an environment
193200
194201
The scoring script in this example uses some libraries that aren't part of the standard OpenAI SDK. Create an environment that contains a base image and also a conda YAML file to capture those dependencies:
195202
196203
# [Azure CLI](#tab/cli)
197204
198-
The *environment* folder contains a file named *environment.yml* that configures the environment.
205+
The environment definition consists of the following lines, which are included in the deployment definition.
199206
200207
:::code language="yaml" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/openai-embeddings/environment/environment.yml":::
201208
@@ -241,8 +248,8 @@ If you use the environment variable `OPENAI_API_TYPE` with a value of `azure_ad`
241248
242249
To use an access key instead of Microsoft Entra authentication, you use the following environment variables and values:
243250
244-
* `OPENAI_API_TYPE: "azure"`
245-
* `OPENAI_API_KEY: "<your-Azure-OpenAI-key>"`
251+
- `OPENAI_API_TYPE: "azure"`
252+
- `OPENAI_API_KEY: "<your-Azure-OpenAI-resource-key>"`
246253
247254
---
248255
@@ -264,6 +271,8 @@ To use an access key instead of Microsoft Entra authentication, you use the foll
264271
> [!TIP]
265272
> The `environment_variables` section provides the configuration for the OpenAI deployment.
266273
274+
---
275+
267276
1. Create the deployment.
268277
269278
# [Azure CLI](#tab/cli)
@@ -278,84 +287,72 @@ To use an access key instead of Microsoft Entra authentication, you use the foll
278287
279288
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=set_default_deployment)]
280289
290+
---
291+
281292
The batch endpoint is ready for use.
282293
283294
## Test the deployment
284295
285-
For testing the endpoint, you use a sample of the dataset [BillSum: A Corpus for Automatic Summarization of US Legislation](https://arxiv.org/abs/1910.00523). This sample is included in the repository, in the *data* folder.
296+
For testing the endpoint, you use a sample of the dataset [BillSum: A Corpus for Automatic Summarization of US Legislation](https://arxiv.org/abs/1910.00523). This sample is included in the *data* folder of cloned repository.
286297
287298
1. Set up the input data:
288299
289-
# [Azure CLI](#tab/cli)
290-
291-
1. Create a YAML file, bill-summarization.yml:
292-
293-
```yml
294-
$schema: https://azuremlschemas.azureedge.net/latest/data.schema.json
295-
name: bill_summarization
296-
description: A sample of a dataset for summarization of US Congressional and California state bills.
297-
type: uri_file
298-
path: data/billsum-0.csv
299-
```
300-
301-
1. Create a data asset.
302-
303-
```azurecli
304-
az ml data create -f bill-summarization.yml
305-
```
306-
307-
1. Get the ID of the data asset.
300+
# [Azure CLI](#tab/cli)
308301
309-
```azurecli
310-
DATA_ASSET_ID=$(az ml data show -n bill_summarization --label latest | jq -r .id)
311-
```
302+
In the commands in this section, use *data* as the name of the folder that contains the input data.
312303
313-
# [Python SDK](#tab/python)
304+
# [Python SDK](#tab/python)
314305
315-
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=configure_inputs)]
306+
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=configure_inputs)]
307+
308+
---
316309
317310
1. Invoke the endpoint:
318311
319-
# [Azure CLI](#tab/cli)
312+
# [Azure CLI](#tab/cli)
320313
321-
```azurecli
322-
JOB_NAME=$(az ml batch-endpoint invoke --name $ENDPOINT_NAME --set inputs.bill_summarization.type="uri_file" inputs.bill_summarization.path=$DATASET_ID --query name -o tsv)
323-
```
314+
:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/openai-embeddings/deploy-and-run.sh" ID="start_batch_scoring_job" :::
324315
325-
# [Python SDK](#tab/python)
316+
# [Python SDK](#tab/python)
326317
327-
> [!TIP]
328-
> [!INCLUDE [batch-endpoint-invoke-inputs-sdk](includes/batch-endpoint-invoke-inputs-sdk.md)]
318+
> [!TIP]
319+
> [!INCLUDE [batch-endpoint-invoke-inputs-sdk](includes/batch-endpoint-invoke-inputs-sdk.md)]
320+
321+
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=start_batch_scoring_job)]
329322
330-
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=start_batch_scoring_job)]
323+
---
331324
332325
1. Track the progress:
333326
334-
# [Azure CLI](#tab/cli)
327+
# [Azure CLI](#tab/cli)
335328
336-
:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/openai-embeddings/deploy-and-run.sh" ID="show_job_in_studio" :::
329+
:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/openai-embeddings/deploy-and-run.sh" ID="show_job_in_studio" :::
337330
338-
# [Python SDK](#tab/python)
331+
# [Python SDK](#tab/python)
339332
340-
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=get_job)]
333+
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=get_job)]
334+
335+
---
341336
342337
1. After the deployment is finished, download the predictions:
343338
344-
# [Azure CLI](#tab/cli)
339+
# [Azure CLI](#tab/cli)
345340
346341
:::code language="azurecli" source="~/azureml-examples-main/cli/endpoints/batch/deploy-models/openai-embeddings/deploy-and-run.sh" ID="download_outputs" :::
347342
348-
# [Python SDK](#tab/python)
343+
# [Python SDK](#tab/python)
344+
345+
The deployment creates a child job that implements the scoring. Get a reference to that child job:
349346
350-
The deployment creates a child job that implements the scoring. Get a reference to that child job:
347+
```python
348+
scoring_job = list(ml_client.jobs.list(parent_job_name=job.name))[0]
349+
```
351350
352-
```python
353-
scoring_job = list(ml_client.jobs.list(parent_job_name=job.name))[0]
354-
```
351+
Download the scores:
355352
356-
Download the scores:
353+
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=download_outputs)]
357354
358-
[!notebook-python[] (~/azureml-examples-main/sdk/python/endpoints/batch/deploy-models/openai-embeddings/deploy-and-test.ipynb?name=download_outputs)]
355+
---
359356
360357
1. Use the following code to view the output predictions:
361358
@@ -364,7 +361,7 @@ For testing the endpoint, you use a sample of the dataset [BillSum: A Corpus for
364361
from io import StringIO
365362
366363
# Read the output data into an object.
367-
with open('sample-output.jsonl', 'r') as f:
364+
with open('embeddings.jsonl', 'r') as f:
368365
json_lines = f.readlines()
369366
string_io = StringIO()
370367
for line in json_lines:
@@ -378,25 +375,13 @@ For testing the endpoint, you use a sample of the dataset [BillSum: A Corpus for
378375
print(embeddings)
379376
```
380377
381-
__embeddings.jsonl__
378+
You can also open the output file, *embeddings.jsonl*, to see the predictions:
382379
383-
```json
384-
{
385-
"file": "billsum-0.csv",
386-
"row": 0,
387-
"embeddings": [
388-
[0, 0, 0, 0, 0, 0, 0 ]
389-
]
390-
},
391-
{
392-
"file": "billsum-0.csv",
393-
"row": 1,
394-
"embeddings": [
395-
[0, 0, 0, 0, 0, 0, 0 ]
396-
]
397-
},
380+
```jsonl
381+
{"file": "billsum-0.csv", "row": 0, "embeddings": [[0, 0, 0, 0, 0, 0, 0]]}
382+
{"file": "billsum-0.csv", "row": 1, "embeddings": [[0, 0, 0, 0, 0, 0, 0]]}
398383
```
399-
384+
400385
## Next steps
401386
402-
* [Create jobs and input data for batch endpoints](how-to-access-data-batch-endpoints-jobs.md)
387+
- [Create jobs and input data for batch endpoints](how-to-access-data-batch-endpoints-jobs.md)

articles/machine-learning/includes/azureml-batch-prereqs.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ms.author: cacrest
1111
- The following permissions in the Azure Machine Learning workspace:
1212
- For creating or managing batch endpoints and deployments: Use an Owner, Contributor, or Custom role that has been assigned the `Microsoft.MachineLearningServices/workspaces/batchEndpoints/*` actions.
1313
- For creating Azure Resource Manager deployments in the workspace resource group: Use an Owner, Contributor, or Custom role that has been assigned the `Microsoft.Resources/deployments/write` action in the resource group where the workspace is deployed.
14-
- The following software to work with Azure Machine Learning:
14+
- The Azure Machine Learning CLI or the Azure Machine Learning SDK for Python:
1515

1616
# [Azure CLI](#tab/cli)
1717

0 commit comments

Comments
 (0)