Подія
17 бер., 21 - 21 бер., 10
Приєднайтеся до серії нарад, щоб створити масштабовані рішення зі ШІ на основі реальних випадків використання з колегами-розробниками та експертами.
Зареєструватися заразЦей браузер більше не підтримується.
Замініть його на Microsoft Edge, щоб користуватися перевагами найновіших функцій, оновлень безпеки та технічної підтримки.
In this quickstart, you create a .NET console AI chat app to connect to an AI model with local function calling enabled. 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.
Примітка
The Microsoft.Extensions.AI
library is currently in Preview.
Примітка
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 FunctionCallingAI
Change directory into the app folder:
cd FunctionCallingAI
Install the required packages:
dotnet add package Azure.Identity
dotnet add package Azure.AI.OpenAI
dotnet add package Microsoft.Extensions.AI
dotnet add package Microsoft.Extensions.AI.OpenAI
dotnet add package Microsoft.Extensions.Configuration
dotnet add package Microsoft.Extensions.Configuration.UserSecrets
dotnet add package Microsoft.Extensions.AI
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. The ChatClient
is also configured to use function invocation, which allows .NET functions in your code to be called by 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 ChatClientBuilder()
.UseFunctionInvocation()
.Use(
new AzureOpenAIClient(new Uri(endpoint),
new DefaultAzureCredential())
.AsChatClient(deployment));
Примітка
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"];
IChatClient client =
new ChatClientBuilder(new OpenAIClient(key).AsChatClient(model ?? "gpt-4o"))
.UseFunctionInvocation()
.Build();
// Add a new plugin with a local .NET function
Create a new ChatOptions
object that contains an inline function the AI model can call to get the current weather. The function declaration includes a delegate to run logic and name and description parameters to describe the purpose of the function to the AI model.
var chatOptions = new ChatOptions
{
Tools = [AIFunctionFactory.Create((string location, string unit) =>
{
// Here you would call a weather API to get the weather for the location
return "Periods of rain or drizzle, 15 C";
},
"get_current_weather",
"Get the current weather in a given location")]
};
Add a system prompt to the chatHistory
to provide context and instructions to the model. Send a user prompt with a question that requires the AI model to call the registered function to properly answer the question.
List<ChatMessage> chatHistory = [new(ChatRole.System, """
You are a hiking enthusiast who helps people discover fun hikes in their area. You are upbeat and friendly.
""")];
// Weather conversation relevant to the registered function.
chatHistory.Add(new ChatMessage(ChatRole.User,
"I live in Montreal and I'm looking for a moderate intensity hike. What's the current weather like? "));
Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
ChatResponse response = await client.GetResponseAsync(chatHistory, chatOptions);
chatHistory.Add(new ChatMessage(ChatRole.Assistant, response.Message.Contents));
Console.WriteLine($"{chatHistory.Last().Role} >>> {chatHistory.Last()}");
Use the dotnet run
command to run the app:
dotnet run
The app prints a the completion response from the AI model that includes data provided by the .NET function. The AI model understood the registered function was available and called it automatically to generate a proper response.
When you no longer need the sample application or resources, remove the corresponding deployment and all resources.
azd down
Відгук про .NET
.NET – це проект із відкритим кодом. Виберіть посилання, щоб надати відгук:
Подія
17 бер., 21 - 21 бер., 10
Приєднайтеся до серії нарад, щоб створити масштабовані рішення зі ШІ на основі реальних випадків використання з колегами-розробниками та експертами.
Зареєструватися заразНавчання
Модуль
Автоматичні виклики функцій - Training
Дізнайтеся, як автоматично викликати функції за допомогою SDK семантичного ядра.
Сертифікація
Microsoft Certified: Azure AI Fundamentals - Certifications
Демонстрація основних концепцій AI, пов'язаних із розробкою програмного забезпечення та служб Microsoft Azure для створення рішень зі ШІ.