|
| 1 | +--- |
| 2 | +title: Azure OpenAI on your Azure Machine Learning index data Python & REST API reference |
| 3 | +titleSuffix: Azure OpenAI |
| 4 | +description: Learn how to use Azure OpenAI on your Azure Machine Learning index data Python & REST API. |
| 5 | +manager: nitinme |
| 6 | +ms.service: azure-ai-openai |
| 7 | +ms.topic: conceptual |
| 8 | +ms.date: 02/14/2024 |
| 9 | +author: mrbullwinkle |
| 10 | +ms.author: mbullwin |
| 11 | +recommendations: false |
| 12 | +ms.custom: |
| 13 | +--- |
| 14 | + |
| 15 | +# Data source - Azure Machine Learning index |
| 16 | + |
| 17 | +The configurable options of Azure Machine Learning index when using Azure OpenAI On Your Data. This data source is supported in API version `2024-02-15-preview`. |
| 18 | + |
| 19 | +|Name | Type | Required | Description | |
| 20 | +|--- | --- | --- | --- | |
| 21 | +|`parameters`| [Parameters](#parameters)| True| The parameters to use when configuring Azure Machine Learning index.| |
| 22 | +| `type`| string| True | Must be `azure_ml_index`. | |
| 23 | + |
| 24 | +## Parameters |
| 25 | + |
| 26 | +|Name | Type | Required | Description | |
| 27 | +|--- | --- | --- | --- | |
| 28 | +| `project_resource_id` | string | True | The resource ID of the Azure Machine Learning project.| |
| 29 | +| `name` | string | True | The Azure Machine Learning index name.| |
| 30 | +| `version` | string | True | The version of the Azure Machine Learning index.| |
| 31 | +| `authentication`| One of [AccessTokenAuthenticationOptions](#access-token-authentication-options), [SystemAssignedManagedIdentityAuthenticationOptions](#system-assigned-managed-identity-authentication-options), [UserAssignedManagedIdentityAuthenticationOptions](#user-assigned-managed-identity-authentication-options) | True | The authentication method to use when accessing the defined data source. | |
| 32 | +| `in_scope` | boolean | False | Whether queries should be restricted to use of indexed data. Default is `True`.| |
| 33 | +| `role_information`| string | False | Give the model instructions about how it should behave and any context it should reference when generating a response. You can describe the assistant's personality and tell it how to format responses.| |
| 34 | +| `strictness` | integer | False | The configured strictness of the search relevance filtering. The higher of strictness, the higher of the precision but lower recall of the answer. Default is `3`.| |
| 35 | +| `top_n_documents` | integer | False | The configured top number of documents to feature for the configured query. Default is `5`. | |
| 36 | +| `filter`| string | False | Search filter. Only supported if the Azure Machine Learning index is of type Azure Search.| |
| 37 | + |
| 38 | + |
| 39 | +## Access token authentication options |
| 40 | + |
| 41 | +The authentication options for Azure OpenAI On Your Data when using access token. |
| 42 | + |
| 43 | +|Name | Type | Required | Description | |
| 44 | +|--- | --- | --- | --- | |
| 45 | +| `access_token`|string|True|The access token to use for authentication.| |
| 46 | +| `type`|string|True| Must be `access_token`.| |
| 47 | + |
| 48 | +## System assigned managed identity authentication options |
| 49 | + |
| 50 | +The authentication options for Azure OpenAI On Your Data when using a system-assigned managed identity. |
| 51 | + |
| 52 | +|Name | Type | Required | Description | |
| 53 | +|--- | --- | --- | --- | |
| 54 | +| `type`|string|True| Must be `system_assigned_managed_identity`.| |
| 55 | + |
| 56 | +## User assigned managed identity authentication options |
| 57 | + |
| 58 | +The authentication options for Azure OpenAI On Your Data when using a user-assigned managed identity. |
| 59 | + |
| 60 | +|Name | Type | Required | Description | |
| 61 | +|--- | --- | --- | --- | |
| 62 | +| `managed_identity_resource_id`|string|True|The resource ID of the user-assigned managed identity to use for authentication.| |
| 63 | +| `type`|string|True| Must be `user_assigned_managed_identity`.| |
| 64 | + |
| 65 | +## Examples |
| 66 | + |
| 67 | +Prerequisites: |
| 68 | +* Configure the role assignments from Azure OpenAI system assigned managed identity to Azure Machine Learning workspace resource. Required role: `AzureML Data Scientist`. |
| 69 | +* Configure the role assignments from the user to the Azure OpenAI resource. Required role: `Cognitive Services OpenAI User`. |
| 70 | +* Install [Az CLI](/cli/azure/install-azure-cli) and run `az login`. |
| 71 | +* Define the following environment variables: `AzureOpenAIEndpoint`, `ChatCompletionsDeploymentName`, `ProjectResourceId`, `IndexName`, `IndexVersion`. |
| 72 | +* Run `export MSYS_NO_PATHCONV=1` if you're using MINGW. |
| 73 | +```bash |
| 74 | +export AzureOpenAIEndpoint=https://example.openai.azure.com/ |
| 75 | +export ChatCompletionsDeploymentName=turbo |
| 76 | +export ProjectResourceId='/subscriptions/{subscription-id}/resourceGroups/{resource-group-name}/providers/Microsoft.MachineLearningServices/workspaces/{workspace-id}' |
| 77 | +export IndexName=testamlindex |
| 78 | +export IndexVersion=2 |
| 79 | +``` |
| 80 | + |
| 81 | +# [Python 1.x](#tab/python) |
| 82 | + |
| 83 | +Install the latest pip packages `openai`, `azure-identity`. |
| 84 | + |
| 85 | +```python |
| 86 | +import os |
| 87 | +from openai import AzureOpenAI |
| 88 | +from azure.identity import DefaultAzureCredential, get_bearer_token_provider |
| 89 | + |
| 90 | +endpoint = os.environ.get("AzureOpenAIEndpoint") |
| 91 | +deployment = os.environ.get("ChatCompletionsDeploymentName") |
| 92 | +project_resource_id = os.environ.get("ProjectResourceId") |
| 93 | +index_name = os.environ.get("IndexName") |
| 94 | +index_version = os.environ.get("IndexVersion") |
| 95 | + |
| 96 | +token_provider = get_bearer_token_provider( |
| 97 | + DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default") |
| 98 | + |
| 99 | +client = AzureOpenAI( |
| 100 | + azure_endpoint=endpoint, |
| 101 | + azure_ad_token_provider=token_provider, |
| 102 | + api_version="2024-02-15-preview", |
| 103 | +) |
| 104 | + |
| 105 | +completion = client.chat.completions.create( |
| 106 | + model=deployment, |
| 107 | + messages=[ |
| 108 | + { |
| 109 | + "role": "user", |
| 110 | + "content": "Who is DRI?", |
| 111 | + }, |
| 112 | + ], |
| 113 | + extra_body={ |
| 114 | + "data_sources": [ |
| 115 | + { |
| 116 | + "type": "azure_ml_index", |
| 117 | + "parameters": { |
| 118 | + "project_resource_id": project_resource_id, |
| 119 | + "name": index_name, |
| 120 | + "version": index_version, |
| 121 | + "authentication": { |
| 122 | + "type": "system_assigned_managed_identity" |
| 123 | + }, |
| 124 | + } |
| 125 | + } |
| 126 | + ] |
| 127 | + } |
| 128 | +) |
| 129 | + |
| 130 | +print(completion.model_dump_json(indent=2)) |
| 131 | + |
| 132 | +``` |
| 133 | + |
| 134 | +# [REST](#tab/rest) |
| 135 | + |
| 136 | +```bash |
| 137 | + |
| 138 | +az rest --method POST \ |
| 139 | + --uri $AzureOpenAIEndpoint/openai/deployments/$ChatCompletionsDeploymentName/chat/completions?api-version=2024-02-15-preview \ |
| 140 | + --resource https://cognitiveservices.azure.com/ \ |
| 141 | + --body \ |
| 142 | +' |
| 143 | +{ |
| 144 | + "data_sources": [ |
| 145 | + { |
| 146 | + "type": "azure_ml_index", |
| 147 | + "parameters": { |
| 148 | + "project_resource_id": "'$ProjectResourceId'", |
| 149 | + "name": "'$IndexName'", |
| 150 | + "version": "'$IndexVersion'", |
| 151 | + "authentication": { |
| 152 | + "type": "system_assigned_managed_identity" |
| 153 | + }, |
| 154 | + } |
| 155 | + } |
| 156 | + ], |
| 157 | + "messages": [ |
| 158 | + { |
| 159 | + "role": "user", |
| 160 | + "content": "Who is DRI?" |
| 161 | + } |
| 162 | + ] |
| 163 | +} |
| 164 | +' |
| 165 | +``` |
| 166 | + |
| 167 | +--- |
0 commit comments