Skip to content

Commit 5b71a9b

Browse files
authored
Merge pull request #280258 from alexwolfmsft/openai-quickstart-updates
Openai quickstart updates
2 parents eeedd67 + 8cd1c31 commit 5b71a9b

File tree

4 files changed

+170
-167
lines changed

4 files changed

+170
-167
lines changed

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

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,22 @@ using static System.Environment;
5050
string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
5151
string key = GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
5252

53-
OpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key));
54-
55-
var chatCompletionsOptions = new ChatCompletionsOptions()
56-
{
57-
DeploymentName = "gpt-35-turbo", //This must match the custom deployment name you chose for your model
58-
Messages =
59-
{
60-
new ChatRequestSystemMessage("You are a helpful assistant."),
61-
new ChatRequestUserMessage("Does Azure OpenAI support customer managed keys?"),
62-
new ChatRequestAssistantMessage("Yes, customer managed keys are supported by Azure OpenAI."),
63-
new ChatRequestUserMessage("Do other Azure AI services support this too?"),
64-
},
65-
MaxTokens = 100
66-
};
67-
68-
Response<ChatCompletions> response = client.GetChatCompletions(chatCompletionsOptions);
69-
70-
Console.WriteLine(response.Value.Choices[0].Message.Content);
71-
72-
Console.WriteLine();
53+
AzureOpenAIClient azureClient = new(
54+
new Uri(endpoint),
55+
new AzureKeyCredential(key));
56+
57+
// This must match the custom deployment name you chose for your model
58+
ChatClient chatClient = azureClient.GetChatClient("gpt-35-turbo");
59+
60+
ChatCompletion completion = chatClient.CompleteChat(
61+
[
62+
new SystemChatMessage("You are a helpful assistant that talks like a pirate."),
63+
new UserChatMessage("Does Azure OpenAI support customer managed keys?"),
64+
new AssistantChatMessage("Yes, customer managed keys are supported by Azure OpenAI"),
65+
new UserChatMessage("Do other Azure AI services support this too?")
66+
]);
67+
68+
Console.WriteLine($"{completion.Role}: {completion.Content[0].Text}");
7369
```
7470

7571
> [!IMPORTANT]
@@ -82,7 +78,7 @@ dotnet run program.cs
8278
## Output
8379

8480
```output
85-
Yes, many of the Azure AI services support customer managed keys. Some examples include Text Analytics, Speech Services, and Translator. However, it's important to note that not all services support customer managed keys, so it's best to check the documentation for each individual service to see if it is supported.
81+
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.
8682
```
8783

8884
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.
@@ -92,35 +88,37 @@ This will wait until the model has generated its entire response before printing
9288
```csharp
9389
using Azure;
9490
using Azure.AI.OpenAI;
91+
using OpenAI.Chat;
9592
using static System.Environment;
9693

9794
string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
9895
string key = GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
9996

100-
OpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key));
97+
AzureOpenAIClient azureClient = new(
98+
new Uri(endpoint),
99+
new AzureKeyCredential(key));
101100

102-
var chatCompletionsOptions = new ChatCompletionsOptions()
103-
{
104-
DeploymentName= "gpt-35-turbo", //This must match the custom deployment name you chose for your model
105-
Messages =
106-
{
107-
new ChatRequestSystemMessage("You are a helpful assistant."),
108-
new ChatRequestUserMessage("Does Azure OpenAI support customer managed keys?"),
109-
new ChatRequestAssistantMessage("Yes, customer managed keys are supported by Azure OpenAI."),
110-
new ChatRequestUserMessage("Do other Azure AI services support this too?"),
111-
},
112-
MaxTokens = 100
113-
};
114-
115-
await foreach (StreamingChatCompletionsUpdate chatUpdate in client.GetChatCompletionsStreaming(chatCompletionsOptions))
101+
// This must match the custom deployment name you chose for your model
102+
ChatClient chatClient = azureClient.GetChatClient("gpt-35-turbo");
103+
104+
var chatUpdates = chatClient.CompleteChatStreamingAsync(
105+
[
106+
new SystemChatMessage("You are a helpful assistant that talks like a pirate."),
107+
new UserChatMessage("Does Azure OpenAI support customer managed keys?"),
108+
new AssistantChatMessage("Yes, customer managed keys are supported by Azure OpenAI"),
109+
new UserChatMessage("Do other Azure AI services support this too?")
110+
]);
111+
112+
await foreach(var chatUpdate in chatUpdates)
116113
{
117114
if (chatUpdate.Role.HasValue)
118115
{
119-
Console.Write($"{chatUpdate.Role.Value.ToString().ToUpperInvariant()}: ");
116+
Console.Write($"{chatUpdate.Role} : ");
120117
}
121-
if (!string.IsNullOrEmpty(chatUpdate.ContentUpdate))
118+
119+
foreach(var contentPart in chatUpdate.ContentUpdate)
122120
{
123-
Console.Write(chatUpdate.ContentUpdate);
121+
Console.Write(contentPart.Text);
124122
}
125123
}
126124
```

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

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -70,38 +70,30 @@ dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.6
7070
From the project directory, open the *program.cs* file and replace the contents with the following code:
7171

7272
```csharp
73-
using System;
74-
using System.IO;
75-
using System.Threading.Tasks;
73+
using Azure;
7674
using Azure.AI.OpenAI;
75+
using OpenAI.Images;
76+
using static System.Environment;
7777

