Skip to content

Commit 6650f4d

Browse files
Merge pull request #281163 from jeffwmartinez/jefmarti-mi-updates
improving workflow for java and py
2 parents ce419fb + 676403d commit 6650f4d

File tree

2 files changed

+77
-86
lines changed

2 files changed

+77
-86
lines changed

articles/app-service/includes/deploy-intelligent-apps/deploy-intelligent-apps-linux-java-pivot.md

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -29,51 +29,11 @@ For this Spring Boot application, we are building off the [quickstart](../../qui
2929
}
3030
```
3131

32-
### Secure your app with managed identity
33-
34-
Although optional, it's highly recommended to secure your application using [managed identity](../../overview-managed-identity.md) to authenticate your app to your Azure OpenAI resource. Skip this step if you are not using Azure OpenAI. This enables your application to access the Azure OpenAI resource without needing to manage API keys.
35-
36-
Follow the steps below to secure your application:
37-
38-
Add the Azure OpenAI dependency package. This package enables using Azure credentials in your app.
39-
40-
```java
41-
<dependency>
42-
<groupId>com.azure</groupId>
43-
<artifactId>azure-ai-openai</artifactId>
44-
<version>1.0.0-beta.9</version>
45-
</dependency>
46-
```
47-
48-
Next, include the default Azure default credentials when creating the client
49-
50-
```java
51-
TokenCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
52-
53-
OpenAIClient client = new OpenAIClientBuilder()
54-
.credential(defaultCredential)
55-
.endpoint("{endpoint}")
56-
.buildClient();
57-
```
58-
59-
Once the credentials are added to the application, you’ll then need to enable managed identity in your application and grant access to the resource.
60-
61-
1. In your web app resource, navigate to the **Identity** blade and turn on **System assigned** and click **Save**
62-
2. Once System assigned identity is turned on, it will register the web app with Microsoft Entra ID and the web app can be granted permissions to access protected resources.
63-
3. Go to your Azure OpenAI resource and navigate to the **Access control (IAM)** blade on the left pane.
64-
4. Find the Grant access to this resource card and click on **Add role assignment**
65-
5. Search for the **Cognitive Services OpenAI User** role and click **Next**
66-
6. On the **Members** tab, find **Assign access to** and choose the **Managed identity** option
67-
7. Next, click on **+Select Members** and find your web app
68-
8. Click **Review + assign**
69-
70-
Your web app is now added as a cognitive service OpenAI user and can communicate to your Azure OpenAI resource.
71-
7232
### API Keys and Endpoints
7333

7434
First, you need to grab the keys and endpoint values from Azure OpenAI, or OpenAI and add them as secrets for use in your application. Retrieve and save the values for later use to build the client.
7535

76-
For Azure OpenAI, see [this documentation](../../../ai-services/openai/quickstart.md?pivots=programming-language-csharp&tabs=command-line%2Cpython#retrieve-key-and-endpoint) to retrieve the key and endpoint values. For our application, you need the following values:
36+
For Azure OpenAI, see [this documentation](../../../ai-services/openai/quickstart.md?pivots=programming-language-csharp&tabs=command-line%2Cpython#retrieve-key-and-endpoint) to retrieve the key and endpoint values. If you're planning to use [managed identity](../../overview-managed-identity.md) to secure your app you'll only need the `endpoint` value. Otherwise, you need each of the following:
7737

7838
- `endpoint`
7939
- `apiKey`
@@ -139,11 +99,11 @@ For OpenAI:
13999

140100
Before you can create the client, you first need to add the Azure SDK dependency. Add the following Azure OpenAI package to the *pom.xl* file and run the **mvn package** command to build the package.
141101

142-
```python
102+
```java
143103
<dependency>
144104
<groupId>com.azure</groupId>
145105
<artifactId>azure-ai-openai</artifactId>
146-
<version>1.0.0-beta.6</version>
106+
<version>1.0.0-beta.9</version>
147107
</dependency>
148108
```
149109

