Skip to content

Commit cbfc580

Browse files
committed
Entra ID for AOAI QS - C#
1 parent aa09425 commit cbfc580

File tree

10 files changed

+160
-134
lines changed

10 files changed

+160
-134
lines changed

articles/ai-services/openai/how-to/dotnet-migration.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ OpenAIClient client = new(
4848

4949
```csharp
5050
// 2.0 - NEW: Get a chat completions client from a top-level Azure client
51-
AzureOpenAIClient azureClient = new(
51+
AzureOpenAIClient openAIClient = new(
5252
new Uri("https://your-resource.openai.azure.com/"),
5353
new AzureKeyCredential(Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY"));
54-
ChatClient chatClient = azureClient.GetChatClient("my-gpt-4o-mini-deployment");
54+
ChatClient chatClient = openAIClient.GetChatClient("my-gpt-4o-mini-deployment");
5555
```
5656

5757
Just like in 1.0, the new `AzureOpenAIClient` supports the use of Microsoft Entra ID credentials when the [Azure.Identity](/dotnet/api/overview/azure/identity-readme?view=azure-dotnet&preserve-view=true) package is installed.

articles/ai-services/openai/includes/assistants-csharp.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ The sample code in this quickstart uses Microsoft Entra ID for the recommended k
6969
#### [Microsoft Entra ID](#tab/keyless)
7070

7171
```csharp
72-
var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
72+
AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
7373
```
7474
7575
#### [API key](#tab/api-key)
7676
7777
```csharp
78-
var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
78+
AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
7979
```
8080
---
8181
@@ -101,8 +101,8 @@ To create an assistant, you need to:
101101
string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? "<your-key>";
102102
103103
// Use the recommended keyless credential instead of the AzureKeyCredential credential.
104-
var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
105-
//var openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
104+
AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
105+
//AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
106106
107107
OpenAIFileClient fileClient = openAIClient.GetOpenAIFileClient();
108108
AssistantClient assistantClient = openAIClient.GetAssistantClient();

articles/ai-services/openai/includes/chatgpt-dotnet.md

Lines changed: 131 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -16,58 +16,109 @@ ms.date: 11/15/2023
1616

1717
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
1818
- The [.NET 7 SDK](https://dotnet.microsoft.com/download/dotnet/7.0)
19-
- An Azure OpenAI Service resource with either the `gpt-35-turbo` or the `gpt-4` models deployed. For more information about model deployment, see the [resource deployment guide](../how-to/create-resource.md).
19+
- An Azure OpenAI Service resource with the `gpt-4o` model deployed. For more information about model deployment, see the [resource deployment guide](../how-to/create-resource.md).
2020

21+
### Microsoft Entra ID prerequisites
22+
23+
For the recommended keyless authentication with Microsoft Entra ID, you need to:
24+
- Install the [Azure CLI](/cli/azure/install-azure-cli) used for keyless authentication with Microsoft Entra ID.
25+
- Assign the `Cognitive Services User` role to your user account. You can assign roles in the Azure portal under **Access control (IAM)** > **Add role assignment**.
2126

2227
## Set up
2328

24-
[!INCLUDE [Create a new .NET application](./dotnet-new-application.md)]
29+
1. Create a new folder `chat-quickstart` to contain the application and open Visual Studio Code in that folder with the following command:
2530

26-
[!INCLUDE [get-key-endpoint](get-key-endpoint.md)]
31+
```shell
32+
mkdir chat-quickstart && cd chat-quickstart
33+
```
2734

28-
[!INCLUDE [environment-variables](environment-variables.md)]
35+
1. Create a new console application with the following command:
2936

37+
```shell
38+
dotnet new console
39+
```
3040

31-
## Create a sample application
41+
3. Install the [OpenAI .NET client library](https://www.nuget.org/packages/Azure.AI.OpenAI/) with the [dotnet add package](/dotnet/core/tools/dotnet-add-package) command:
3242

33-
From the project directory, open the *program.cs* file and replace with the following code:
43+
```console
44+
dotnet add package Azure.AI.OpenAI --prerelease
45+
```
3446

35-
### Without response streaming
47+
1. For the **recommended** keyless authentication with Microsoft Entra ID, install the [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) package with:
3648

37-
```csharp
38-
using Azure;
39-
using Azure.AI.OpenAI;
40-
using static System.Environment;
49+
```console
50+
dotnet add package Azure.Identity
51+
```
52+
53+
1. For the **recommended** keyless authentication with Microsoft Entra ID, sign in to Azure with the following command:
54+
55+
```console
56+
az login
57+
```
4158

42-
string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
43-
string key = GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
59+
## Retrieve resource information
4460

45-
AzureOpenAIClient azureClient = new(
46-
new Uri(endpoint),
47-
new AzureKeyCredential(key));
61+
[!INCLUDE [resource authentication](resource-authentication.md)]
4862

49-
// This must match the custom deployment name you chose for your model
50-
ChatClient chatClient = azureClient.GetChatClient("gpt-35-turbo");
63+
## Add the code for chat completion
5164

52-
ChatCompletion completion = chatClient.CompleteChat(
53-
[
54-
new SystemChatMessage("You are a helpful assistant that talks like a pirate."),
55-
new UserChatMessage("Does Azure OpenAI support customer managed keys?"),
56-
new AssistantChatMessage("Yes, customer managed keys are supported by Azure OpenAI"),
57-
new UserChatMessage("Do other Azure AI services support this too?")
58-
]);
65+
The sample code in this quickstart uses Microsoft Entra ID for the recommended keyless authentication. If you prefer to use an API key, you can replace the `DefaultAzureCredential` object with a `AzureKeyCredential` object.
5966

60-
Console.WriteLine($"{completion.Role}: {completion.Content[0].Text}");
67+
#### [Microsoft Entra ID](#tab/keyless)
68+
69+
```csharp
70+
AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
6171
```
6272
63-
> [!IMPORTANT]
64-
> For production, use a secure way of storing and accessing your credentials like [Azure Key Vault](/azure/key-vault/general/overview). For more information about credential security, see the Azure AI services [security](../../security-features.md) article.
73+
#### [API key](#tab/api-key)
6574
66-
```cmd
67-
dotnet run program.cs
75+
```csharp
76+
AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
6877
```
78+
---
79+
80+
You can use streaming or non-streaming to get the chat completion. The following code examples show how to use both methods. The first example shows how to use the non-streaming method, and the second example shows how to use the streaming method.
81+
82+
### Without response streaming
83+
84+
To use the non-streaming method:
85+
86+
1. Update the `Program.cs` file with the following code:
87+
88+
```csharp
89+
using Azure;
90+
using Azure.AI.OpenAI;
91+
using static System.Environment;
92+
93+
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? "https://<your-resource-name>.openai.azure.com/";
94+
string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? "<your-key>";
95+
96+
// Use the recommended keyless credential instead of the AzureKeyCredential credential.
97+
AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
98+
//AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
99+
100+
// This must match the custom deployment name you chose for your model
101+
ChatClient chatClient = openAIClient.GetChatClient("gpt-4o");
102+
103+
ChatCompletion completion = chatClient.CompleteChat(
104+
[
105+
new SystemChatMessage("You are a helpful assistant that talks like a pirate."),
106+
new UserChatMessage("Does Azure OpenAI support customer managed keys?"),
107+
new AssistantChatMessage("Yes, customer managed keys are supported by Azure OpenAI"),
108+
new UserChatMessage("Do other Azure AI services support this too?")
109+
]);
110+
111+
Console.WriteLine($"{completion.Role}: {completion.Content[0].Text}");
112+
```
113+
114+
1. Run the application with the following command:
115+
116+
```shell
117+
dotnet run
118+
```
69119
70-
## Output
120+
121+
#### Output
71122
72123
```output
73124
Assistant : Yes, many other Azure AI services also support customer managed keys, including Azure Cognitive Services, Azure Machine Learning, and Azure Databricks. By using customer managed keys, you can retain complete control over your encryption keys and provide an additional layer of security for your AI assets.
@@ -77,42 +128,58 @@ This will wait until the model has generated its entire response before printing
77128
78129
### Async with streaming
79130
80-
```csharp
81-
using Azure;
82-
using Azure.AI.OpenAI;
83-
using OpenAI.Chat;
84-
using static System.Environment;
85-
86-
string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
87-
string key = GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
88-
89-
AzureOpenAIClient azureClient = new(
90-
new Uri(endpoint),
91-
new AzureKeyCredential(key));
92-
93-
// This must match the custom deployment name you chose for your model
94-
ChatClient chatClient = azureClient.GetChatClient("gpt-35-turbo");
95-
96-
var chatUpdates = chatClient.CompleteChatStreamingAsync(
97-
[
98-
new SystemChatMessage("You are a helpful assistant that talks like a pirate."),
99-
new UserChatMessage("Does Azure OpenAI support customer managed keys?"),
100-
new AssistantChatMessage("Yes, customer managed keys are supported by Azure OpenAI"),
101-
new UserChatMessage("Do other Azure AI services support this too?")
102-
]);
103-
104-
await foreach(var chatUpdate in chatUpdates)
105-
{
106-
if (chatUpdate.Role.HasValue)
107-
{
108-
Console.Write($"{chatUpdate.Role} : ");
109-
}
131+
To use the streaming method:
132+
1. Update the `Program.cs` file with the following code:
133+
134+
```csharp
135+
using Azure;
136+
using Azure.AI.OpenAI;
137+
using OpenAI.Chat;
138+
using static System.Environment;
139+
140+
string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? "https://<your-resource-name>.openai.azure.com/";
141+
string key = Environment.GetEnvironmentVariable("AZURE_OPENAI_API_KEY") ?? "<your-key>";
142+
143+
// Use the recommended keyless credential instead of the AzureKeyCredential credential.
144+
AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
145+
//AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
110146
111-
foreach(var contentPart in chatUpdate.ContentUpdate)
147+
// This must match the custom deployment name you chose for your model
148+
ChatClient chatClient = openAIClient.GetChatClient("gpt-4o");
149+
150+
var chatUpdates = chatClient.CompleteChatStreamingAsync(
151+
[
152+
new SystemChatMessage("You are a helpful assistant that talks like a pirate."),
153+
new UserChatMessage("Does Azure OpenAI support customer managed keys?"),
154+
new AssistantChatMessage("Yes, customer managed keys are supported by Azure OpenAI"),
155+
new UserChatMessage("Do other Azure AI services support this too?")
156+
]);
157+
158+
await foreach(var chatUpdate in chatUpdates)
112159
{
113-
Console.Write(contentPart.Text);
160+
if (chatUpdate.Role.HasValue)
161+
{
162+
Console.Write($"{chatUpdate.Role} : ");
163+
}
164+
165+
foreach(var contentPart in chatUpdate.ContentUpdate)
166+
{
167+
Console.Write(contentPart.Text);
168+
}
114169
}
115-
}
170+
```
171+
172+
1. Run the application with the following command:
173+
174+
```shell
175+
dotnet run
176+
```
177+
178+
179+
#### Output
180+
181+
```output
182+
Assistant : Yes, many other Azure AI services also support customer managed keys, including Azure Cognitive Services, Azure Machine Learning, and Azure Databricks. By using customer managed keys, you can retain complete control over your encryption keys and provide an additional layer of security for your AI assets.
116183
```
117184
118185

articles/ai-services/openai/includes/dall-e-dotnet.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,12 @@ using static System.Environment;
7474
string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
7575
string key = GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
7676

77-
AzureOpenAIClient azureClient = new(
77+
AzureOpenAIClient openAIClient = new(
7878
new Uri(endpoint),
7979
new AzureKeyCredential(key));
8080

8181
// This must match the custom deployment name you chose for your model
82-
ImageClient chatClient = azureClient.GetImageClient("dalle-3");
82+
ImageClient chatClient = openAIClient.GetImageClient("dalle-3");
8383

8484
var imageGeneration = await chatClient.GenerateImageAsync(
8585
"a happy monkey sitting in a tree, in watercolor",

articles/ai-services/openai/includes/dotnet-new-application.md

Lines changed: 0 additions & 39 deletions
This file was deleted.

articles/ai-services/openai/includes/gpt-v-dotnet.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ Passwordless authentication is more secure than key-based alternatives and is th
8484
// var credentials = new DefaultAzureCredential(); // Use this line for Passwordless auth
8585
var deploymentName = "gpt-4"; // Default name, update with your own if needed
8686
87-
var openAIClient = new AzureOpenAIClient(endpoint, credentials);
87+
AzureOpenAIClient openAIClient = new AzureOpenAIClient(endpoint, credentials);
8888
var chatClient = openAIClient.GetChatClient(deploymentName);
8989
9090
var imageUri = "YOUR_IMAGE_URL";

0 commit comments

Comments
 (0)