78-
namespace Azure.AI.OpenAI.Tests.Samples
79-
{
80-
public partial class GenerateImages
81-
{
82-
// add an async Main method:
83-
public static async Task Main(string[] args)
78+
string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
79+
string key = GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
80+
81+
AzureOpenAIClient azureClient = new(
82+
new Uri(endpoint),
83+
new AzureKeyCredential(key));
84+
85+
// This must match the custom deployment name you chose for your model
86+
ImageClient chatClient = azureClient.GetImageClient("dalle-3");
87+
88+
var imageGeneration = await chatClient.GenerateImageAsync(
89+
"a happy monkey sitting in a tree, in watercolor",
90+
new ImageGenerationOptions()
8491
{
85-
string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
86-
string key = GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
87-
88-
OpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key));
89-
90-
Response<ImageGenerations> imageGenerations = await client.GetImageGenerationsAsync(
91-
new ImageGenerationOptions()
92-
{
93-
Prompt = "a happy monkey eating a banana, in watercolor",
94-
Size = ImageSize.Size256x256,
95-
});
96-
97-
// Image Generations responses provide URLs you can use to retrieve requested images
98-
Uri imageUri = imageGenerations.Value.Data[0].Url;
99-
100-
// Print the image URI to console:
101-
Console.WriteLine(imageUri);
92+
Size = GeneratedImageSize.W1024xH1024
10293
}
103-
}
104-
}
94+
);
95+
96+
Console.WriteLine(imageGeneration.Value.ImageUri);
10597
```
10698

10799
Build and run the application from your application directory with these commands:
@@ -116,7 +108,7 @@ dotnet run
116108
The URL of the generated image is printed to the console.
117109

118110
```console
119-
https://dalleproduse.blob.core.windows.net/private/images/552c5522-af4a-4877-a19c-400fac04a422/generated_00.png?se=2023-08-17T16%3A54%3A40Z&sig=XGCIx9r0WvWTJ0LL%2FJGymo2WYp4FDbSQNNrGRUnnUzI%3D&ske=2023-08-19T01%3A10%3A14Z&skoid=09ba021e-c417-441c-b203-c81e5dcd7b7f&sks=b&skt=2023-08-12T01%3A10%3A14Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02
111+
https://dalleproduse.blob.core.windows.net/private/images/b7ac5e55-f1f8-497a-8d0e-8f51446bf538/generated_00.png?se=2024-07-12T13%3A47%3A56Z&sig=Zri37iYVTVtc52qzTFBOqPgSHvXwEhcO86Smv2ojB%2FE%3D&ske=2024-07-17T12%3A15%3A44Z&skoid=09ba021e-c417-441c-b203-c81e5dcd7b7f&sks=b&skt=2024-07-10T12%3A15%3A44Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02
120112
```
121113

122114
> [!NOTE]

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,43 @@ ms.date: 07/26/2023
2626
2727
## Set up
2828

29-
[!INCLUDE [Create a new .NET application](./dotnet-new-application.md)]
29+
### Create a new .NET Core application
30+
31+
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*.
32+
33+
```dotnetcli
34+
dotnet new console -n azure-openai-quickstart
35+
```
36+
37+
Change your directory to the newly created app folder. You can build the application with:
38+
39+
```dotnetcli
40+
dotnet build
41+
```
42+
43+
The build output should contain no warnings or errors.
44+
45+
```output
46+
...
47+
Build succeeded.
48+
0 Warning(s)
49+
0 Error(s)
50+
...
51+
```
52+
53+
Install the OpenAI .NET client library with:
54+
55+
```console
56+
dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.17
57+
```
58+
59+
> [!NOTE]
60+
> The completions API is only available in version `1.0.0-beta.17` and earlier of the `Azure.AI.OpenAI` client library. For the latest `2.0.0` and higher version of `Azure.AI.OpenAI`, the recommended approach to generate completions is to use the [chat completions API](/azure/ai-services/openai/chatgpt-quickstart).
3061
3162
[!INCLUDE [get-key-endpoint](get-key-endpoint.md)]
3263

3364
[!INCLUDE [environment-variables](environment-variables.md)]
3465

35-
3666
> [!div class="nextstepaction"]
3767
> [I ran into an issue with the setup.](https://microsoft.qualtrics.com/jfe/form/SV_0Cl5zkG3CnDjq6O?PLanguage=DOTNET&Pillar=AOAI&Product=gpt&Page=quickstart&Section=Set-up)
3868
@@ -48,11 +78,13 @@ using static System.Environment;
4878
string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
4979
string key = GetEnvironmentVariable("AZURE_OPENAI_API_KEY");
5080

51-
var client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
81+
var client = new OpenAIClient(
82+
new Uri(endpoint),
83+
new AzureKeyCredential(key));
5284

5385
CompletionsOptions completionsOptions = new()
5486
{
55-
DeploymentName = "gpt-35-turbo-instruct",
87+
DeploymentName = "gpt-35-turbo-instruct",
5688
Prompts = { "When was Microsoft founded?" },
5789
};
5890

0 commit comments

Comments
 (0)