Sending a single prompt to an AI model is useful for quick answers, but real applications often need an agent that follows persistent system instructions, invokes C# functions when needed, and formats helpful replies. In .NET 11, the Microsoft Agent Framework (Microsoft.Agents.AI) builds directly on IChatClient so you can wrap a model and your C# tools into one clean AIAgent.
Student Question → ChatClientAgent (Instructions + Model) → AIFunctionFactory Tool Call → Final Answer
1. Install the Packages
Create a new console application and add the Microsoft Agent Framework and OpenAI adapter packages:
dotnet new console -n StudyAgentDemo
cd StudyAgentDemo
dotnet add package Microsoft.Agents.AI --prerelease
dotnet add package OpenAI --prerelease
dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease
2. Create a Study Assistant with ChatClientAgent
Set your OPENAI_API_KEY environment variable and update Program.cs with an AIAgent backed by ChatClientAgent:
using System.ClientModel;
using System.ComponentModel;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
using OpenAI;
string apiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY")
?? throw new InvalidOperationException("Set OPENAI_API_KEY first.");
IChatClient chatClient = new OpenAIClient(new ApiKeyCredential(apiKey))
.GetChatClient("gpt-4o-mini")
.AsIChatClient();
[Description("Look up upcoming homework due dates for a subject.")]
static string GetAssignmentDeadline(
[Description("Course subject such as CSharp or SQL")] string subject)
{
return subject.ToUpperInvariant() switch
{
"CSHARP" => "LINQ Lab 4 is due Friday at 6:00 PM.",
"SQL" => "Joins Worksheet is due Monday at 5:00 PM.",
_ => "No upcoming deadline found for that subject."
};
}
AIAgent agent = new ChatClientAgent(
chatClient: chatClient,
instructions: "You are a concise study assistant. Always check assignment deadlines with tools before answering.",
name: "StudyAssistant",
tools: [AIFunctionFactory.Create(GetAssignmentDeadline)]
);
var response = await agent.RunAsync("When is my SQL assignment due?");
Console.WriteLine(response);
Understand the Key Parts
IChatClientconnects your agent to the model provider while keeping the rest of your code portable.ChatClientAgentimplementsAIAgentand packages your model, system instructions, agent name, and tool list into one object.agent.RunAsync(...)automatically runs the prompt, invokesGetAssignmentDeadlineif needed, and returns the final answer (or useRunStreamingAsyncfor live streaming).
Practice
- Add a second tool named
GetOfficeHours(string subject)and pass it inside thetoolsarray. - Replace
agent.RunAsync(...)withawait foreach (var update in agent.RunStreamingAsync(...))to stream output word by word. - Ask a question combining two subjects in one sentence to see how the agent calls your tool multiple times.
Continue learning: .NET Core and ASP.NET Core: Student Learning Path.
References: AI for .NET Developers (Microsoft Learn).
No comments:
Post a Comment
Please do not enter any spam link in the comment box.