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
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
18
18
- 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).
20
20
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**.
21
26
22
27
## Set up
23
28
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:
1. Create a new console application with the following command:
29
36
37
+
```shell
38
+
dotnet new console
39
+
```
30
40
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:
32
42
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
+
```
34
46
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:
36
48
37
-
```csharp
38
-
usingAzure;
39
-
usingAzure.AI.OpenAI;
40
-
usingstaticSystem.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:
newSystemChatMessage("You are a helpful assistant that talks like a pirate."),
55
-
newUserChatMessage("Does Azure OpenAI support customer managed keys?"),
56
-
newAssistantChatMessage("Yes, customer managed keys are supported by Azure OpenAI"),
57
-
newUserChatMessage("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.
AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
61
71
```
62
72
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)
65
74
66
-
```cmd
67
-
dotnet run program.cs
75
+
```csharp
76
+
AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
68
77
```
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.
69
81
70
-
## Output
82
+
### Without response streaming
83
+
84
+
To use the non-streaming method:
85
+
86
+
1. Update the `Program.cs` file with the following code:
1. Run the application with the following command:
118
+
119
+
```shell
120
+
dotnet run
121
+
```
122
+
123
+
124
+
#### Output
71
125
72
126
```output
73
-
Assistant : Yes, many other Azure AI services also support customermanaged 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.
127
+
Assistant: Arrr, ye be askin’ a fine question, matey! Aye, several Azure AI services support customer-managed keys (CMK)! This lets ye take the wheel and secure yer data with encryption keys stored inAzure Key Vault. Services such as Azure Machine Learning, Azure Cognitive Search, and others also offer CMK fer data protection. Always check the specific service's documentation fer the latest updates, as features tend to shift swifter than the tides, aye!
74
128
```
75
129
76
130
This will wait until the model has generated its entire response before printing the results. Alternatively, if you want to asynchronously stream the response and print the results, you can replace the contents of *program.cs* with the code in the next example.
var chatUpdates = chatClient.CompleteChatStreamingAsync(
156
+
[
157
+
new SystemChatMessage("You are a helpful assistant that talks like a pirate."),
158
+
new UserChatMessage("Does Azure OpenAI support customer managed keys?"),
159
+
new AssistantChatMessage("Yes, customer managed keys are supported by Azure OpenAI"),
160
+
new UserChatMessage("Do other Azure AI services support this too?")
161
+
]);
162
+
163
+
await foreach(var chatUpdate in chatUpdates)
112
164
{
113
-
Console.Write(contentPart.Text);
165
+
if (chatUpdate.Role.HasValue)
166
+
{
167
+
Console.Write($"{chatUpdate.Role} : ");
168
+
}
169
+
170
+
foreach(var contentPart in chatUpdate.ContentUpdate)
171
+
{
172
+
Console.Write(contentPart.Text);
173
+
}
114
174
}
115
-
}
175
+
```
176
+
177
+
1. Run the application with the following command:
178
+
179
+
```shell
180
+
dotnet run
181
+
```
182
+
183
+
184
+
#### Output
185
+
186
+
```output
187
+
Assistant: Arrr, ye be askin’ a fine question, matey! Aye, several Azure AI services support customer-managed keys (CMK)! This lets ye take the wheel and secure yer data with encryption keys stored in Azure Key Vault. Services such as Azure Machine Learning, Azure Cognitive Search, and others also offer CMK fer data protection. Always check the specific service's documentation fer the latest updates, as features tend to shift swifter than the tides, aye!
0 commit comments