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
In this quickstart, you learn how to create a conversational .NET console chat app using an OpenAI or Azure OpenAI model. The app uses the [`Microsoft.Extensions.AI`](https://www.nuget.org/packages/Microsoft.Extensions.AI) library so you can write code using AI abstractions rather than a specific SDK. AI abstractions enable you to change the underlying AI model with minimal code changes.
16
+
17
+
> [!NOTE]
18
+
> The [`Microsoft.Extensions.AI`](https://www.nuget.org/packages/Microsoft.Extensions.AI/) library is currently in Preview.
18
19
19
-
Get started with OpenAI and [Semantic Kernel](../semantic-kernel-dotnet-overview.md) by creating a simple .NET 8 console chat application. The application will run locally and use 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 OpenAI and [Semantic Kernel](../semantic-kernel-dotnet-overview.md) by creating a simple .NET 8 console chat application. The application will run locally and connect to the OpenAI `gpt-35-turbo` model deployed into Azure OpenAI. Follow these steps to provision Azure OpenAI and learn how to use Semantic Kernel.
Complete the following steps to create a .NET console app to connect to an AI model.
42
39
43
-
# [Azure Developer CLI](#tab/azd)
40
+
1. In an empty directory on your computer, use the `dotnet new` command to create a new console app:
44
41
45
-
[!INCLUDE [deploy-azd](includes/deploy-azd.md)]
42
+
```dotnetcli
43
+
dotnet new console -o ChatAppAI
44
+
```
46
45
47
-
# [Azure CLI](#tab/azure-cli)
46
+
1. Change directory into the app folder:
48
47
49
-
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.
48
+
```dotnetcli
49
+
cd ChatAppAI
50
+
```
50
51
51
-
1.From a terminal or command prompt, navigate to the `src\quickstarts\azure-openai\semantic-kernel\02-HikerAI` directory.
52
+
1. Install the required packages:
52
53
53
-
1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
54
+
:::zone target="docs" pivot="azure-openai"
54
55
55
56
```bash
56
-
dotnet user-secrets init
57
-
dotnet user-secrets set OpenAIKey <your-openai-key>
58
-
59
-
# [Azure Portal](#tab/azure-portal)
60
-
61
-
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.
The app uses the [`Microsoft.SemanticKernel`](https://www.nuget.org/packages/Microsoft.SemanticKernel) package to send and receive requests to the OpenAI service.
91
+
## Configure the app
117
92
118
-
The app code is contained within the **Program.cs** file. The first several lines of code set configuration values and get the OpenAI Key that was previously set using the `dotnet user-secrets`command.
93
+
1. Navigate to the root of your .NET project from a terminal or command prompt.
119
94
120
-
```csharp
121
-
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
122
-
string model = "gpt-3.5-turbo";
123
-
string key = config["OpenAIKey"];
124
-
```
125
-
126
-
The `OpenAIChatCompletionService` service facilitates the requests and responses.
95
+
1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
127
96
128
-
```csharp
129
-
// Create the OpenAI Chat Completion Service
130
-
OpenAIChatCompletionService service = new(model, key);
131
-
```
97
+
```bash
98
+
dotnet user-secrets init
99
+
dotnet user-secrets set OpenAIKey <your-openai-key>
100
+
dotnet user-secrets set ModelName <your-openai-model-name>
101
+
```
132
102
133
103
:::zone-end
134
104
135
-
<!-- markdownlint-disable MD044 -->
136
-
:::zone target="docs" pivot="azure-openai"
137
-
<!-- markdownlint-enable MD044 -->
105
+
## Add the app code
138
106
139
-
## Explore the code
107
+
The app uses the [`Microsoft.Extensions.AI`](https://www.nuget.org/packages/Microsoft.Extensions.AI/) package to send and receive requests to the AI model and is designed to provide users with information about hiking trails.
140
108
141
-
The application uses the [`Microsoft.SemanticKernel`](https://www.nuget.org/packages/Microsoft.SemanticKernel) package to send and receive requests to an Azure OpenAI service deployed in Azure.
109
+
1. In the **Program.cs** file, add the following code to connect and authenticate to the AI model.
142
110
143
-
The entire application is contained within the **Program.cs** file. The first several lines of code retrieve the secrets and configuration values that were setin the `dotnet user-secrets`for you during the application provisioning.
111
+
:::zone target="docs" pivot="azure-openai"
144
112
145
-
```csharp
146
-
// Retrieve the local secrets saved during the Azure deployment
147
-
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
The `AzureOpenAIChatCompletionService` service facilitates the requests and responses.
115
+
> [!NOTE]
116
+
> <xref:Azure.Identity.DefaultAzureCredential> searches for authentication credentials from your local tooling. If you aren't using the `azd` template to provision the Azure OpenAI resource, you'll need to assign the `Azure AI Developer` role to the account you used to sign in to Visual Studio or the Azure CLI. For more information, see [Authenticate to Azure AI services with .NET](../azure-ai-services-authentication.md).
153
117
154
-
```csharp
155
-
// Create the Azure OpenAI Chat Completion Service
156
-
AzureOpenAIChatCompletionService service = new(deployment, endpoint, new DefaultAzureCredential());
157
-
```
118
+
:::zone-end
158
119
159
-
:::zone-end
120
+
:::zone target="docs" pivot="openai"
160
121
161
-
Add a system prompt to provide more context to the model, which influences model behavior and the generated completions during the conversation.
Add a user message to the chat history using the `AddUserMessage` function. Use the `GetChatMessageContentAsync`functiontoinstruct the model to generate a response based off the system prompt and the user request.
130
+
1. Create a conversational loop that accepts an input prompt from the user, sends the prompt to the model, and prints the response completion:
181
131
182
-
```csharp
183
-
184
-
// Add user message to chat history
185
-
chatHistory.AddUserMessage("Hi! Apparently you can help me find a hike that I will like?");
Customize the system prompt and user message to see how the model responds to help you find a hike that you'll like.
140
+
The app prints out the completion response from the AI model. Send additional follow up prompts and ask other questions to experiment with the AI chat functionality.
206
141
207
-
<!-- markdownlint-disable MD044 -->
208
142
:::zone target="docs" pivot="azure-openai"
209
-
<!-- markdownlint-enable MD044 -->
143
+
210
144
## Clean up resources
211
145
212
-
Remove the corresponding deployment and all resources when you no longer need the sample application or resources.
146
+
When you no longer need the sample application or resources, remove the corresponding deployment and all resources.
Copy file name to clipboardExpand all lines: docs/ai/quickstarts/includes/clone-sample-repo.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ ms.topic: include
7
7
8
8
## Clone the sample repository
9
9
10
-
You can create your own app and follow along the steps in the sections ahead, or you can clone the GitHub repository that contains the completed sample apps for all of the quickstarts. The sample repo is also structured as an Azure Developer CLI template that can provision an Azure OpenAI resource for you.
10
+
You can create your own app using the steps in the sections ahead, or you can clone the GitHub repository that contains the completed sample apps for all of the quickstarts. If you plan to use Azure OpenAI, the sample repo is also structured as an Azure Developer CLI template that can provision an Azure OpenAI resource for you.
0 commit comments