@@ -180,6 +140,36 @@ import com.azure.ai.openai.OpenAIClientBuilder;
180140
import com.azure.core.credential.AzureKeyCredential;
181141
```
182142

143+
### Secure your app with managed identity
144+
145+
Although optional, it's highly recommended to secure your application using [managed identity](../../overview-managed-identity.md) to authenticate your app to your Azure OpenAI resource. Skip this step if you are not using Azure OpenAI. This enables your application to access the Azure OpenAI resource without needing to manage API keys.
146+
147+
Follow the steps below to secure your application:
148+
149+
The Azure SDK package previously installed in the previous section enables the use of default credentials in your app. Include the default Azure default credentials when you create the client.
150+
151+
```java
152+
TokenCredential defaultCredential = new DefaultAzureCredentialBuilder().build();
153+
154+
OpenAIClient client = new OpenAIClientBuilder()
155+
.credential(defaultCredential)
156+
.endpoint(endpoint)
157+
.buildClient();
158+
```
159+
160+
Once the credentials are added to the application, you’ll then need to enable managed identity in your application and grant access to the resource.
161+
162+
1. In your web app resource, navigate to the **Identity** blade and turn on **System assigned** and click **Save**
163+
2. Once System assigned identity is turned on, it will register the web app with Microsoft Entra ID and the web app can be granted permissions to access protected resources.
164+
3. Go to your Azure OpenAI resource and navigate to the **Access control (IAM)** blade on the left pane.
165+
4. Find the Grant access to this resource card and click on **Add role assignment**
166+
5. Search for the **Cognitive Services OpenAI User** role and click **Next**
167+
6. On the **Members** tab, find **Assign access to** and choose the **Managed identity** option
168+
7. Next, click on **+Select Members** and find your web app
169+
8. Click **Review + assign**
170+
171+
Your web app is now added as a cognitive service OpenAI user and can communicate to your Azure OpenAI resource.
172+
183173
### Setup prompt and call to OpenAI
184174

185175
Now that our OpenAI service is created we can use the chat completions method to send our request message to OpenAI and return a response. Here's where we add our chat message prompt to the code to be passed to the chat completions method. Use the following code to set up the chat completions method:

articles/app-service/includes/deploy-intelligent-apps/deploy-intelligent-apps-linux-python-pivot.md

Lines changed: 44 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ You can use Azure App Service to work with popular AI frameworks like LangChain
1919

2020
For this Flask web application, we are building off the [quickstart](../../quickstart-python.md?tabs=flask%2Cwindows%2Cazure-cli%2Cvscode-deploy%2Cdeploy-instructions-azportal%2Cterminal-bash%2Cdeploy-instructions-zip-azcli#1---sample-application) app and updating the *app.py* file to send and receive requests to an Azure OpenAI OR OpenAI service using LangChain.
2121

22-
First, copy and replace the *index.html* file with the following code:
22+
First, copy, and replace the *index.html* file with the following code:
2323

2424
```html
2525
<!doctype html>
@@ -54,7 +54,7 @@ First, copy and replace the *index.html* file with the following code:
5454
</html>
5555
```
5656

57-
Next, copy and replace the *hello.html* file with the following code:
57+
Next, copy, and replace the *hello.html* file with the following code:
5858

5959
```html
6060
<!doctype html>
@@ -82,54 +82,16 @@ Next, copy and replace the *hello.html* file with the following code:
8282

8383
After the files are updated, we can start preparing our environment variables to work with OpenAI.
8484

85-
### Secure your app with managed identity
86-
87-
Although optional, it's highly recommended to secure your application using [managed identity](../../overview-managed-identity.md) to authenticate your app to your Azure OpenAI resource. Skip this step if you are not using Azure OpenAI. This enables your application to access the Azure OpenAI resource without needing to manage API keys.
88-
89-
Follow the steps below to secure your application:
90-
91-
Add the identity package `Azure.Identity`. This package enables using Azure credentials in your app. Install the package and import the default credential and bearer token provider.
92-
93-
```python
94-
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
95-
```
96-
97-
Next, include the default Azure credentials and token provider in the AzureOpenAI options.
98-
99-
```python
100-
token_provider = get_bearer_token_provider(
101-
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
102-
)
103-
104-
client = AzureOpenAI(
105-
api_version="2024-02-15-preview",
106-
azure_endpoint="https://{your-custom-endpoint}.openai.azure.com/",
107-
azure_ad_token_provider=token_provider
108-
)
109-
```
110-
111-
Once the credentials are added to the application, you’ll then need to enable managed identity in your application and grant access to the resource.
112-
113-
1. In your web app resource, navigate to the **Identity** blade and turn on **System assigned** and click **Save**
114-
2. Once System assigned identity is turned on, it will register the web app with Microsoft Entra ID and the web app can be granted permissions to access protected resources.
115-
3. Go to your Azure OpenAI resource and navigate to the **Access control (IAM)** blade on the left pane.
116-
4. Find the Grant access to this resource card and click on **Add role assignment**
117-
5. Search for the **Cognitive Services OpenAI User** role and click **Next**
118-
6. On the **Members** tab, find **Assign access to** and choose the **Managed identity** option
119-
7. Next, click on **+Select Members** and find your web app
120-
8. Click **Review + assign**
121-
122-
Your web app is now added as a cognitive service OpenAI user and can communicate to your Azure OpenAI resource.
123-
12485
### API Keys and Endpoints
12586

