Skip to content

Commit 7917b03

Browse files
committed
1st commit
1 parent 6005dd5 commit 7917b03

File tree

3 files changed

+240
-0
lines changed

3 files changed

+240
-0
lines changed

articles/service-connector/how-to-integrate-openai.md

Whitespace-only changes.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
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+
### [.NET](#tab/dotnet)
10+
11+
1. Install dependencies.
12+
```bash
13+
dotnet add package Azure.AI.OpenAI --prerelease
14+
dotnet add package Azure.Identity
15+
```
16+
1. Authenticate using Azure Identity library and get the Azure OpenAI 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.
17+
18+
```csharp
19+
using Azure.AI.OpenAI;
20+
using Azure.Identity;
21+
22+
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_BASE");
23+
24+
// Uncomment the following lines according to the authentication type.
25+
// system-assigned managed identity
26+
// var credential = new DefaultAzureCredential();
27+
28+
// user-assigned managed identity
29+
// var credential = new DefaultAzureCredential(
30+
// new DefaultAzureCredentialOptions
31+
// {
32+
// ManagedIdentityClientId = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTID");
33+
// });
34+
35+
// service principal
36+
// var tenantId = Environment.GetEnvironmentVariable("AZURE_OPENAI_TENANTID");
37+
// var clientId = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTID");
38+
// var clientSecret = Environment.GetEnvironmentVariable("AZURE_OPENAI_CLIENTSECRET");
39+
// var credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
40+
41+
AzureOpenAIClient openAIClient = new(
42+
new Uri(endpoint),
43+
credential
44+
);
45+
```
46+
47+
### [Java](#tab/java)
48+
49+
1. Add the following dependencies in your *pom.xml* file:
50+
```xml
51+
<dependency>
52+
<groupId>com.azure</groupId>
53+
<artifactId>azure-ai-openai</artifactId>
54+
<version>1.0.0-beta.6</version>
55+
</dependency>
56+
<dependency>
57+
<groupId>com.azure</groupId>
58+
<artifactId>azure-identity</artifactId>
59+
<version>1.11.4</version>
60+
</dependency>
61+
```
62+
1. Authenticate using `azure-identity` and get the Azure OpenAI 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.
63+
64+
```java
65+
// Uncomment the following lines according to the authentication type.
66+
// for system-managed identity
67+
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
68+
69+
// for user-assigned managed identity
70+
// DefaultAzureCredential defaultCredential = new DefaultAzureCredentialBuilder()
71+
// .managedIdentityClientId(System.getenv("AZURE_OPENAI_CLIENTID"))
72+
// .build();
73+
74+
// for service principal
75+
// ClientSecretCredential defaultCredential = new ClientSecretCredentialBuilder()
76+
// .clientId(System.getenv("AZURE_OPENAI_CLIENTID"))
77+
// .clientSecret(System.getenv("AZURE_OPENAI_CLIENTSECRET"))
78+
// .tenantId(System.getenv("AZURE_OPENAI_TENANTID"))
79+
// .build();
80+
81+
String endpoint = System.getenv("AZURE_OPENAI_ENDPOINT");
82+
83+
OpenAIClient client = new OpenAIClientBuilder()
84+
.credential(credential)
85+
.endpoint(endpoint)
86+
.buildClient();
87+
```
88+
89+
### [Python](#tab/python)
90+
91+
1. Install dependencies.
92+
```bash
93+
pip install azure-OPENAI
94+
pip install azure-identity
95+
```
96+
1. Authenticate using `azure-identity` and get the Azure App Configuration 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.
97+
```python
98+
import os
99+
from azure.OPENAI import AzureOPENAIClient
100+
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
101+
102+
# Uncomment the following lines according to the authentication type.
103+
# system-assigned managed identity
104+
# cred = ManagedIdentityCredential()
105+
106+
# user-assigned managed identity
107+
# managed_identity_client_id = os.getenv('AZURE_OPENAI_CLIENTID')
108+
# cred = ManagedIdentityCredential(client_id=managed_identity_client_id)
109+
110+
# service principal
111+
# tenant_id = os.getenv('AZURE_OPENAI_TENANTID')
112+
# client_id = os.getenv('AZURE_OPENAI_CLIENTID')
113+
# client_secret = os.getenv('AZURE_OPENAI_CLIENTSECRET')
114+
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
115+
116+
endpoint_url = os.getenv('AZURE_OPENAI_ENDPOINT')
117+
118+
client = AzureOPENAIClient(base_url="your_endpoint_url", credential=credential)
119+
```
120+
121+
### [NodeJS](#tab/nodejs)
122+
123+
1. Install dependencies.
124+
```bash
125+
npm install --save @azure/identity
126+
npm install @azure/app-configuration
127+
```
128+
1. Authenticate using `@azure/identity` and get the Azure App Configuration 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.
129+
130+
```javascript
131+
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
132+
const appConfig = require("@azure/app-configuration");
133+
134+
// Uncomment the following lines according to the authentication type.
135+
// for system-assigned managed identity
136+
// const credential = new DefaultAzureCredential();
137+
138+
// for user-assigned managed identity
139+
// const clientId = process.env.AZURE_OPENAI_CLIENTID;
140+
// const credential = new DefaultAzureCredential({
141+
// managedIdentityClientId: clientId
142+
// });
143+
144+
// for service principal
145+
// const tenantId = process.env.AZURE_OPENAI_TENANTID;
146+
// const clientId = process.env.AZURE_OPENAI_CLIENTID;
147+
// const clientSecret = process.env.AZURE_OPENAI_CLIENTSECRET;
148+
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
149+
150+
const endpoint = process.env.AZURE_OPENAI_ENDPOINT;
151+
152+
const client = new appConfig.OPENAIClient(
153+
endpoint,
154+
credential
155+
);
156+
```
157+
158+
### [Other](#tab/none)
159+
For other languages, you can use the connection information that Service Connector sets to the environment variables to connect to Azure OpenAI. For environment variable details, see [Integrate Azure OpenAI with Service Connector](../how-to-integrate-openai.md).
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
author: wchigit
3+
ms.service: service-connector
4+
ms.topic: include
5+
ms.date: 06/12/2024
6+
ms.author: wchi
7+
---
8+
9+
### [.NET](#tab/dotnet)
10+
11+
1. Install the following dependency.
12+
```bash
13+
dotnet add package Azure.AI.OpenAI --prerelease
14+
```
15+
1. Get the Azure OpenAI endpoint and API key from the environment variables added by Service Connector.
16+
17+
```csharp
18+
using Azure.AI.OpenAI;
19+
20+
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_BASE")
21+
string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_KEY");
22+
23+
AzureOpenAIClient openAIClient = new(
24+
new Uri(endpoint),
25+
new AzureKeyCredential(key));
26+
```
27+
28+
### [Java](#tab/java)
29+
30+
1. Add the following dependency in your *pom.xml* file:
31+
```xml
32+
<dependency>
33+
<groupId>com.azure</groupId>
34+
<artifactId>azure-ai-openai</artifactId>
35+
<version>1.0.0-beta.6</version>
36+
</dependency>
37+
```
38+
1. Get the Azure OpenAI endpoint and API key from the environment variables added by Service Connector.
39+
```java
40+
String endpoint = System.getenv("AZURE_OPENAI_BASE");
41+
String key = System.getenv("AZURE_OPENAI_KEY");
42+
OpenAIClient client = new OpenAIClientBuilder()
43+
.credential(new AzureKeyCredential(key))
44+
.endpoint(endpoint)
45+
.buildClient();
46+
```
47+
48+
### [Python](#tab/python)
49+
50+
1. Install the following dependency.
51+
```bash
52+
pip install openai
53+
```
54+
1. Get the App Configuration connection string from the environment variables added by Service Connector.
55+
```python
56+
import os
57+
import OpenAI
58+
from azure.core.credentials import AzureKeyCredential
59+
60+
openai.api_key = os.environ['AZURE_OPENAI_KEY']
61+
openai.base_url = os.environ['AZURE_OPENAI_BASE']
62+
client = OpenAI()
63+
```
64+
65+
### [NodeJS](#tab/nodejs)
66+
67+
1. Install the following dependency.
68+
```bash
69+
npm install @azure/app-configuration
70+
```
71+
1. Get the App Configuration connection string from the environment variables added by Service Connector.
72+
73+
```javascript
74+
const appConfig = require("@azure/app-configuration");
75+
76+
const connection_string = process.env.AZURE_APPCONFIGURATION_CONNECTIONSTRING;
77+
const client = new appConfig.AppConfigurationClient(connection_string);
78+
```
79+
80+
### [Other](#tab/none)
81+
For other languages, you can use the connection information that Service Connector sets to the environment variables to connect to Azure OpenAI. For environment variable details, see [Integrate Azure OpenAI with Service Connector](../how-to-integrate-openai.md).

0 commit comments

Comments
 (0)