Skip to content

Commit 9436275

Browse files
committed
ai services
1 parent 8f92636 commit 9436275

File tree

3 files changed

+255
-0
lines changed

3 files changed

+255
-0
lines changed
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
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 AI 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 AI 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_AISERVICES_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_AISERVICES_CLIENTID");
35+
// });
36+
37+
// service principal
38+
// var tenantId = Environment.GetEnvironmentVariable("AZURE_AISERVICES_TENANTID");
39+
// var clientId = Environment.GetEnvironmentVariable("AZURE_AISERVICES_CLIENTID");
40+
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_AISERVICES_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 AI 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_AISERVICES_CLIENTID"))
73+
// .build();
74+
75+
// for service principal
76+
// ClientSecretCredential credential = new ClientSecretCredentialBuilder()
77+
// .clientId(System.getenv("AZURE_AISERVICES_CLIENTID"))
78+
// .clientSecret(System.getenv("AZURE_AISERVICES_CLIENTSECRET"))
79+
// .tenantId(System.getenv("AZURE_AISERVICES_TENANTID"))
80+
// .build();
81+
82+
String endpoint = System.getenv("AZURE_AISERVICES_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 AI 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_AISERVICES_CLIENTID')
109+
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
110+
111+
# service principal
112+
# tenant_id = os.getenv('AZURE_AISERVICES_TENANTID')
113+
# client_id = os.getenv('AZURE_AISERVICES_CLIENTID')
114+
# client_secret = os.getenv('AZURE_AISERVICES_CLIENTSECRET')
115+
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
116+
endpoint = os.getenv('AZURE_AISERVICES_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 AI 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_AISERVICES_CLIENTID;
142+
// const credential = new DefaultAzureCredential({
143+
// managedIdentityClientId: clientId
144+
// });
145+
146+
// for service principal
147+
// const tenantId = process.env.AZURE_AISERVICES_TENANTID;
148+
// const clientId = process.env.AZURE_AISERVICES_CLIENTID;
149+
// const clientSecret = process.env.AZURE_AISERVICES_CLIENTSECRET;
150+
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
151+
152+
const endpoint = process.env.AZURE_AISERVICES_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 AI Services. For environment variable details, see [Integrate Azure AI Services with Service Connector](../how-to-integrate-aiservices.md).
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
author: wchigit
3+
ms.service: service-connector
4+
ms.topic: include
5+
ms.date: 06/19/2024
6+
ms.author: wchi
7+
---
8+
9+
You can use the Azure client library to access various cognitive APIs that Azure AI 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-a-multi-service-resource-key) 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.Core --version 1.40.0
17+
```
18+
1. Get the Azure AI Services endpoint and key from the environment variables added by Service Connector.
19+
20+
```csharp
21+
using Azure.AI.TextAnalytics;
22+
23+
string endpoint = Environment.GetEnvironmentVariable("AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT")
24+
string key = Environment.GetEnvironmentVariable("AZURE_AISERVICES_KEY");
25+
26+
TextAnalyticsClient languageServiceClient = new(
27+
new Uri(endpoint),
28+
new AzureKeyCredential(key));
29+
```
30+
31+
### [Java](#tab/java)
32+
33+
1. Add the following dependencies in your *pom.xml* file. We use `azure-ai-textanalytics` as an example.
34+
```xml
35+
<dependency>
36+
<groupId>com.azure</groupId>
37+
<artifactId>azure-core</artifactId>
38+
<version>1.49.1</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.azure</groupId>
42+
<artifactId>azure-ai-textanalytics</artifactId>
43+
<version>5.1.12</version>
44+
</dependency>
45+
```
46+
1. Get the Azure AI Services endpoint and key from the environment variables added by Service Connector.
47+
```java
48+
String endpoint = System.getenv("AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT");
49+
String key = System.getenv("AZURE_AISERVICES_KEY");
50+
TextAnalyticsClient languageClient = new TextAnalyticsClientBuilder()
51+
.credential(new AzureKeyCredential(key))
52+
.endpoint(endpoint)
53+
.buildClient();
54+
```
55+
56+
### [Python](#tab/python)
57+
58+
1. Install the following dependencies. We use `azure-ai-textanalytics` as an example.
59+
```bash
60+
pip install azure-ai-textanalytics==5.1.0
61+
pip install azure-core
62+
```
63+
1. Get the Azure AI Services endpoint and key from the environment variables added by Service Connector.
64+
```python
65+
import os
66+
from azure.ai.textanalytics import TextAnalyticsClient
67+
from azure.core.credentials import AzureKeyCredential
68+
69+
key = os.environ['AZURE_AISERVICES_KEY']
70+
endpoint = os.environ['AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT']
71+
language_service_client = TextAnalyticsClient(
72+
endpoint=retrieved_endpoint,
73+
credential=AzureKeyCredential(key))
74+
```
75+
76+
### [NodeJS](#tab/nodejs)
77+
78+
1. Install the following dependency. We use `ai-text-analytics` as an example.
79+
```bash
80+
npm install @azure/[email protected]
81+
```
82+
1. Get the Azure AI Services endpoint and key from the environment variables added by Service Connector.
83+
84+
```javascript
85+
const { TextAnalyticsClient, AzureKeyCredential } = require("@azure/ai-text-analytics");
86+
87+
const endpoint = process.env.AZURE_AISERVICES_COGNITIVESERVICES_ENDPOINT;
88+
const credential = new AzureKeyCredential(process.env.AZURE_AISERVICES_KEY);
89+
90+
const languageClient = new TextAnalyticsClient(endpoint, credential);
91+
```
92+
93+
### [Other](#tab/none)
94+
For other languages, you can use the connection information that Service Connector sets to the environment variables to connect to Azure AI Services. For environment variable details, see [Integrate Azure AI Services with Service Connector](../how-to-integrate-aiservices.md).

articles/service-connector/toc.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ items:
138138
href: how-to-troubleshoot-front-end-error.md
139139
- name: Samples
140140
items:
141+
- name: Azure AI Services
142+
href: how-to-integrate-aiservices.md
141143
- name: Azure App Configuration
142144
href: how-to-integrate-app-configuration.md
143145
- name: Azure Blob Storage
@@ -164,6 +166,8 @@ items:
164166
href: how-to-integrate-storage-file.md
165167
- name: Azure Key Vault
166168
href: how-to-integrate-key-vault.md
169+
- name: Azure Multi-service Cognitive Services
170+
href: how-to-integrate-cognitive-services.md
167171
- name: Azure OpenAI
168172
href: how-to-integrate-openai.md
169173
- name: Azure Queue Storage

0 commit comments

Comments
 (0)