Skip to content

Commit c85c4d1

Browse files
Migrate quickstarts to Extensions AI library (#43673)
* Migrate quickstarts to Extensions AI library --------- Co-authored-by: Genevieve Warren <[email protected]>
1 parent eb1812f commit c85c4d1

File tree

13 files changed

+399
-297
lines changed

13 files changed

+399
-297
lines changed

docs/ai/quickstarts/get-started-openai.md

Lines changed: 69 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -12,214 +12,146 @@ zone_pivot_groups: openai-library
1212

1313
# Build an AI chat app with .NET
1414

15-
<!-- markdownlint-disable MD044 -->
16-
:::zone target="docs" pivot="openai"
17-
<!-- markdownlint-enable MD044 -->
15+
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.
1819
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.
20+
:::zone target="docs" pivot="openai"
2021

21-
[!INCLUDE [download-alert](includes/prerequisites-openai.md)]
22+
[!INCLUDE [openai-prereqs](includes/prerequisites-openai.md)]
2223

2324
:::zone-end
2425

25-
<!-- markdownlint-disable MD044 -->
2626
:::zone target="docs" pivot="azure-openai"
27-
<!-- markdownlint-enable MD044 -->
2827

29-
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.
30-
31-
[!INCLUDE [download-alert](includes/prerequisites-azure-openai.md)]
28+
[!INCLUDE [azure-openai-prereqs](includes/prerequisites-azure-openai.md)]
3229

3330
:::zone-end
3431

35-
## Get the sample project
32+
[!INCLUDE [semantic-kernel](../../azure/includes/semantic-kernel.md)]
3633

3734
[!INCLUDE [clone-sample-repo](includes/clone-sample-repo.md)]
3835

39-
:::zone target="docs" pivot="azure-openai"
36+
## Create the app
4037

41-
## Create the Azure OpenAI service
38+
Complete the following steps to create a .NET console app to connect to an AI model.
4239

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:
4441

45-
[!INCLUDE [deploy-azd](includes/deploy-azd.md)]
42+
```dotnetcli
43+
dotnet new console -o ChatAppAI
44+
```
4645
47-
# [Azure CLI](#tab/azure-cli)
46+
1. Change directory into the app folder:
4847
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+
```
5051
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:
5253
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"
5455
5556
```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.
57+
dotnet add package Azure.Identity
58+
dotnet add package Azure.AI.OpenAI
59+
dotnet add package Microsoft.Extensions.AI.OpenAI
60+
dotnet add package Microsoft.Extensions.Configuration
61+
dotnet add package Microsoft.Extensions.Configuration.UserSecrets
62+
```
6263
63-
1. From a terminal or command prompt, navigate to the `src\quickstarts\azure-openai\semantic-kernel\02-HikerAI` directory.
64+
:::zone-end
6465
65-
1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
66+
:::zone target="docs" pivot="openai"
6667
6768
```bash
68-
dotnet user-secrets init
69-
dotnet user-secrets set OpenAIKey <your-openai-key>
70-
71-
---
72-
73-
:::zone-end
74-
75-
## Try the HikerAI sample
76-
77-
<!-- markdownlint-disable MD029 MD044 -->
78-
:::zone target="docs" pivot="openai"
69+
dotnet add package OpenAI
70+
dotnet add package Microsoft.Extensions.AI.OpenAI
71+
dotnet add package Microsoft.Extensions.Configuration
72+
dotnet add package Microsoft.Extensions.Configuration.UserSecrets
73+
```
7974
80-
1. From a terminal or command prompt, navigate to the `src\quickstarts\azure-openai\semantic-kernel\02-HikerAI` directory.
75+
:::zone-end
8176
82-
1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
77+
1. Open the app in Visual Studio Code (or your editor of choice).
8378
8479
```bash
85-
dotnet user-secrets init
86-
dotnet user-secrets set OpenAIKey <your-openai-key>
87-
```
88-
89-
1. Use the `dotnet run` command to run the app:
90-
91-
```dotnetcli
92-
dotnet run
80+
code .
9381
```
9482
95-
:::zone-end
96-
9783
:::zone target="docs" pivot="azure-openai"
9884
99-
1. From a terminal or command prompt, navigate to the `semantic-kernel\02-HikerAI` directory.
100-
101-
2. Use the `dotnet run` command to run the app:
102-
103-
```dotnetcli
104-
dotnet run
105-
```
106-
107-
> [!TIP]
108-
> If you get an error message, the Azure OpenAI resources might not have finished deploying. Wait a couple of minutes and try again.
85+
[!INCLUDE [create-ai-service](includes/create-ai-service.md)]
10986
11087
:::zone-end
11188
11289
:::zone target="docs" pivot="openai"
11390
114-
## Explore the code
115-
116-
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
11792
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.
11994
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:
12796
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+
```
132102
133103
:::zone-end
134104
135-
<!-- markdownlint-disable MD044 -->
136-
:::zone target="docs" pivot="azure-openai"
137-
<!-- markdownlint-enable MD044 -->
105+
## Add the app code
138106
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.
140108
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.
142110
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 set in the `dotnet user-secrets` for you during the application provisioning.
111+
:::zone target="docs" pivot="azure-openai"
144112
145-
```csharp
146-
// Retrieve the local secrets saved during the Azure deployment
147-
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
148-
string endpoint = config["AZURE_OPENAI_ENDPOINT"];
149-
string deployment = config["AZURE_OPENAI_GPT_NAME"];
150-
```
113+
:::code language="csharp" source="snippets/build-chat-app/azure-openai/program.cs" range="1-12":::
151114
152-
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).
153117
154-
```csharp
155-
// Create the Azure OpenAI Chat Completion Service
156-
AzureOpenAIChatCompletionService service = new(deployment, endpoint, new DefaultAzureCredential());
157-
```
118+
:::zone-end
158119
159-
:::zone-end
120+
:::zone target="docs" pivot="openai"
160121
161-
Add a system prompt to provide more context to the model, which influences model behavior and the generated completions during the conversation.
122+
:::code language="csharp" source="snippets/build-chat-app/openai/program.cs" range="1-11":::
162123
163-
```csharp
164-
// Start the conversation with context for the AI model
165-
ChatHistory chatHistory = new("""
166-
You are a hiking enthusiast who helps people discover fun hikes in their area.
167-
You are upbeat and friendly. You introduce yourself when first saying hello.
168-
When helping people out, you always ask them for this information
169-
to inform the hiking recommendation you provide:
124+
:::zone-end
170125
171-
1. Where they are located
172-
2. What hiking intensity they are looking for
126+
1. Create a system prompt to provide the AI model with initial role context and instructions about hiking recommendations:
173127
174-
You will then provide three suggestions for nearby hikes that vary in length
175-
after you get that information. You will also share an interesting fact about
176-
the local nature on the hikes when making a recommendation.
177-
""");
178-
```
128+
:::code language="csharp" source="snippets/build-chat-app/openai/program.cs" range="13-30":::
179129
180-
Add a user message to the chat history using the `AddUserMessage` function. Use the `GetChatMessageContentAsync` function to instruct 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:
181131
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?");
186-
187-
// Print User Message to console
188-
Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
189-
190-
// Get response
191-
var response = await service.GetChatMessageContentAsync(
192-
chatHistory, new OpenAIPromptExecutionSettings() { MaxTokens = 400 });
193-
```
132+
:::code language="csharp" source="snippets/build-chat-app/openai/program.cs" range="32-51":::
194133
195-
Add the response from the mode to maintain the chat history.
196-
197-
```csharp
198-
// Add response to chat history
199-
chatHistory.Add(response);
134+
1. Use the `dotnet run` command to run the app:
200135
201-
// Print Response to console
202-
Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last().Content}");
203-
```
136+
```dotnetcli
137+
dotnet run
138+
```
204139
205-
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.
206141
207-
<!-- markdownlint-disable MD044 -->
208142
:::zone target="docs" pivot="azure-openai"
209-
<!-- markdownlint-enable MD044 -->
143+
210144
## Clean up resources
211145
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.
213147
214148
```azdeveloper
215149
azd down
216150
```
217151

218-
[!INCLUDE [troubleshoot](includes/troubleshoot.md)]
219-
220152
:::zone-end
221153

222154
## Next steps
223155

224-
- [Quickstart - Get insight about your data from .NET AI chat app](quickstart-ai-chat-with-data.md)
225-
- [Generate text and conversations with .NET and OpenAI Completions](/training/modules/open-ai-dotnet-text-completions/)
156+
- [Quickstart - Chat with a local AI model](quickstart-local-ai.md)
157+
- [Generate images using AI with .NET](quickstart-openai-generate-images.md)

docs/ai/quickstarts/includes/clone-sample-repo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ ms.topic: include
77

88
## Clone the sample repository
99

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.
1111

1212
```bash
1313
git clone https://github.com/dotnet/ai-samples.git

docs/ai/quickstarts/includes/create-ai-service.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
1. From a terminal or command prompt, navigate to the root of your project directory.
1212

13-
1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
13+
1. Run the following commands to configure your Azure OpenAI endpoint and model name for the sample app:
1414

1515
```bash
1616
dotnet user-secrets init
@@ -24,7 +24,7 @@
2424

2525
1. From a terminal or command prompt, navigate to the root of your project directory.
2626

27-
1. Run the following commands to configure your OpenAI API key as a secret for the sample app:
27+
1. Run the following commands to configure your Azure OpenAI endpoint and model name for the sample app:
2828

2929
```bash
3030
dotnet user-secrets init

0 commit comments

Comments
 (0)