Skip to content

Commit 38235c4

Browse files
Merge pull request #4179 from MicrosoftDocs/bretjohn/update-openai-tutorials
Improve security guidance for the OpenAI API key
2 parents c70ac12 + dc25d8f commit 38235c4

File tree

4 files changed

+30
-11
lines changed

4 files changed

+30
-11
lines changed

hub/apps/how-tos/chatgpt-openai-winui3.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ After creating your project, you should see the following default file structure
4141

4242
## Set your environment variable
4343

44-
In order to use the OpenAI SDK, you'll need to set an environment variable with your API key. In this example, we'll use the `MY_OPEN_AI_API_KEY` environment variable. Once you have your API key from the [OpenAI developer dashboard](https://platform.openai.com/api-keys), you can set the environment variable from the command line as follows:
44+
In order to use the OpenAI SDK, you'll need to set an environment variable with your API key. In this example, we'll use the `OPENAI_API_KEY` environment variable. Once you have your API key from the [OpenAI developer dashboard](https://platform.openai.com/api-keys), you can set the environment variable from the command line as follows:
4545

4646
```powershell
47-
setx MY_OPEN_AI_API_KEY <your-api-key>
47+
setx OPENAI_API_KEY <your-api-key>
4848
```
4949

50-
Note that this method works well for development, but you'll want to use a more secure method for production apps (for example: you could store your API key in a secure key vault that a remote service can access on behalf of your app).
50+
Note that this method works well for development, but you'll want to use a more secure method for production apps (for example: you could store your API key in a secure key vault that a remote service can access on behalf of your app). See [Best practices for OpenAI key safety](https://help.openai.com/articles/5112595-best-practices-for-api-key-safety).
5151

5252

5353
## Install the OpenAI SDK
@@ -80,7 +80,7 @@ namespace ChatGPT_WinUI3
8080
{
8181
this.InitializeComponent();
8282

83-
var openAiKey = Environment.GetEnvironmentVariable("MY_OPEN_AI_API_KEY");
83+
var openAiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
8484

8585
openAiService = new OpenAIService(new OpenAiOptions(){
8686
ApiKey = openAiKey
@@ -446,7 +446,7 @@ namespace ChatGPT_WinUI3
446446
{
447447
this.InitializeComponent();
448448

449-
var openAiKey = Environment.GetEnvironmentVariable("MY_OPEN_AI_API_KEY");
449+
var openAiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
450450

451451
openAiService = new OpenAIService(new OpenAiOptions(){
452452
ApiKey = openAiKey

hub/apps/how-tos/dall-e-winui3.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ In this how-to, we'll integrate DALL-E's image generation capabilities into your
2020

2121
- [Visual Studio 2022 and Tools for Windows App SDK](../windows-app-sdk/set-up-your-development-environment.md)
2222
- A functional chat interface into which this capability will be integrated. See *[How to add OpenAI chat completions to your WinUI 3 / Windows App SDK desktop app](./chatgpt-openai-winui3.md)* - we'll demonstrate how to integrate DALL-E into the chat interface from this how-to.
23-
- An OpenAI API key from your [OpenAI developer dashboard](https://platform.openai.com/api-keys) assigned to the `MY_OPEN_AI_API_KEY` environment variable.
23+
- An OpenAI API key from your [OpenAI developer dashboard](https://platform.openai.com/api-keys) assigned to the `OPENAI_API_KEY` environment variable.
2424
- An OpenAI SDK installed in your project. Refer to the [OpenAI documentation](https://platform.openai.com/docs/libraries) for a list of community libraries. In this how-to, we'll use [betalgo/openai](https://github.com/betalgo/openai).
2525

2626
## Install and initialize the OpenAI SDK
@@ -44,7 +44,7 @@ namespace ChatGPT_WinUI3
4444
{
4545
this.InitializeComponent();
4646

47-
var openAiKey = Environment.GetEnvironmentVariable("MY_OPEN_AI_API_KEY");
47+
var openAiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
4848

4949
openAiService = new OpenAIService(new OpenAiOptions(){
5050
ApiKey = openAiKey

hub/apps/windows-dotnet-maui/dall-e-maui-windows.md

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,23 @@ In this quickstart, we'll demonstrate how to integrate DALL-E's image generation
1414

1515
- Visual Studio 2022 17.8 or greater, with the .NET Multi-platform App UI workload installed. For more information, see [Installation](/dotnet/maui/get-started/installation).
1616
- A functional .NET MAUI project with OpenAI integration into which this capability will be integrated. See *[Create a recommendation app with .NET MAUI and ChatGPT](tutorial-maui-ai.md)* - we'll demonstrate how to integrate DALL-E into the user interface from this how-to.
17-
- An OpenAI API key from your [OpenAI developer dashboard](https://platform.openai.com/api-keys) assigned to the `openAIKey` variable in your project.
17+
- An OpenAI API key from your [OpenAI developer dashboard](https://platform.openai.com/api-keys).
1818
- An [Azure.AI.OpenAI](https://www.nuget.org/packages/Azure.AI.OpenAI/) NuGet package installed in your project. If you've followed along with the .NET MAUI ChatGPT tutorial, you will have this dependency installed and configured.
1919

2020
## What problem will we solve?
2121

2222
You want to add DALL-E's image generation capabilities to your .NET MAUI Windows desktop app to provide users with a rich, interactive experience. They can already use the app to generate text-based recommendations, and you want to add the ability to generate images that visualize an activity in the location they have entered.
2323

24+
## Set your environment variable
25+
26+
In order to use the OpenAI SDK, you'll need to set an environment variable with your API key. In this example, we'll use the `OPENAI_API_KEY` environment variable. Once you have your API key from the [OpenAI developer dashboard](https://platform.openai.com/api-keys), you can set the environment variable from the command line as follows:
27+
28+
```powershell
29+
setx OPENAI_API_KEY <your-api-key>
30+
```
31+
32+
Note that this method works for development on Windows, but you'll want to use a more secure method for production apps and for mobile support. For example, you can store your API key in a secure key vault that a remote service can access on behalf of your app. See [Best practices for OpenAI key safety](https://help.openai.com/articles/5112595-best-practices-for-api-key-safety) for more information.
33+
2434
## Install and initialize the Azure OpenAI SDK
2535

2636
In this section, we'll install the SDK into the .NET MAUI project and initialize it with your OpenAI API key.
@@ -32,7 +42,6 @@ In this section, we'll install the SDK into the .NET MAUI project and initialize
3242
```csharp
3343
private OpenAIClient _chatGptClient;
3444
private Guid _sessionGuid = Guid.Empty;
35-
private string openAIKey = "MY_OPEN_AI_API_KEY";
3645
private string openAIEndpoint = null;
3746
private char[] trimChars = { '\n', '?' };
3847

@@ -44,6 +53,8 @@ In this section, we'll install the SDK into the .NET MAUI project and initialize
4453

4554
private void MainPage_Loaded(object sender, EventArgs e)
4655
{
56+
var openAiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");
57+
4758
_chatGptClient = !string.IsNullOrWhiteSpace(openAIEndpoint)
4859
? new OpenAIClient(
4960
new Uri(openAIEndpoint),
@@ -52,8 +63,6 @@ In this section, we'll install the SDK into the .NET MAUI project and initialize
5263
}
5364
```
5465

55-
1. Replace `MY_OPEN_AI_API_KEY` with your OpenAI API key.
56-
5766
## Modify your app's UI
5867

5968
Next, we'll modify the user interface to include an `Image` control that displays a generated image below the recommendation text.

hub/apps/windows-dotnet-maui/tutorial-maui-ai.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ In this tutorial, you learn how to:
2525
* The .NET MAUI [installation requirements](/dotnet/maui/get-started/installation)
2626
* If you are new to .NET MAUI on Windows, you should start with the [Build your first .NET MAUI app for Windows](/windows/apps/windows-dotnet-maui/walkthrough-first-app) tutorial.
2727

28+
## Set your environment variable
29+
30+
In order to use the OpenAI SDK, you'll need to set an environment variable with your API key. In this example, we'll use the `OPENAI_API_KEY` environment variable. Once you have your API key from the [OpenAI developer dashboard](https://platform.openai.com/api-keys), you can set the environment variable from the command line as follows:
31+
32+
```powershell
33+
setx OPENAI_API_KEY <your-api-key>
34+
```
35+
36+
Note that this method works for development on Windows, but you'll want to use a more secure method for production apps and for mobile support. For example, you can store your API key in a secure key vault that a remote service can access on behalf of your app. See [Best practices for OpenAI key safety](https://help.openai.com/articles/5112595-best-practices-for-api-key-safety) for more information.
37+
2838
## Create a new .NET MAUI project with the required UI elements
2939

3040
We're going to start by creating a new .NET MAUI project in Visual Studio. We'll use the **.NET MAUI App** template and add some UI elements to the **MainPage** to provide users with some recommendations based on a provided location. The UI will have buttons to get recommendations for restaurants, hotels, and attractions.

0 commit comments

Comments
 (0)