You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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.
11
11
---
12
12
13
13
# Summarize text using AI chat app with .NET
14
14
15
-
:::zone target="docs" pivot="openai"
15
+
:::zone target="docs" pivot="semantic-kernel"
16
16
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.
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.
22
18
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.
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)
46
28
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.
48
30
49
31
1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
50
32
@@ -53,26 +35,124 @@ Get started with AI by creating a simple .NET 8.0 console chat application to su
53
35
dotnet user-secrets set OpenAIKey <your-openai-key>
54
36
```
55
37
56
-
# [Azure Portal](#tab/azure-portal)
38
+
1. Use the `dotnet run`command to run the app:
39
+
40
+
```dotnetcli
41
+
dotnet run
42
+
```
57
43
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)
59
45
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.
61
47
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:
63
49
64
50
```bash
65
51
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
67
59
```
68
60
69
61
---
70
62
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`functionsends 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
+
71
133
:::zone-end
72
134
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.
1. From a terminal or command prompt, navigate to the `openai\01-HikeBenefitsSummary` directory.
78
158
@@ -89,52 +169,40 @@ Get started with AI by creating a simple .NET 8.0 console chat application to su
89
169
dotnet run
90
170
```
91
171
92
-
:::zone-end
93
-
94
-
:::zone target="docs" pivot="azure-openai"
172
+
# [Azure OpenAI](#tab/azure-openai)
95
173
96
174
1. From a terminal or command prompt, navigate to the `azure-openai\01-HikeBenefitsSummary` directory.
97
175
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:
99
184
100
185
```dotnetcli
101
186
dotnet run
102
187
```
103
188
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
+
---
108
190
109
191
## Explore the code
110
192
111
-
:::zone target="docs" pivot="openai"
112
-
113
193
The app uses the [`Microsoft.SemanticKernel`](https://www.nuget.org/packages/Microsoft.SemanticKernel) package to send and receive requests to the OpenAI service.
114
194
115
195
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.
116
196
197
+
# [OpenAI](#tab/openai)
198
+
117
199
```csharp
118
200
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
119
201
string model = "gpt-3.5-turbo";
120
202
string key = config["OpenAIKey"];
121
203
```
122
204
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 setin the `dotnet user-secrets`for you during the application provisioning.
205
+
# [Azure OpenAI](#tab/azure-openai)
138
206
139
207
```csharp
140
208
// Retrieve the local secrets saved during the Azure deployment
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.
0 commit comments