|
| 1 | +--- |
| 2 | +title: Include file |
| 3 | +description: Include file |
| 4 | +author: sdgilley |
| 5 | +ms.reviewer: sgilley |
| 6 | +ms.author: sgilley |
| 7 | +ms.service: azure-ai-foundry |
| 8 | +ms.topic: include |
| 9 | +ms.date: 08/05/2025 |
| 10 | +ms.custom: include |
| 11 | +--- |
| 12 | + |
| 13 | +### Set up your environment |
| 14 | + |
| 15 | +[!INCLUDE [SDK setup](../../includes/development-environment-config.md)] |
| 16 | + |
| 17 | +### Authenticating with Microsoft Entra ID |
| 18 | + |
| 19 | +There are various authentication methods for the different connection types. When you use Microsoft Entra ID, in addition to creating the connection you might also need to grant Azure role-based access control permissions before the connection can be used. For more information, visit [Role-based access control](../../concepts/rbac-azure-ai-foundry.md#scenario-connections-using-microsoft-entra-id-authentication). |
| 20 | + |
| 21 | +[!INCLUDE [Azure Key Vault](~/reusable-content/ce-skilling/azure/includes/ai-services/security/microsoft-entra-id-akv-expanded.md)] |
| 22 | + |
| 23 | +## [Azure OpenAI](#tab/aoai) |
| 24 | + |
| 25 | +The following example uses the [AzureOpenAIConnection](/python/api/azure-ai-ml/azure.ai.ml.entities.azureopenaiconnection) class to create an Azure OpenAI in Azure AI Foundry Models connection. |
| 26 | + |
| 27 | +> [!TIP] |
| 28 | +> To connect to Azure OpenAI and more AI services with one connection, you can use the [AI services connection](#azure-ai-services) instead. |
| 29 | +
|
| 30 | +```python |
| 31 | +from azure.ai.ml.entities import AzureOpenAIConnection |
| 32 | +name = "XXXXXXXXX" |
| 33 | + |
| 34 | +target = "https://XXXXXXXXX.cognitiveservices.azure.com/" |
| 35 | + |
| 36 | +resource_id= "Azure-resource-id" |
| 37 | + |
| 38 | +# Microsoft Entra ID |
| 39 | +credentials = None |
| 40 | + |
| 41 | +wps_connection = AzureOpenAIConnection( |
| 42 | + name=name, |
| 43 | + azure_endpoint=target, |
| 44 | + credentials=credentials, |
| 45 | + resource_id = resource_id, |
| 46 | + is_shared=False |
| 47 | +) |
| 48 | +ml_client.connections.create_or_update(wps_connection) |
| 49 | +``` |
| 50 | + |
| 51 | +## [AI services](#tab/ai-services) |
| 52 | + |
| 53 | +The following example uses the [AzureAIServicesConnection](/python/api/azure-ai-ml/azure.ai.ml.entities.azureaiservicesconnection) class to create an Azure AI services connection. This example creates one connection for the AI services documented in the [Connect to Azure AI services](../../../ai-services/connect-services-ai-foundry-portal.md) article. The same connection also supports Azure OpenAI. |
| 54 | + |
| 55 | +```python |
| 56 | +from azure.ai.ml.entities import AzureAIServicesConnection |
| 57 | + |
| 58 | +name = "my-ai-services" |
| 59 | + |
| 60 | +target = "https://my.cognitiveservices.azure.com/" |
| 61 | +resource_id="" |
| 62 | + |
| 63 | +# Microsoft Entra ID |
| 64 | +credentials = None |
| 65 | + |
| 66 | + |
| 67 | +wps_connection = AzureAIServicesConnection( |
| 68 | + name=name, |
| 69 | + endpoint=target, |
| 70 | + credentials=credentials, |
| 71 | + ai_services_resource_id=resource_id, |
| 72 | +) |
| 73 | +ml_client.connections.create_or_update(wps_connection) |
| 74 | +``` |
| 75 | + |
| 76 | +## [Search](#tab/search) |
| 77 | + |
| 78 | +The following example uses the [AzureAISearchConnection](/python/api/azure-ai-ml/azure.ai.ml.entities.azureaisearchconnection) class to create an Azure AI Search connection: |
| 79 | + |
| 80 | +```python |
| 81 | +from azure.ai.ml.entities import AzureAISearchConnection |
| 82 | + |
| 83 | +name = "my_aisearch_demo_connection" |
| 84 | +target = "https://my.search.windows.net" |
| 85 | + |
| 86 | +# Microsoft Entra ID |
| 87 | +credentials = None |
| 88 | + |
| 89 | + |
| 90 | +wps_connection = AzureAISearchConnection( |
| 91 | + name=name, |
| 92 | + endpoint=target, |
| 93 | + credentials=credentials, |
| 94 | +) |
| 95 | +ml_client.connections.create_or_update(wps_connection) |
| 96 | +``` |
| 97 | + |
| 98 | +## [Content Safety](#tab/content-safety) |
| 99 | + |
| 100 | +The following example creates an Azure AI Content Safety connection: |
| 101 | + |
| 102 | +```python |
| 103 | +from azure.ai.ml.entities import AzureContentSafetyConnection, ApiKeyConfiguration |
| 104 | + |
| 105 | +name = "my_content_safety" |
| 106 | + |
| 107 | +target = "https://my.cognitiveservices.azure.com/" |
| 108 | +api_key = "XXXXXXXXX" |
| 109 | + |
| 110 | +wps_connection = AzureContentSafetyConnection( |
| 111 | + name=name, |
| 112 | + endpoint=target, |
| 113 | + credentials=ApiKeyConfiguration(key=api_key), |
| 114 | + #api_version="1234" |
| 115 | +) |
| 116 | +ml_client.connections.create_or_update(wps_connection) |
| 117 | +``` |
| 118 | + |
| 119 | +## [Serverless model (preview)](#tab/serverless) |
| 120 | + |
| 121 | +The following example creates a serverless endpoint connection: |
| 122 | + |
| 123 | +```python |
| 124 | +from azure.ai.ml.entities import ServerlessConnection |
| 125 | + |
| 126 | +name = "my_maas_apk" |
| 127 | + |
| 128 | +endpoint = "https://my.eastus2.inference.ai.azure.com/" |
| 129 | +api_key = "XXXXXXXXX" |
| 130 | +wps_connection = ServerlessConnection( |
| 131 | + name=name, |
| 132 | + endpoint=endpoint, |
| 133 | + api_key=api_key, |
| 134 | + |
| 135 | +) |
| 136 | +ml_client.connections.create_or_update(wps_connection) |
| 137 | +``` |
| 138 | + |
| 139 | +## [Blob Storage](#tab/blob) |
| 140 | + |
| 141 | +The following example uses the [AzureBlobStoreConnection](/python/api/azure-ai-ml/azure.ai.ml.entities.azureblobstoreconnection) class to create an Azure Blob Storage connection. This connection is authenticated with an account key or a SAS token: |
| 142 | + |
| 143 | +```python |
| 144 | +from azure.ai.ml.entities import AzureBlobStoreConnection, SasTokenConfiguration,AccountKeyConfiguration |
| 145 | + |
| 146 | + |
| 147 | +name = "my_blobstore" |
| 148 | +url = "https://XXXXXXXXX.blob.core.windows.net/mycontainer/" |
| 149 | + |
| 150 | +wps_connection = AzureBlobStoreConnection( |
| 151 | + name=name, |
| 152 | + container_name="XXXXXXXXX", |
| 153 | + account_name="XXXXXXXXX", |
| 154 | + url=url, |
| 155 | + #credentials=None |
| 156 | + credentials=SasTokenConfiguration(sas_token="XXXXXXXXX") |
| 157 | + #credentials=AccountKeyConfiguration(account_key="XXXXXXXXX") |
| 158 | +) |
| 159 | +ml_client.connections.create_or_update(wps_connection) |
| 160 | +``` |
| 161 | + |
| 162 | +## [Azure Data Lake Storage Gen 2](#tab/adl2) |
| 163 | + |
| 164 | +The following example creates Azure Data Lake Storage Gen 2 connection. This connection is authenticated with a Service Principal: |
| 165 | + |
| 166 | +```python |
| 167 | +from azure.ai.ml.entities import WorkspaceConnection |
| 168 | +from azure.ai.ml.entities import ServicePrincipalConfiguration |
| 169 | + |
| 170 | +sp_config = ServicePrincipalConfiguration( |
| 171 | + tenant_id="XXXXXXXXXXXX", |
| 172 | + client_id="XXXXXXXXXXXXX", |
| 173 | + client_secret="XXXXXXXXXXXXXXXk" # your-client-secret |
| 174 | + |
| 175 | +) |
| 176 | +name = "myadlsgen2" |
| 177 | + |
| 178 | +target = "https://ambadaladlsgen2.core.windows.net/dummycont" |
| 179 | + |
| 180 | +wps_connection = WorkspaceConnection( |
| 181 | + name=name, |
| 182 | + type="azure_data_lake_gen2", |
| 183 | + target=target, |
| 184 | + credentials=sp_config |
| 185 | + |
| 186 | +) |
| 187 | +ml_client.connections.create_or_update(workspace_connection=wps_connection) |
| 188 | +``` |
| 189 | + |
| 190 | +## [Microsoft OneLake](#tab/onelake) |
| 191 | + |
| 192 | +The following example uses the [MicrosoftOneLakeWorkspaceConnection](/python/api/azure-ai-ml/azure.ai.ml.entities.microsoftonelakeconnection) class to create a Microsoft OneLake connection. This connection is authenticated with a Service Principal: |
| 193 | + |
| 194 | +```python |
| 195 | +from azure.ai.ml.entities import MicrosoftOneLakeWorkspaceConnection, OneLakeArtifact |
| 196 | +from azure.ai.ml.entities import ServicePrincipalConfiguration |
| 197 | + |
| 198 | +sp_config = ServicePrincipalConfiguration( |
| 199 | + tenant_id="XXXXXXXXXXX", |
| 200 | + client_id="XXXXXXXXXXXXXXXXXX", |
| 201 | + client_secret="XXXXXXXXXXXXXXXX" # your-client-secret |
| 202 | +) |
| 203 | +name = "my_onelake_sp" |
| 204 | + |
| 205 | +artifact = OneLakeArtifact( |
| 206 | + name="XXXXXXXXX", |
| 207 | + type="lake_house" |
| 208 | + |
| 209 | +) |
| 210 | + |
| 211 | +wps_connection = MicrosoftOneLakeWorkspaceConnection( |
| 212 | + name=name, |
| 213 | + artifact=artifact, |
| 214 | + one_lake_workspace_name="XXXXXXXXXXXXXXXXX", |
| 215 | + endpoint="XXXXXXXXX.dfs.fabric.microsoft.com" |
| 216 | + credentials=sp_config |
| 217 | + |
| 218 | +) |
| 219 | +ml_client.connections.create_or_update(workspace_connection=wps_connection) |
| 220 | +``` |
| 221 | + |
| 222 | +## [Serp](#tab/serp) |
| 223 | + |
| 224 | +The following example uses the [SerpConnection](/python/api/azure-ai-ml/azure.ai.ml.entities.serpconnection) class: |
| 225 | + |
| 226 | +```python |
| 227 | +from azure.ai.ml.entities import SerpConnection |
| 228 | + |
| 229 | +name = "my_serp_apk" |
| 230 | +api_key = "XXXXXXXXX" |
| 231 | + |
| 232 | +wps_connection = SerpConnection( |
| 233 | + name=name, |
| 234 | + api_key=api_key, |
| 235 | +) |
| 236 | +ml_client.connections.create_or_update(wps_connection) |
| 237 | +``` |
| 238 | + |
| 239 | +## [OpenAI](#tab/openai) |
| 240 | + |
| 241 | +The following example uses the [OpenAIConnection](/python/api/azure-ai-ml/azure.ai.ml.entities.openaiconnection) class to create an OpenAI (not Azure OpenAI) connection: |
| 242 | + |
| 243 | +```python |
| 244 | +from azure.ai.ml.entities import OpenAIConnection |
| 245 | + |
| 246 | +name = "my_oai_apk" |
| 247 | +api_key = "XXXXXXXX" |
| 248 | + |
| 249 | +wps_connection = OpenAIConnection( |
| 250 | + name=name, |
| 251 | + api_key=api_key, |
| 252 | +) |
| 253 | +ml_client.connections.create_or_update(wps_connection) |
| 254 | +``` |
| 255 | + |
| 256 | +## [Custom](#tab/custom) |
| 257 | + |
| 258 | +The following example uses the [ApiKeyConfiguration](/python/api/azure-ai-ml/azure.ai.ml.entities.apikeyconnection) class to create custom connection: |
| 259 | + |
| 260 | +```python |
| 261 | +from azure.ai.ml.entities import WorkspaceConnection |
| 262 | +from azure.ai.ml.entities import ApiKeyConfiguration |
| 263 | + |
| 264 | + |
| 265 | +name = "my_custom" |
| 266 | + |
| 267 | +target = "https://XXXXXXXXX.core.windows.net/mycontainer" |
| 268 | + |
| 269 | +wps_connection = WorkspaceConnection( |
| 270 | + name=name, |
| 271 | + type="custom", |
| 272 | + target=target, |
| 273 | + credentials=ApiKeyConfiguration(key="XXXXXXXXX"), |
| 274 | +) |
| 275 | +ml_client.connections.create_or_update(workspace_connection=wps_connection) |
| 276 | +``` |
| 277 | +--- |
| 278 | + |
| 279 | +### List connections |
| 280 | + |
| 281 | +To list all connections, use the following example: |
| 282 | + |
| 283 | +```python |
| 284 | +from azure.ai.ml.entities import Connection, AzureOpenAIConnection, ApiKeyConfiguration |
| 285 | +connection_list = ml_client.connections.list() |
| 286 | +for conn in connection_list: |
| 287 | + print(conn) |
| 288 | +``` |
| 289 | + |
| 290 | +### Delete connections |
| 291 | + |
| 292 | +To delete a connection, use the following example: |
| 293 | + |
| 294 | +```python |
| 295 | +name = "my-connection" |
| 296 | + |
| 297 | +ml_client.connections.delete(name) |
| 298 | +``` |
0 commit comments