You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/app-service/includes/deploy-intelligent-apps/deploy-intelligent-apps-linux-dotnet-pivot.md
+41-41Lines changed: 41 additions & 41 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,7 +8,7 @@ ms.date: 04/10/2024
8
8
ms.author: jefmarti
9
9
---
10
10
11
-
You can use Azure App Service to work with popular AI frameworks like LangChain and Semantic Kernel connected to OpenAI for creating intelligent apps. In the following tutorial, we are adding an Azure OpenAI service using Semantic Kernel to a .NET 8 Blazor web application.
11
+
You can use Azure App Service to work with popular AI frameworks like LangChain and Semantic Kernel connected to OpenAI for creating intelligent apps. In the following tutorial, we're adding an Azure OpenAI service using Semantic Kernel to a .NET 8 Blazor web application.
12
12
13
13
#### Prerequisites
14
14
@@ -17,7 +17,7 @@ You can use Azure App Service to work with popular AI frameworks like LangChain
17
17
18
18
### Setup Blazor web app
19
19
20
-
For this Blazor web application, we are building off the Blazor [template](https://dotnet.microsoft.com/learn/aspnet/blazor-tutorial/intro) and creating a new razor page that can send and receive requests to an Azure OpenAI OR OpenAI service using Semantic Kernel.
20
+
For this Blazor web application, we're building off the Blazor [template](https://dotnet.microsoft.com/learn/aspnet/blazor-tutorial/intro) and creating a new razor page that can send and receive requests to an Azure OpenAI OR OpenAI service using Semantic Kernel.
21
21
22
22
1. Right click on the **Pages** folder found under the **Components** folder and add a new item named *OpenAI.razor*
23
23
2. Add the following code to the **OpenAI.razor* file and click **Save**
@@ -60,48 +60,11 @@ Next, we need to add the new page to the navigation so we can navigate to the se
60
60
61
61
After the Navigation is updated, we can start preparing to build the OpenAI client to handle our requests.
62
62
63
-
### Secure your app with managed identity
64
-
65
-
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.
66
-
67
-
Follow the steps below to secure your application:
68
-
69
-
Add the identity package `Azure.Identity`. This package enables using Azure credentials in your app. Install the package using Nuget package manager and add the using statement to the top of the OpenAI.razor file.
70
-
71
-
```c#
72
-
@using Azure.Identity
73
-
```
74
-
75
-
Next, include the default Azure credentials in the chat completions options
76
-
77
-
```c#
78
-
var kernel = Kernel.CreateBuilder()
79
-
.AddAzureOpenAIChatCompletion(
80
-
deploymentName: deploymentName,
81
-
endpoint: endpoint,
82
-
credentials: new DefaultAzureCredential()
83
-
)
84
-
.Build();
85
-
```
86
-
87
-
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.
88
-
89
-
1. In your web app resource, navigate to the **Identity** blade and turn on **System assigned** and click **Save**
90
-
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.
91
-
3. Go to your Azure OpenAI resource and navigate to the **Access control (IAM)** blade on the left pane.
92
-
4. Find the Grant access to this resource card and click on **Add role assignment**
93
-
5. Search for the **Cognitive Services OpenAI User** role and click **Next**
94
-
6. On the **Members** tab, find **Assign access to** and choose the **Managed identity** option
95
-
7. Next, click on **+Select Members** and find your web app
96
-
8. Click **Review + assign**
97
-
98
-
Your web app is now added as a cognitive service OpenAI user and can communicate to your Azure OpenAI resource.
99
-
100
63
### API keys and endpoints
101
64
102
65
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.
103
66
104
-
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, youneed the following values:
67
+
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 `deploymentName` and `endpoint` values. Otherwise, you need each of the following:
105
68
106
69
- `deploymentName`
107
70
- `endpoint`
@@ -198,7 +161,7 @@ Here we're adding the using statement and creating the Kernel in a method that w
Once the Kernel is initialized, we can add our chosen AI service to the kernel. Here we define our model and pass in our key and endpoint information to be consumed by the chosen model. If you plan to use managed identity with Azure OpenAI, add the service using the example in the next section.
If you’re using Azure OpenAI, it's highly recommended to secure your application using [managed identity](../../overview-managed-identity.md) to authenticate your app to your Azure OpenAI resource. This enables your application to access the Azure OpenAI resource without needing to manage API keys. If you're not using Azure OpenAI, your secrets can remain secure using Azure Key Vault outlined above.
194
+
195
+
Follow the steps below to secure your application with managed identity:
196
+
197
+
Add the identity package `Azure.Identity`. This package enables using Azure credentials in your app. Install the package using Nuget package manager and add the using statement to the top of the OpenAI.razor file.
198
+
199
+
```c#
200
+
@usingAzure.Identity
201
+
```
202
+
203
+
Next, include the default Azure credentials in the chat completions parameters. The `deploymentName` and `endpoint` parameters are still required and should be secured using the Key Vault method covered in the previous section.
204
+
205
+
```c#
206
+
varkernel=Kernel.CreateBuilder()
207
+
.AddAzureOpenAIChatCompletion(
208
+
deploymentName: deploymentName,
209
+
endpoint: endpoint,
210
+
credentials: newDefaultAzureCredential()
211
+
)
212
+
.Build();
213
+
```
214
+
215
+
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.
216
+
217
+
1. In your web app resource, navigate to the **Identity** blade and turn on **System assigned** and click **Save**
218
+
2. Once System assigned identity is turned on, it register's the web app with Microsoft Entra ID and the web app can be granted permissions to access protected resources.
219
+
3. Go to your Azure OpenAI resource and navigate to the **Access control (IAM)** blade on the left pane.
220
+
4. Find the Grant access to this resource card and click on **Add role assignment**
221
+
5. Search for the **Cognitive Services OpenAI User** role and click **Next**
222
+
6. On the **Members** tab, find **Assign access to** and choose the **Managed identity** option
223
+
7. Next, click on **+Select Members** and find your web app
224
+
8. Click **Review + assign**
225
+
226
+
Your web app is now added as a cognitive service OpenAI user and can communicate to your Azure OpenAI resource.
227
+
228
228
### Configure prompt and create semantic function
229
229
230
230
Now that our chosen OpenAI service client is created with the correct keys we can add a function to handle the prompt. With Semantic Kernel you can handle prompts by the use of a semantic function, which turn the prompt and the prompt configuration settings into a function the Kernel can execute. Learn more on configuring prompts [here](/semantic-kernel/prompts/configure-prompts?tabs=Csharp).
0 commit comments