Skip to content

Commit 4a077f9

Browse files
authored
Merge pull request #230781 from mrbullwinkle/mrb_03_14_2023_openai_net_quickstart
[Cognitive Services] [Azure OpenAI] Update quickstart
2 parents f13f64d + b97ce89 commit 4a077f9

File tree

3 files changed

+187
-4
lines changed

3 files changed

+187
-4
lines changed
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
---
2+
title: 'Quickstart: Use Azure OpenAI Service with the C# SDK'
3+
titleSuffix: Azure OpenAI
4+
description: Walkthrough on how to get started with Azure OpenAI and make your first completions call with the C# SDK.
5+
services: cognitive-services
6+
manager: nitinme
7+
ms.service: cognitive-services
8+
ms.subservice: openai
9+
ms.topic: include
10+
author: mrbullwinkle
11+
ms.author: mbullwin
12+
ms.date: 03/14/2023
13+
keywords:
14+
---
15+
16+
[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)
17+
18+
## Prerequisites
19+
20+
- An Azure subscription - [Create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true)
21+
- Access granted to the Azure OpenAI service in the desired Azure subscription.
22+
Currently, access to this service is granted only by application. You can apply for access to Azure OpenAI Service by completing the form at [https://aka.ms/oai/access](https://aka.ms/oai/access?azure-portal=true).
23+
- The current version of <a href="https://dotnet.microsoft.com/download/dotnet-core" target="_blank">.NET Core</a>
24+
- An Azure OpenAI Service resource with the `text-davinci-003` model deployed. For more information about model deployment, see the [resource deployment guide](../how-to/create-resource.md).
25+
26+
## Set up
27+
28+
### Create a new .NET Core application
29+
30+
In a console window (such as cmd, PowerShell, or Bash), use the `dotnet new` command to create a new console app with the name `anomaly-detector-quickstart`. This command creates a simple "Hello World" project with a single C# source file: *Program.cs*.
31+
32+
```dotnetcli
33+
dotnet new console -n azure-openai-quickstart
34+
```
35+
36+
Change your directory to the newly created app folder. You can build the application with:
37+
38+
```dotnetcli
39+
dotnet build
40+
```
41+
42+
The build output should contain no warnings or errors.
43+
44+
```output
45+
...
46+
Build succeeded.
47+
0 Warning(s)
48+
0 Error(s)
49+
...
50+
```
51+
52+
Install the OpenAI .NET client library with:
53+
54+
```console
55+
dotnet add package Azure.AI.OpenAI --prerelease
56+
```
57+
58+
### Retrieve key and endpoint
59+
60+
To successfully make a call against Azure OpenAI, you'll need an **endpoint** and a **key**.
61+
62+
|Variable name | Value |
63+
|--------------------------|-------------|
64+
| `ENDPOINT` | This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. Alternatively, you can find the value in the **Azure OpenAI Studio** > **Playground** > **Code View**. An example endpoint is: `https://docs-test-001.openai.azure.com/`.|
65+
| `API-KEY` | This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. You can use either `KEY1` or `KEY2`.|
66+
67+
Go to your resource in the Azure portal. The **Endpoint and Keys** can be found in the **Resource Management** section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either `KEY1` or `KEY2`. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
68+
69+
:::image type="content" source="../media/quickstarts/endpoint.png" alt-text="Screenshot of the overview blade for an OpenAI Resource in the Azure portal with the endpoint & access keys location circled in red." lightbox="../media/quickstarts/endpoint.png":::
70+
71+
Create and assign persistent environment variables for your key and endpoint.
72+
73+
### Environment variables
74+
75+
# [Command Line](#tab/command-line)
76+
77+
```CMD
78+
setx OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE"
79+
```
80+
81+
```CMD
82+
setx OPENAI_API_BASE "REPLACE_WITH_YOUR_ENDPOINT_HERE"
83+
```
84+
85+
# [PowerShell](#tab/powershell)
86+
87+
```powershell
88+
[System.Environment]::SetEnvironmentVariable('OPENAI_API_KEY', 'REPLACE_WITH_YOUR_KEY_VALUE_HERE', 'User')
89+
```
90+
91+
```powershell
92+
[System.Environment]::SetEnvironmentVariable('OPENAI_API_BASE', 'REPLACE_WITH_YOUR_ENDPOINT_HERE', 'User')
93+
```
94+
95+
# [Bash](#tab/bash)
96+
97+
```Bash
98+
echo export OPENAI_API_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" >> /etc/environment && source /etc/environment
99+
```
100+
101+
```Bash
102+
echo export OPENAI_API_BASE="REPLACE_WITH_YOUR_ENDPOINT_HERE" >> /etc/environment && source /etc/environment
103+
```
104+
---
105+
106+
## Create .NET application
107+
108+
From the project directory, open the *program.cs* file and replace with the following code:
109+
110+
```csharp
111+
using Azure;
112+
using Azure.AI.OpenAI;
113+
using static System.Environment;
114+
115+
namespace azure_openai_quickstart
116+
{
117+
internal class Program
118+
{
119+
static void Main(string[] args)
120+
{
121+
string endpoint = GetEnvironmentVariable("OPENAI_API_BASE");
122+
string key = GetEnvironmentVariable("OPENAI_API_KEY");
123+
string engine = "text-davinci-003"; //Enter the deployment name you chose when you deployed the model.
124+
125+
OpenAIClient client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
126+
127+
string prompt = "When was Microsoft founded?";
128+
Console.Write($"Input: {prompt}\n");
129+
130+
Response<Completions> completionsResponse = client.GetCompletions(engine, prompt);
131+
string completion = completionsResponse.Value.Choices[0].Text;
132+
Console.WriteLine($"Chatbot: {completion}");
133+
134+
}
135+
}
136+
}
137+
138+
```
139+
140+
> [!IMPORTANT]
141+
> For production, use a secure way of storing and accessing your credentials like [Azure Key Vault](../../../key-vault/general/overview.md). For more information about credential security, see the Cognitive Services [security](../../security-features.md) article.
142+
143+
```cmd
144+
dotnet run program.cs
145+
```
146+
147+
## Output
148+
149+
```console
150+
Input: When was Microsoft founded?
151+
Chatbot:
152+
153+
Microsoft was founded on April 4, 1975
154+
```
155+
156+
## Clean up resources
157+
158+
If you want to clean up and remove an OpenAI resource, you can delete the resource. Before deleting the resource you must first delete any deployed models.
159+
160+
- [Portal](../../cognitive-services-apis-create-account.md#clean-up-resources)
161+
- [Azure CLI](../../cognitive-services-apis-create-account-cli.md#clean-up-resources)
162+
163+
## Next steps
164+
165+
* For more examples check out the [Azure OpenAI Samples GitHub repository](https://github.com/Azure/openai-samples)

articles/cognitive-services/openai/quickstart.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ manager: nitinme
77
ms.service: cognitive-services
88
ms.subservice: openai
99
ms.topic: quickstart
10-
author: ChrisHMSFT
11-
ms.author: chrhoder
12-
ms.date: 02/02/2023
13-
zone_pivot_groups: openai-quickstart
10+
author: mrbullwinkle
11+
ms.author: mbullwin
12+
ms.date: 03/15/2023
13+
zone_pivot_groups: openai-quickstart-new
1414
recommendations: false
1515
---
1616

@@ -24,6 +24,12 @@ Use this article to get started making your first calls to Azure OpenAI.
2424

2525
::: zone-end
2626

27+
::: zone pivot="programming-language-csharp"
28+
29+
[!INCLUDE [Csharp quickstart](includes/dotnet.md)]
30+
31+
::: zone-end
32+
2733
::: zone pivot="programming-language-python"
2834

2935
[!INCLUDE [Python SDK quickstart](includes/python.md)]

articles/zone-pivot-groups.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1457,6 +1457,18 @@ groups:
14571457
- id: rest-api
14581458
title: REST
14591459
# Owner: mbullwin
1460+
- id: openai-quickstart-new
1461+
title: Programming languages
1462+
prompt: Choose your preferred usage method
1463+
pivots:
1464+
- id: programming-language-studio
1465+
title: Studio
1466+
- id: programming-language-csharp
1467+
title: C#
1468+
- id: programming-language-python
1469+
title: Python
1470+
- id: rest-api
1471+
title: REST
14601472
- id: openai-chat
14611473
title: API Options
14621474
prompt: Chat API options

0 commit comments

Comments
 (0)