|
| 1 | +--- |
| 2 | +author: wchigit |
| 3 | +ms.service: service-connector |
| 4 | +ms.topic: include |
| 5 | +ms.date: 10/20/2023 |
| 6 | +ms.author: wchi |
| 7 | +--- |
| 8 | + |
| 9 | +You can use the Azure client library to access various cognitive APIs that Azure Multi-service Cognitive Services support. We use the language service as an example in this sample. Refer to [Authenticate requests to Azure AI services](https://learn.microsoft.com/en-us/azure/ai-services/authentication#authenticate-with-azure-active-directory) to call the cognitive APIs directly. |
| 10 | + |
| 11 | +### [.NET](#tab/dotnet) |
| 12 | + |
| 13 | +1. Install the following dependencies. We use `Azure.AI.TextAnalytics` as an example. |
| 14 | + ```bash |
| 15 | + dotnet add package Azure.AI.TextAnalytics |
| 16 | + dotnet add package Azure.Identity |
| 17 | + ``` |
| 18 | +1. Authenticate using Azure Identity library and get the Azure Multi-service Cognitive Services endpoint from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use. |
| 19 | + |
| 20 | + ```csharp |
| 21 | + using Azure.AI.TextAnalytics; |
| 22 | + using Azure.Identity; |
| 23 | + |
| 24 | + string endpoint = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_ENDPOINT"); |
| 25 | + |
| 26 | + // Uncomment the following lines according to the authentication type. |
| 27 | + // system-assigned managed identity |
| 28 | + // var credential = new DefaultAzureCredential(); |
| 29 | + |
| 30 | + // user-assigned managed identity |
| 31 | + // var credential = new DefaultAzureCredential( |
| 32 | + // new DefaultAzureCredentialOptions |
| 33 | + // { |
| 34 | + // ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_CLIENTID"); |
| 35 | + // }); |
| 36 | + |
| 37 | + // service principal |
| 38 | + // var tenantId = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_TENANTID"); |
| 39 | + // var clientId = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_CLIENTID"); |
| 40 | + // var clientSecret = Environment.GetEnvironmentVariable("AZURE_COGNITIVESERVICES_CLIENTSECRET"); |
| 41 | + // var credential = new ClientSecretCredential(tenantId, clientId, clientSecret); |
| 42 | + |
| 43 | + TextAnalyticsClient languageServiceClient = new( |
| 44 | + new Uri(endpoint), |
| 45 | + credential); |
| 46 | + ``` |
| 47 | + |
| 48 | +### [Java](#tab/java) |
| 49 | +
|
| 50 | +1. Add the following dependencies in your *pom.xml* file. We use `azure-ai-textanalytics` as an example. |
| 51 | + ```xml |
| 52 | + <dependency> |
| 53 | + <groupId>com.azure</groupId> |
| 54 | + <artifactId>azure-ai-textanalytics</artifactId> |
| 55 | + <version>5.1.12</version> |
| 56 | + </dependency> |
| 57 | + <dependency> |
| 58 | + <groupId>com.azure</groupId> |
| 59 | + <artifactId>azure-identity</artifactId> |
| 60 | + <version>1.11.4</version> |
| 61 | + </dependency> |
| 62 | + ``` |
| 63 | +1. Authenticate using `azure-identity` and get the Azure Multi-service Cognitive Services endpoint from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use. |
| 64 | +
|
| 65 | + ```java |
| 66 | + // Uncomment the following lines according to the authentication type. |
| 67 | + // for system-managed identity |
| 68 | + // DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build(); |
| 69 | +
|
| 70 | + // for user-assigned managed identity |
| 71 | + // DefaultAzureCredential credential = new DefaultAzureCredentialBuilder() |
| 72 | + // .managedIdentityClientId(System.getenv("AZURE_COGNITIVESERVICES_CLIENTID")) |
| 73 | + // .build(); |
| 74 | +
|
| 75 | + // for service principal |
| 76 | + // ClientSecretCredential credential = new ClientSecretCredentialBuilder() |
| 77 | + // .clientId(System.getenv("AZURE_COGNITIVESERVICES_CLIENTID")) |
| 78 | + // .clientSecret(System.getenv("AZURE_COGNITIVESERVICES_CLIENTSECRET")) |
| 79 | + // .tenantId(System.getenv("AZURE_COGNITIVESERVICES_TENANTID")) |
| 80 | + // .build(); |
| 81 | + |
| 82 | + String endpoint = System.getenv("AZURE_COGNITIVESERVICES_ENDPOINT"); |
| 83 | + |
| 84 | + TextAnalyticsClient languageClient = new TextAnalyticsClientBuilder() |
| 85 | + .credential(credential) |
| 86 | + .endpoint(endpoint) |
| 87 | + .buildClient(); |
| 88 | + ``` |
| 89 | +
|
| 90 | +### [Python](#tab/python) |
| 91 | +
|
| 92 | +1. Install the following dependencies. We use `azure-ai-textanalytics` as an example. |
| 93 | + ```bash |
| 94 | + pip install azure-ai-textanalytics==5.1.0 |
| 95 | + pip install azure-identity |
| 96 | + ``` |
| 97 | +1. Authenticate using `azure-identity` and get the Azure Multi-service Cognitive Services endpoint from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use. |
| 98 | + ```python |
| 99 | + import os |
| 100 | + from azure.ai.textanalytics import TextAnalyticsClient |
| 101 | + from azure.identity import ManagedIdentityCredential, ClientSecretCredential |
| 102 | + |
| 103 | + # Uncomment the following lines according to the authentication type. |
| 104 | + # system-assigned managed identity |
| 105 | + # cred = ManagedIdentityCredential() |
| 106 | + |
| 107 | + # user-assigned managed identity |
| 108 | + # managed_identity_client_id = os.getenv('AZURE_COGNITIVESERVICES_CLIENTID') |
| 109 | + # cred = ManagedIdentityCredential(client_id=managed_identity_client_id) |
| 110 | + |
| 111 | + # service principal |
| 112 | + # tenant_id = os.getenv('AZURE_COGNITIVESERVICES_TENANTID') |
| 113 | + # client_id = os.getenv('AZURE_COGNITIVESERVICES_CLIENTID') |
| 114 | + # client_secret = os.getenv('AZURE_COGNITIVESERVICES_CLIENTSECRET') |
| 115 | + # cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret) |
| 116 | + endpoint = os.getenv('AZURE_COGNITIVESERVICES_ENDPOINT') |
| 117 | +
|
| 118 | + language_service_client = TextAnalyticsClient( |
| 119 | + endpoint=endpoint, |
| 120 | + credential=cred) |
| 121 | + ``` |
| 122 | +
|
| 123 | +### [NodeJS](#tab/nodejs) |
| 124 | +
|
| 125 | +1. Install the following dependencies. We use `ai-text-analytics` as an example. |
| 126 | + ```bash |
| 127 | + npm install @azure/[email protected] |
| 128 | + npm install @azure/identity |
| 129 | + ``` |
| 130 | +1. Authenticate using `@azure/identity` and get the Azure Multi-service Cognitive Services endpoint from the environment variables added by Service Connector. When using the code below, uncomment the part of the code snippet for the authentication type you want to use. |
| 131 | + |
| 132 | + ```javascript |
| 133 | + import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity"; |
| 134 | + const { TextAnalyticsClient } = require("@azure/ai-text-analytics"); |
| 135 | + |
| 136 | + // Uncomment the following lines according to the authentication type. |
| 137 | + // for system-assigned managed identity |
| 138 | + // const credential = new DefaultAzureCredential(); |
| 139 | + |
| 140 | + // for user-assigned managed identity |
| 141 | + // const clientId = process.env.AZURE_COGNITIVESERVICES_CLIENTID; |
| 142 | + // const credential = new DefaultAzureCredential({ |
| 143 | + // managedIdentityClientId: clientId |
| 144 | + // }); |
| 145 | + |
| 146 | + // for service principal |
| 147 | + // const tenantId = process.env.AZURE_COGNITIVESERVICES_TENANTID; |
| 148 | + // const clientId = process.env.AZURE_COGNITIVESERVICES_CLIENTID; |
| 149 | + // const clientSecret = process.env.AZURE_COGNITIVESERVICES_CLIENTSECRET; |
| 150 | + // const credential = new ClientSecretCredential(tenantId, clientId, clientSecret); |
| 151 | + |
| 152 | + const endpoint = process.env.AZURE_COGNITIVESERVICES_ENDPOINT; |
| 153 | + const languageClient = new TextAnalyticsClient(endpoint, credential); |
| 154 | + ``` |
| 155 | +
|
| 156 | +### [Other](#tab/none) |
| 157 | +For other languages, you can use the connection information that Service Connector sets to the environment variables to connect to Azure Multi-service Cognitive Services. For environment variable details, see [Integrate Azure Multi-service Cognitive Services with Service Connector](../how-to-integrate-cognitive-services.md). |
0 commit comments