12687
In order to make calls to OpenAI with your client, you need to first grab the Keys and Endpoint values from Azure OpenAI, or OpenAI and add them as secrets for use in your application. Retrieve and save the values for later use.
12788

128-
For Azure OpenAI, see [this documentation](../../../ai-services/openai/quickstart.md?pivots=programming-language-csharp&tabs=command-line%2Cpython#retrieve-key-and-endpoint) to retrieve the key and endpoint values. For our application, you need the following values:
89+
For Azure OpenAI, see [this documentation](../../../ai-services/openai/quickstart.md?pivots=programming-language-csharp&tabs=command-line%2Cpython#retrieve-key-and-endpoint) to retrieve the key and endpoint values. If you're planning to use [managed identity](../../overview-managed-identity.md) to secure your app you'll only need the `api_version` and `azure__endpoint` values. Otherwise, you need each of the following:
12990

13091
- `api_key`
13192
- `api_version`
13293
- `azure_deployment`
94+
- `azure_endpoint`
13395
- `model_name`
13496

13597
For OpenAI, see this [documentation](https://platform.openai.com/docs/api-reference) to retrieve the API keys. For our application, you need the following values:
@@ -202,7 +164,7 @@ from langchain_openai import AzureOpenAI~~
202164

203165
After LangChain is imported into our file, you can add the code that will call to OpenAI with the LangChain invoke chat method. Update *app.py `http://app.py`* to include the following code:
204166

205-
For Azure OpenAI, use the following code:
167+
For Azure OpenAI, use the following code. If you plan to use managed identity you can use the credentials outlined in the following section for the Azure OpenAI parameters.
206168

207169
```python
208170
@app.route('/hello', methods=['POST'])
@@ -294,6 +256,45 @@ if __name__ == '__main__':
294256

295257
Now save the application and follow the next steps to deploy it to App Service. If you would like to test it locally first at this step, you can swap out the key and endpoint values with the literal string values of your OpenAI service. For example: model_name = 'gpt-4-turbo';
296258

259+
### Secure your app with managed identity
260+
261+
Although optional, it's highly recommended to secure your application using [managed identity](../../overview-managed-identity.md) to authenticate your app to your Azure OpenAI resource. Skip this step if you are not using Azure OpenAI. This enables your application to access the Azure OpenAI resource without needing to manage API keys.
262+
263+
Follow the steps below to secure your application:
264+
265+
Add the identity package `Azure.Identity`. This package enables using Azure credentials in your app. Install the package and import the default credential and bearer token provider.
266+
267+
```python
268+
from azure.identity import DefaultAzureCredential, get_bearer_token_provider
269+
```
270+
271+
Next, include the default Azure credentials and token provider in the AzureOpenAI options.
272+
273+
```python
274+
token_provider = get_bearer_token_provider(
275+
DefaultAzureCredential(), "https://cognitiveservices.azure.com/.default"
276+
)
277+
278+
client = AzureOpenAI(
279+
api_version="2024-02-15-preview",
280+
azure_endpoint="https://{your-custom-endpoint}.openai.azure.com/",
281+
azure_ad_token_provider=token_provider
282+
)
283+
```
284+
285+
Once the credentials are added to the application, you’ll then need to enable managed identity in your application and grant access to the resource.
286+
287+
1. In your web app resource, navigate to the **Identity** blade and turn on **System assigned** and click **Save**
288+
2. Once System assigned identity is turned on, it registers the web app with Microsoft Entra ID and the web app can be granted permissions to access protected resources.
289+
3. Go to your Azure OpenAI resource and navigate to the **Access control (IAM)** blade on the left pane.
290+
4. Find the Grant access to this resource card and click on **Add role assignment**
291+
5. Search for the **Cognitive Services OpenAI User** role and click **Next**
292+
6. On the **Members** tab, find **Assign access to** and choose the **Managed identity** option
293+
7. Next, click on **+Select Members** and find your web app
294+
8. Click **Review + assign**
295+
296+
Your web app is now added as a cognitive service OpenAI user and can communicate to your Azure OpenAI resource.
297+
297298
### Deploy to App Service
298299

299300
Before deploying to App Service, you need to edit the *requirments.txt* file and add an environment variable to your web app so it recognizes the LangChain library and build properly.

0 commit comments

Comments
 (0)