Skip to content

Commit 3e6f378

Browse files
committed
initial commit
1 parent 7426cfe commit 3e6f378

File tree

2 files changed

+137
-62
lines changed

2 files changed

+137
-62
lines changed

docs/ai/quickstarts/quickstart-openai-summarize-text.md

Lines changed: 128 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,45 +6,27 @@ ms.topic: quickstart
66
ms.custom: devx-track-dotnet, devx-track-dotnet-ai
77
author: fboucher
88
ms.author: frbouche
9-
zone_pivot_groups: openai-library
9+
zone_pivot_groups: dotnet-ai-library
1010
# CustomerIntent: As a .NET developer new to OpenAI, I want deploy and use sample code to interact to learn from the sample code to summarize text.
1111
---
1212

1313
# Summarize text using AI chat app with .NET
1414

15-
:::zone target="docs" pivot="openai"
15+
:::zone target="docs" pivot="semantic-kernel"
1616

17-
Get started with AI by creating a simple .NET 8.0 console chat application to summarize text. The application runs locally and uses the OpenAI `gpt-3.5-turbo` model. Follow these steps to get access to OpenAI and learn how to use Semantic Kernel.
18-
19-
[!INCLUDE [download-alert](includes/prerequisites-openai.md)]
20-
21-
:::zone-end
17+
Get started with AI by creating a simple .NET 8.0 console chat application to summarize text. The application runs locally and uses the OpenAI `gpt-3.5-turbo` model. Follow these steps to get access to OpenAI and learn how to use the `Microsoft.Extensions.AI` library.
2218

23-
:::zone target="docs" pivot="azure-openai"
24-
25-
Get started with AI by creating a simple .NET 8.0 console chat application to summarize text. The app runs locally and connects to the OpenAI `gpt-35-turbo` model deployed into Azure OpenAI. Follow these steps to provision the Azure OpenAI service and learn how to use Semantic Kernel.
26-
27-
[!INCLUDE [download-alert](includes/prerequisites-azure-openai.md)]
28-
29-
:::zone-end
19+
[!INCLUDE [prerequisites](includes/prerequisites-openai.md)]
3020

3121
## Get the sample project
3222

3323
[!INCLUDE [clone-sample-repo](includes/clone-sample-repo.md)]
3424

35-
:::zone target="docs" pivot="azure-openai"
36-
37-
## Create the Azure OpenAI service
38-
39-
# [Azure Developer CLI](#tab/azd)
40-
41-
[!INCLUDE [deploy-azd](includes/deploy-azd.md)]
42-
43-
# [Azure CLI](#tab/azure-cli)
25+
## Try the hiking benefits sample
4426

45-
1. To provision an Azure OpenAI service and model using the Azure CLI, complete the steps in the [Create and deploy an Azure OpenAI Service resource](/azure/ai-services/openai/how-to/create-resource?pivots=cli) article.
27+
# [OpenAI](#tab/openai)
4628

47-
1. From a terminal or command prompt, navigate to the `src\quickstarts\azure-openai\01-HikeBenefitsSummary` directory.
29+
1. From a terminal or command prompt, navigate to the `openai\01-HikeBenefitsSummary` directory.
4830

4931
1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
5032

