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.
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:
1. Run the application with the following command:
115
+
116
+
```shell
117
+
dotnet run
118
+
```
69
119
70
-
## Output
120
+
121
+
#### Output
71
122
72
123
```output
73
124
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
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)
112
159
{
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
+
}
114
169
}
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.
0 commit comments