Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nåDenne nettleseren støttes ikke lenger.
Oppgrader til Microsoft Edge for å dra nytte av de nyeste funksjonene, sikkerhetsoppdateringene og den nyeste tekniske støtten.
In this quickstart, you learn how to create a .NET console chat app to connect to and prompt an OpenAI or Azure OpenAI model. The app uses the 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.
Obs!
The Microsoft.Extensions.AI
library is currently in Preview.
Obs!
You can also use Semantic Kernel to accomplish the tasks in this article. Semantic Kernel is a lightweight, open-source SDK that lets you build AI agents and integrate the latest AI models into your .NET apps.
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.
git clone https://github.com/dotnet/ai-samples.git
Complete the following steps to create a .NET console app to connect to an AI model.
In an empty directory on your computer, use the dotnet new
command to create a new console app:
dotnet new console -o ExtensionsAI
Change directory into the app folder:
cd ExtensionsAI
Install the required packages:
dotnet add package Azure.Identity
dotnet add package Azure.AI.OpenAI
dotnet add package Microsoft.Extensions.AI.OpenAI
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.UserSecrets
dotnet add package OpenAI
dotnet add package Microsoft.Extensions.AI.OpenAI
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.UserSecrets
Open the app in Visual Studio code or your editor of choice
code .
The sample GitHub repository is structured as an Azure Developer CLI (azd
) template, which azd
can use to provision the Azure OpenAI service and model for you.
From a terminal or command prompt, navigate to the src\quickstarts\azure-openai
directory of the sample repo.
Run the azd up
command to provision the Azure OpenAI resources. It might take several minutes to create the Azure OpenAI service and deploy the model.
azd up
azd
also configures the required user secrets for the sample app, such as the Azure OpenAI endpoint and model name.
Navigate to the root of your .NET project from a terminal or command prompt.
Run the following commands to configure your OpenAI API key as a secret for the sample app:
dotnet user-secrets init
dotnet user-secrets set OpenAIKey <your-openai-key>
dotnet user-secrets set ModelName <your-openai-model-name>
The app uses the Microsoft.Extensions.AI
package to send and receive requests to the AI model.
In the Program.cs file, add the following code to connect and authenticate to the AI model.
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.AI;
using Azure.AI.OpenAI;
using Azure.Identity;
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string endpoint = config["AZURE_OPENAI_ENDPOINT"];
string deployment = config["AZURE_OPENAI_GPT_NAME"];
IChatClient client =
new AzureOpenAIClient(new Uri(endpoint), new DefaultAzureCredential())
.AsChatClient(deployment);
Obs!
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.
using Microsoft.Extensions.AI;
using Microsoft.Extensions.Configuration;
using OpenAI;
IConfigurationRoot config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
string? model = config["ModelName"];
string? key = config["OpenAIKey"];
// Create the IChatClient
IChatClient client =
new OpenAIClient(key).AsChatClient(model);
Read the benefits.md file content and use it to create a prompt for the model. The prompt instructs the model to summarize the file text content.
// Create and print out the prompt
string prompt = $"""
summarize the the following text in 20 words or less:
{File.ReadAllText("benefits.md")}
""";
Console.WriteLine($"user >>> {prompt}");
Call the InvokePromptAsync
function to send the prompt to the model to generate a response.
// Submit the prompt and print out the response
ChatResponse response = await client.GetResponseAsync(prompt, new ChatOptions { MaxOutputTokens = 400 });
Console.WriteLine($"assistant >>> {response}");
Use the dotnet run
command to run the app:
dotnet run
The app prints out the completion response from the AI model. Customize the text content of the benefits.md
file or the length of the summary to see the differences in the responses.
When you no longer need the sample application or resources, remove the corresponding deployment and all resources.
azd down
.NET-tilbakemelding
.NET er et åpen kilde-prosjekt. Velg en kobling for å gi tilbakemelding:
Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nåOpplæring
Modul
Generer tekst og samtaler med .NET og Azure OpenAI Completions - Training
Lær hvordan du bruker .NET SDK med Azure OpenAI-tjenesten for å få programmene til å fortsette samtaler med brukere på naturlig språk.
Sertifisering
Microsoft Certified: Grunnleggende om Azure AI - Certifications
Demonstrere grunnleggende AI-konsepter knyttet til utvikling av programvare og tjenester i Microsoft Azure for å opprette AI-løsninger.
Dokumentasjon
Quickstart - Build a minimal .NET AI RAG app - .NET
Create an AI powered app to search and integrate with vector stores using embeddings and the Microsoft.Extensions.VectorData package for .NET
Quickstart - Generate images using AI with .NET - .NET
Create a simple app using to generate images using .NET and the OpenAI or Azure OpenAI models.
Quickstart - Build an AI chat app with .NET - .NET
Create a simple AI powered chat app using Semantic Kernel SDK for .NET and the OpenAI or Azure OpenAI SDKs