@@ -53,26 +35,124 @@ Get started with AI by creating a simple .NET 8.0 console chat application to su
5335
dotnet user-secrets set OpenAIKey <your-openai-key>
5436
```
5537

56-
# [Azure Portal](#tab/azure-portal)
38+
1. Use the `dotnet run` command to run the app:
39+
40+
```dotnetcli
41+
dotnet run
42+
```
5743

58-
1. To provision an Azure OpenAI service and model using the Azure portal, complete the steps in the [Create and deploy an Azure OpenAI Service resource](/azure/ai-services/openai/how-to/create-resource?pivots=web-portal) article.
44+
# [Azure OpenAI](#tab/azure-openai)
5945

60-
1. From a terminal or command prompt, navigate to the `src\quickstarts\azure-openai\01-HikeBenefitsSummary` directory.
46+
1. From a terminal or command prompt, navigate to the `azure-openai\01-HikeBenefitsSummary` directory.
6147

62-
1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
48+
1. Run the following commands to configure your Azure OpenAI API key as a secret for the sample app:
6349

6450
```bash
6551
dotnet user-secrets init
66-
dotnet user-secrets set OpenAIKey <your-openai-key>
52+
dotnet user-secrets set AzureOpenAIKey <your-azure-openai-key>
53+
```
54+
55+
1. Use the `dotnet run` command to run the app:
56+
57+
```dotnetcli
58+
dotnet run
6759
```
6860

6961
---
7062

63+
## Explore the code
64+
65+
The app uses the [`Microsoft.Extensions.AI`](https://www.nuget.org/packages/Microsoft.Extensions.AI) package to send and receive requests to the OpenAI service.
66+
67+
The **Program.cs** file contains all of the app code. The first several lines of code set configuration values and get the OpenAI Key that was previously set using the `dotnet user-secrets` command.
68+
69+
# [OpenAI](#tab/openai)
70+
71+
```csharp
72+
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
73+
string model = "gpt-3.5-turbo";
74+
string key = config["OpenAIKey"];
75+
```
76+
77+
# [Azure OpenAI](#tab/azure-openai)
78+
79+
```csharp
80+
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
81+
string model = "gpt-3.5-turbo";
82+
string key = config["AzureOpenAIKey"];
83+
```
84+
85+
---
86+
87+
The following code obtains an `IChatClient` service configured to connect to the AI Model. The `OpenAI` and `Azure.AI.OpenAI` libraries implement types defined in the `Microsoft.Extensions.AI` library, which enables you to code using the `IChatClient` interface abstraction. This abstraction allows you to change the underlying AI provider to other services by updating only a few lines of code, such as Ollama or Azure Inference models.
88+
89+
90+
# [OpenAI](#tab/openai)
91+
92+
```csharp
93+
// Create the IChatClient
94+
IChatClient client =
95+
new OpenAIClient(new Uri(endpoint), new ApiKeyCredential(key))
96+
.AsChatClient(deployment);
97+
```
98+
99+
# [Azure OpenAI](#tab/azure-openai)
100+
101+
```csharp
102+
// Create the IChatClient
103+
IChatClient client =
104+
new OpenAIClient(key)
105+
.AsChatClient(model);
106+
```
107+
108+
---
109+
110+
The following code creates a prompt on behalf to send to the AI model.
111+
112+
```csharp
113+
string prompt = $"""
114+
Please summarize the the following text in 20 words or less:
115+
{File.ReadAllText("benefits.md")}
116+
""";
117+
118+
Console.WriteLine($"user >>> {prompt}");
119+
```
120+
121+
The `CompleteAsync` function sends the `prompt` to the model to generate a response.
122+
123+
```csharp
124+
// Submit the prompt and print out the response
125+
ChatCompletion response =
126+
await client.CompleteAsync(prompt, new ChatOptions { MaxOutputTokens = 400 });
127+
128+
Console.WriteLine($"assistant >>> {response}");
129+
```
130+
131+
Customize the text content of the file or the length of the summary to see the differences in the responses.
132+
71133
:::zone-end
72134
135+
:::zone target="docs" pivot="semantic-kernel"
136+
137+
Get started with AI by creating a simple .NET 8.0 console chat application to summarize text. The application runs locally and uses the OpenAI `gpt-3.5-turbo` model. Follow these steps to get access to OpenAI and learn how to use Semantic Kernel.
138+
139+
# [OpenAI](#tab/openai)
140+
141+
[!INCLUDE [openai-prereqs](includes/prerequisites-openai.md)]
142+
143+
# [Azure OpenAI](#tab/azure-openai)
144+
145+
[!INCLUDE [azure-openai-prereqs](includes/prerequisites-azure-openai.md)]
146+
147+
---
148+
149+
## Get the sample project
150+
151+
[!INCLUDE [clone-sample-repo](includes/clone-sample-repo.md)]
152+
73153
## Try the hiking benefits sample
74154
75-
:::zone target="docs" pivot="openai"
155+
# [OpenAI](#tab/openai)
76156
77157
1. From a terminal or command prompt, navigate to the `openai\01-HikeBenefitsSummary` directory.
78158
@@ -89,52 +169,40 @@ Get started with AI by creating a simple .NET 8.0 console chat application to su
89169
dotnet run
90170
```
91171
92-
:::zone-end
93-
94-
:::zone target="docs" pivot="azure-openai"
172+
# [Azure OpenAI](#tab/azure-openai)
95173
96174
1. From a terminal or command prompt, navigate to the `azure-openai\01-HikeBenefitsSummary` directory.
97175
98-
2. Use the `dotnet run` command to run the app:
176+
1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
177+
178+
```bash
179+
dotnet user-secrets init
180+
dotnet user-secrets set AzureOpenAIKey <your-openai-key>
181+
```
182+
183+
1. Use the `dotnet run` command to run the app:
99184
100185
```dotnetcli
101186
dotnet run
102187
```
103188
104-
> [!TIP]
105-
> If you get an error message, the Azure OpenAI resources may not have finished deploying. Wait a couple of minutes and try again.
106-
107-
:::zone-end
189+
---
108190
109191
## Explore the code
110192
111-
:::zone target="docs" pivot="openai"
112-
113193
The app uses the [`Microsoft.SemanticKernel`](https://www.nuget.org/packages/Microsoft.SemanticKernel) package to send and receive requests to the OpenAI service.
114194
115195
The **Program.cs** file contains all of the app code. The first several lines of code set configuration values and get the OpenAI Key that was previously set using the `dotnet user-secrets` command.
116196
197+
# [OpenAI](#tab/openai)
198+
117199
```csharp
118200
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
119201
string model = "gpt-3.5-turbo";
120202
string key = config["OpenAIKey"];
121203
```
122204
123-
The `Kernel` class facilitates the requests and responses and registers an `OpenAIChatCompletion` service.
124-
125-
```csharp
126-
// Create a Kernel containing the OpenAI Chat Completion Service
127-
Kernel kernel = Kernel.CreateBuilder()
128-
.AddOpenAIChatCompletion(model, key)
129-
.Build();
130-
```
131-
132-
:::zone-end
133-
134-
:::zone target="docs" pivot="azure-openai"
135-
The application uses the [`Microsoft.SemanticKernel`](https://www.nuget.org/packages/Microsoft.SemanticKernel) package to send and receive requests to the Azure OpenAI service.
136-
137-
The **Program.cs** file contains all of the app code. The first several lines of code load secrets and configuration values that were set in the `dotnet user-secrets` for you during the application provisioning.
205+
# [Azure OpenAI](#tab/azure-openai)
138206
139207
```csharp
140208
// Retrieve the local secrets saved during the Azure deployment
@@ -144,17 +212,17 @@ string deployment = config["AZURE_OPENAI_GPT_NAME"];
144212
string key = config["AZURE_OPENAI_KEY"];
145213
```
146214
215+
---
216+
147217
The `Kernel` class facilitates the requests and responses and registers an `OpenAIChatCompletion` service.
148218
149219
```csharp
150-
// Create a Kernel containing the Azure OpenAI Chat Completion Service
220+
// Create a Kernel containing the OpenAI Chat Completion Service
151221
Kernel kernel = Kernel.CreateBuilder()
152-
.AddAzureOpenAIChatCompletion(deployment, endpoint, key)
222+
.AddOpenAIChatCompletion(model, key)
153223
.Build();
154224
```
155225
156-
:::zone-end
157-
158226
Once the `Kernel` is created, the app code reads the `benefits.md` file content and uses it to create a `prompt` for model. The prompt instructs the model to summarize the file text content.
159227
160228
```csharp
@@ -182,8 +250,6 @@ Console.WriteLine($"assistant >>> {response}");
182250
183251
Customize the text content of the file or the length of the summary to see the differences in the responses.
184252
185-
:::zone target="docs" pivot="azure-openai"
186-
187253
## Clean up resources
188254
189255
When you no longer need the sample application or resources, remove the corresponding deployment and all resources.

docs/zone-pivot-groups.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,12 @@ groups:
154154
title: OpenAI
155155
- id: azure-openai
156156
title: Azure OpenAI
157+
- id: dotnet-ai-library
158+
title: .NET AI Library
159+
prompt: Choose a .NET AI SDK
160+
pivots:
161+
- id: microsoft-extensions-ai
162+
title: Microsoft.Extensions.AI
163+
- id: semantic-kernel
164+
title: Semantic Kernel
165+

0 commit comments

Comments
 (0)