Skip to content

Commit 669b009

Browse files
authored
Merge pull request #3460 from eric-urban/eur/aoai-entra-csharp
Entra ID for AOAI QS - C#
2 parents 1573719 + 1e4edc5 commit 669b009

File tree

9 files changed

+265
-210
lines changed

9 files changed

+265
-210
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author: aapowell
77
ms.author: aapowell
88
ms.service: azure-ai-openai
99
ms.topic: include
10-
ms.date: 3/10/2025
10+
ms.date: 3/11/2025
1111
---
1212

1313
[Reference documentation](/dotnet/api/overview/azure/ai.openai.assistants-readme?context=/azure/ai-services/openai/context/context) | [Source code](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/openai/Azure.AI.OpenAI/src) | [Package (NuGet)](https://www.nuget.org/packages/Azure.AI.OpenAI/)
@@ -62,9 +62,9 @@ For the recommended keyless authentication with Microsoft Entra ID, you need to:
6262

6363
[!INCLUDE [resource authentication](resource-authentication.md)]
6464

65-
## Create the assistant
65+
## Run the quickstart
6666

67-
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.
67+
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 an `AzureKeyCredential` object.
6868

6969
#### [Microsoft Entra ID](#tab/keyless)
7070

@@ -79,9 +79,9 @@ AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new Az
7979
```
8080
---
8181
82-
To create an assistant, you need to:
82+
To run the quickstart, follow these steps:
8383
84-
1. Update the `Program.cs` file with the following code to create an assistant:
84+
1. Replace the contents of `Program.cs` with the following code and update the placeholder values with your own.
8585
8686
```csharp
8787
using Azure;

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.service: azure-ai-openai
77
ms.topic: include
88
author: mrbullwinkle
99
ms.author: mbullwin
10-
ms.date: 11/15/2023
10+
ms.date: 3/11/2025
1111
---
1212

1313
[Source code](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/openai/Azure.AI.OpenAI/src) | [Package (NuGet)](https://www.nuget.org/packages/Azure.AI.OpenAI/) | [Samples](https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/openai/Azure.AI.OpenAI/tests/Samples)| [Retrieval Augmented Generation (RAG) enterprise chat template](/dotnet/ai/get-started-app-chat-template) |
@@ -60,9 +60,9 @@ For the recommended keyless authentication with Microsoft Entra ID, you need to:
6060

6161
[!INCLUDE [resource authentication](resource-authentication.md)]
6262

63-
## Add the code for chat completion
63+
## Run the quickstart
6464

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.
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 an `AzureKeyCredential` object.
6666

6767
#### [Microsoft Entra ID](#tab/keyless)
6868

@@ -81,9 +81,9 @@ You can use streaming or non-streaming to get the chat completion. The following
8181
8282
### Without response streaming
8383
84-
To use the non-streaming method:
84+
To run the quickstart, follow these steps:
8585
86-
1. Update the `Program.cs` file with the following code:
86+
1. Replace the contents of `Program.cs` with the following code and update the placeholder values with your own.
8787
8888
```csharp
8989
using Azure;
@@ -127,12 +127,13 @@ To use the non-streaming method:
127127
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!
128128
```
129129
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.
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.
131131
132132
### Async with streaming
133133
134-
To use the streaming method:
135-
1. Update the `Program.cs` file with the following code:
134+
To run the quickstart, follow these steps:
135+
136+
1. Replace the contents of `Program.cs` with the following code and update the placeholder values with your own.
136137
137138
```csharp
138139
using Azure;

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

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.service: azure-ai-openai
88
ms.topic: include
99
author: PatrickFarley
1010
ms.author: pafarley
11-
ms.date: 06/26/2024
11+
ms.date: 3/11/2025
1212
---
1313

1414
Use this guide to get started generating images with the Azure OpenAI SDK for C#.
@@ -21,83 +21,101 @@ Use this guide to get started generating images with the Azure OpenAI SDK for C#
2121
- The [.NET 7 SDK](https://dotnet.microsoft.com/download/dotnet/7.0)
2222
- An Azure OpenAI resource created in a supported region (see [Region availability](/azure/ai-services/openai/concepts/models#model-summary-table-and-region-availability)). For more information, see [Create a resource and deploy a model with Azure OpenAI](../how-to/create-resource.md).
2323

24+
### Microsoft Entra ID prerequisites
2425

25-
## Setup
26+
For the recommended keyless authentication with Microsoft Entra ID, you need to:
27+
- Install the [Azure CLI](/cli/azure/install-azure-cli) used for keyless authentication with Microsoft Entra ID.
28+
- 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**.
2629

27-
[!INCLUDE [get-key-endpoint](get-key-endpoint.md)]
30+
## Set up
2831

29-
[!INCLUDE [environment-variables](environment-variables.md)]
32+
1. Create a new folder `vision-quickstart` to contain the application and open Visual Studio Code in that folder with the following command:
3033

34+
```shell
35+
mkdir vision-quickstart && cd vision-quickstart
36+
```
3137

32-
## Create a new .NET Core application
38+
1. Create a new console application with the following command:
3339

34-
In a console window (such as cmd, PowerShell, or Bash), use the `dotnet new` command to create a new console app with the name `azure-openai-quickstart`. This command creates a simple "Hello World" project with a single C# source file: *Program.cs*.
40+
```shell
41+
dotnet new console
42+
```
3543

36-
```dotnetcli
37-
dotnet new console -n azure-openai-quickstart
38-
```
44+
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:
3945

40-
Change your directory to the newly created app folder. You can build the application with:
46+
```console
47+
dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.6
48+
```
4149

42-
```dotnetcli
43-
dotnet build
44-
```
50+
1. For the **recommended** keyless authentication with Microsoft Entra ID, install the [Azure.Identity](https://www.nuget.org/packages/Azure.Identity) package with:
4551

46-
The build output should contain no warnings or errors.
52+
```console
53+
dotnet add package Azure.Identity
54+
```
4755

48-
```output
49-
...
50-
Build succeeded.
51-
0 Warning(s)
52-
0 Error(s)
53-
...
54-
```
56+
1. For the **recommended** keyless authentication with Microsoft Entra ID, sign in to Azure with the following command:
5557

56-
## Install the OpenAI .NET SDK
58+
```console
59+
az login
60+
```
5761

58-
Install the client library with:
62+
## Retrieve resource information
5963

60-
```dotnetcli
61-
dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.6
62-
```
64+
[!INCLUDE [resource authentication](resource-authentication.md)]
6365

64-
## Generate images with DALL-E
66+
## Run the quickstart
6567

66-
From the project directory, open the *program.cs* file and replace the contents with the following code:
68+
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 an `AzureKeyCredential` object.
69+
70+
#### [Microsoft Entra ID](#tab/keyless)
6771

6872
```csharp
69-
using Azure;
70-
using Azure.AI.OpenAI;
71-
using OpenAI.Images;
72-
using static System.Environment;
73-
74-
string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
75-
string key = GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
76-
77-
AzureOpenAIClient openAIClient = new(
78-
new Uri(endpoint),
79-
new AzureKeyCredential(key));
80-
81-
// This must match the custom deployment name you chose for your model
82-
ImageClient chatClient = openAIClient.GetImageClient("dalle-3");
83-
84-
var imageGeneration = await chatClient.GenerateImageAsync(
85-
"a happy monkey sitting in a tree, in watercolor",
86-
new ImageGenerationOptions()
87-
{
88-
Size = GeneratedImageSize.W1024xH1024
89-
}
90-
);
91-
92-
Console.WriteLine(imageGeneration.Value.ImageUri);
73+
AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential());
9374
```
9475
95-
Build and run the application from your application directory with these commands:
76+
#### [API key](#tab/api-key)
9677
97-
```dotnet
98-
dotnet build
99-
dotnet run
78+
```csharp
79+
AzureOpenAIClient openAIClient = new AzureOpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
10080
```
81+
---
82+
83+
To run the quickstart, follow these steps:
84+
85+
1. Replace the contents of `Program.cs` with the following code and update the placeholder values with your own.
86+
87+
```csharp
88+
using Azure;
89+
using Azure.AI.OpenAI;
90+
using OpenAI.Images;
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+
ImageClient chatClient = openAIClient.GetImageClient("dalle-3");
102+
103+
var imageGeneration = await chatClient.GenerateImageAsync(
104+
"a happy monkey sitting in a tree, in watercolor",
105+
new ImageGenerationOptions()
106+
{
107+
Size = GeneratedImageSize.W1024xH1024
108+
}
109+
);
110+
111+
Console.WriteLine(imageGeneration.Value.ImageUri);
112+
```
113+
114+
1. Run the application using the `dotnet run` command or the run button at the top of Visual Studio:
115+
116+
```dotnetcli
117+
dotnet run
118+
```
101119
102120
## Output
103121

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.17
6262

6363
## Create a sample application
6464

65-
From the project directory, open the *program.cs* file and replace with the following code:
65+
From the project directory, open the *Program.cs* file and replace with the following code:
6666

6767
```csharp
6868
using Azure;
@@ -91,7 +91,7 @@ Console.WriteLine($"Chatbot: {completion}");
9191
> 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.
9292
9393
```cmd
94-
dotnet run program.cs
94+
dotnet run Program.cs
9595
```
9696

9797
## Output

0 commit comments

Comments
 (0)