Skip to content

Commit 34cb3be

Browse files
committed
openai sample
1 parent 7917b03 commit 34cb3be

File tree

4 files changed

+142
-27
lines changed

4 files changed

+142
-27
lines changed
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
---
2+
title: Integrate Azure OpenAI with Service Connector
3+
description: In this document, learn how to integrate Azure OpenAI into your application with Service Connector
4+
author: wchigit
5+
ms.author: wchi
6+
ms.service: service-connector
7+
ms.topic: how-to
8+
ms.date: 06/14/2024
9+
---
10+
11+
# Integrate Azure OpenAI with Service Connector
12+
13+
This page shows supported authentication methods and clients, and shows sample code you can use to connect Azure OpenAI to other cloud services using Service Connector. You might still be able to connect to Azure OpenAI using other methods. This page also shows default environment variable names and values you get when you create the service connection.
14+
15+
## Supported compute services
16+
17+
Service Connector can be used to connect the following compute services to Azure OpenAI:
18+
19+
- Azure App Service
20+
- Azure Functions
21+
- Azure Container Apps
22+
- Azure Spring Apps
23+
24+
## Supported authentication types and client types
25+
26+
The table below shows which combinations of authentication methods and clients are supported for connecting your compute service to Azure OpenAI using Service Connector. A “Yes” indicates that the combination is supported, while a “No” indicates that it is not supported.
27+
28+
29+
| Client type | System-assigned managed identity | User-assigned managed identity | Secret/connection string | Service principal |
30+
|-------------|:--------------------------------:|:------------------------------:|:------------------------:|:-----------------:|
31+
| .NET | Yes | Yes | Yes | Yes |
32+
| Java | Yes | Yes | Yes | Yes |
33+
| Node.js | Yes | Yes | Yes | Yes |
34+
| Python | Yes | Yes | Yes | Yes |
35+
| None | Yes | Yes | Yes | Yes |
36+
37+
This table indicates that all combinations of client types and authentication methods in the table are supported. All client types can use any of the authentication methods to connect to Azure OpenAI using Service Connector.
38+
39+
## Default environment variable names or application properties and sample code
40+
41+
Use the connection details below to connect compute services to Azure OpenAI. For more information about naming conventions, check the [Service Connector internals](concept-service-connector-internals.md#configuration-naming-convention) article.
42+
43+
### System-assigned managed identity
44+
45+
| Default environment variable name | Description | Sample value |
46+
| --------------------------------- | ---------------------------- | ------------------------------------------------ |
47+
| AZURE_OPENAI_BASE | Azure OpenAI endpoint | `https://<Azure-OpenAI-name>.openai.azure.com/` |
48+
49+
#### Sample code
50+
Refer to the steps and code below to connect to Azure OpenAI using a system-assigned managed identity.
51+
[!INCLUDE [code sample for app config](./includes/code-openai-me-id.md)]
52+
53+
### User-assigned managed identity
54+
55+
| Default environment variable name | Description | Sample value |
56+
| --------------------------------- | -------------------------- | ----------------------------------------------- |
57+
| AZURE_OPENAI_BASE | Azure OpenAI Endpoint | `https://<Azure-OpenAI-name>.openai.azure.com/` |
58+
| AZURE_OPENAI_CLIENTID | Your client ID | `<client-ID>` |
59+
60+
#### Sample code
61+
Refer to the steps and code below to connect to Azure OpenAI using a user-assigned managed identity.
62+
[!INCLUDE [code sample for azure openai](./includes/code-openai-me-id.md)]
63+
64+
### Connection string
65+
66+
> [!div class="mx-tdBreakAll"]
67+
> | Default environment variable name | Description | Sample value |
68+
> | --------------------------------- | ------------| ------------ |
69+
> | AZURE_OPENAI_BASE | Azure OpenAI Endpoint | `https://<Azure-OpenAI-name>.openai.azure.com/` |
70+
> | AZURE_OPENAI_KEY | Azure OpenAI API key | `<api-key>` |
71+
72+
#### Sample Code
73+
Refer to the steps and code below to connect to Azure OpenAI using a connection string.
74+
[!INCLUDE [code sample for azure openai](./includes/code-openai-secret.md)]
75+
76+
77+
### Service principal
78+
79+
| Default environment variable name | Description | Sample value |
80+
| ----------------------------------- | -------------------------- | ---------------------------------------------- |
81+
| AZURE_OPENAI_BASE | Azure OpenAI Endpoint | `https://<Azure-OpenAI-name>.openai.azure.com/` |
82+
| AZURE_OPENAI_CLIENTID | Your client ID | `<client-ID>` |
83+
| AZURE_OPENAI_CLIENTSECRET | Your client secret | `<client-secret>` |
84+
| AZURE_OPENAI_TENANTID | Your tenant ID | `<tenant-ID>` |
85+
86+
#### Sample code
87+
Refer to the steps and code below to connect to Azure OpenAI using a service principaL.
88+
[!INCLUDE [code sample for azure openai](./includes/code-openai-me-id.md)]
89+
90+
## Next steps
91+
92+
Follow the tutorial listed below to learn more about Service Connector.
93+
94+
> [!div class="nextstepaction"]
95+
> [Learn about Service Connector concepts](./concept-service-connector-internals.md)

articles/service-connector/includes/code-openai-me-id.md

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ ms.author: wchi
7878
// .tenantId(System.getenv("AZURE_OPENAI_TENANTID"))
7979
// .build();
8080
81-
String endpoint = System.getenv("AZURE_OPENAI_ENDPOINT");
81+
String endpoint = System.getenv("AZURE_OPENAI_BASE");
8282
8383
OpenAIClient client = new OpenAIClientBuilder()
8484
.credential(credential)
@@ -90,14 +90,14 @@ ms.author: wchi
9090
9191
1. Install dependencies.
9292
```bash
93-
pip install azure-OPENAI
93+
pip install openai
9494
pip install azure-identity
9595
```
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.
96+
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.
9797
```python
9898
import os
99-
from azure.OPENAI import AzureOPENAIClient
100-
from azure.identity import ManagedIdentityCredential, ClientSecretCredential
99+
import OpenAI
100+
from azure.identity import ManagedIdentityCredential, ClientSecretCredential, get_bearer_token_provider
101101
102102
# Uncomment the following lines according to the authentication type.
103103
# system-assigned managed identity
@@ -113,23 +113,30 @@ ms.author: wchi
113113
# client_secret = os.getenv('AZURE_OPENAI_CLIENTSECRET')
114114
# cred = ClientSecretCredential(tenant_id=tenant_id, client_id=client_id, client_secret=client_secret)
115115
116-
endpoint_url = os.getenv('AZURE_OPENAI_ENDPOINT')
116+
token_provider = get_bearer_token_provider(
117+
cred, "https://cognitiveservices.azure.com/.default"
118+
)
119+
120+
endpoint = os.getenv('AZURE_OPENAI_BASE')
117121
118-
client = AzureOPENAIClient(base_url="your_endpoint_url", credential=credential)
122+
client = AzureOpenAI(
123+
api_version="2024-02-15-preview",
124+
azure_endpoint=endpoint,
125+
azure_ad_token_provider=token_provider
126+
)
119127
```
120128
121129
### [NodeJS](#tab/nodejs)
122130
123131
1. Install dependencies.
124132
```bash
125133
npm install --save @azure/identity
126-
npm install @azure/app-configuration
134+
npm install @azure/openai
127135
```
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.
136+
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.
129137
130138
```javascript
131139
import { DefaultAzureCredential,ClientSecretCredential } from "@azure/identity";
132-
const appConfig = require("@azure/app-configuration");
133140
134141
// Uncomment the following lines according to the authentication type.
135142
// for system-assigned managed identity
@@ -147,12 +154,8 @@ ms.author: wchi
147154
// const clientSecret = process.env.AZURE_OPENAI_CLIENTSECRET;
148155
// const credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
149156
150-
const endpoint = process.env.AZURE_OPENAI_ENDPOINT;
151-
152-
const client = new appConfig.OPENAIClient(
153-
endpoint,
154-
credential
155-
);
157+
const endpoint = process.env.AZURE_OPENAI_BASE;
158+
const client = new OpenAIClient(endpoint, credential);
156159
```
157160
158161
### [Other](#tab/none)

articles/service-connector/includes/code-openai-secret.md

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ ms.author: wchi
88

99
### [.NET](#tab/dotnet)
1010

11-
1. Install the following dependency.
11+
1. Install the following dependencies.
1212
```bash
1313
dotnet add package Azure.AI.OpenAI --prerelease
14+
dotnet add package Azure.Core --version 1.40.0
1415
```
1516
1. Get the Azure OpenAI endpoint and API key from the environment variables added by Service Connector.
1617

@@ -29,6 +30,11 @@ ms.author: wchi
2930

3031
1. Add the following dependency in your *pom.xml* file:
3132
```xml
33+
<dependency>
34+
<groupId>com.azure</groupId>
35+
<artifactId>azure-core</artifactId>
36+
<version>1.49.1</version>
37+
</dependency>
3238
<dependency>
3339
<groupId>com.azure</groupId>
3440
<artifactId>azure-ai-openai</artifactId>
@@ -47,34 +53,43 @@ ms.author: wchi
4753

4854
### [Python](#tab/python)
4955

50-
1. Install the following dependency.
56+
1. Install the following dependencies.
5157
```bash
5258
pip install openai
59+
pip install azure-core
5360
```
5461
1. Get the App Configuration connection string from the environment variables added by Service Connector.
5562
```python
5663
import os
57-
import OpenAI
64+
from openai import AzureOpenAI
5865
from azure.core.credentials import AzureKeyCredential
5966
60-
openai.api_key = os.environ['AZURE_OPENAI_KEY']
61-
openai.base_url = os.environ['AZURE_OPENAI_BASE']
62-
client = OpenAI()
67+
key = os.environ['AZURE_OPENAI_KEY']
68+
endpoint = os.environ['AZURE_OPENAI_BASE']
69+
client = AzureOpenAI(
70+
api_version="2024-02-15-preview",
71+
azure_endpoint=endpoint,
72+
api_key=key
73+
)
6374
```
6475

6576
### [NodeJS](#tab/nodejs)
6677

67-
1. Install the following dependency.
78+
1. Install the following dependencies.
6879
```bash
69-
npm install @azure/app-configuration
80+
npm install @azure/openai
81+
npm install @azure/core-auth
7082
```
7183
1. Get the App Configuration connection string from the environment variables added by Service Connector.
7284

7385
```javascript
74-
const appConfig = require("@azure/app-configuration");
86+
import { OpenAIClient } from "@azure/openai";
87+
import { AzureKeyCredential } from "@azure/core-auth";
88+
89+
const endpoint = process.env.AZURE_OPENAI_BASE;
90+
const credential = new AzureKeyCredential(process.env.AZURE_OPENAI_KEY);
7591
76-
const connection_string = process.env.AZURE_APPCONFIGURATION_CONNECTIONSTRING;
77-
const client = new appConfig.AppConfigurationClient(connection_string);
92+
const client = new OpenAIClient(endpoint, credential);
7893
```
7994
8095
### [Other](#tab/none)

articles/service-connector/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ items:
164164
href: how-to-integrate-storage-file.md
165165
- name: Azure Key Vault
166166
href: how-to-integrate-key-vault.md
167+
- name: Azure OpenAI
168+
href: how-to-integrate-openai.md
167169
- name: Azure Queue Storage
168170
href: how-to-integrate-storage-queue.md
169171
- name: Azure Service Bus

0 commit comments

Comments
 (0)