跳到正文

02 · 02-dotnet-agent-framework

返回:探索 Agent 框架 · 不可变原始文件

原课程补充材料

根据对应简体中文译本整理。原代码片段未进行云端验证。

🔍 探索 Microsoft Agent Framework - 基础 Agent (.NET)

📋 学习目标

本示例通过在 .NET 中实现基础 Agent,探索 Microsoft Agent Framework 的基本概念。你将学习核心 Agent 模式,并通过 C# 和 .NET 生态系统了解智能 Agent 的运行原理。

你将发现

  • 🏗️ Agent 架构 :理解 .NET 中人工智能 Agent 的基本结构
  • 🛠️ 工具集成 :Agent 如何使用外部函数扩展功能
  • 💬 对话流程 :通过线程管理管理多轮对话与上下文
  • 🔧 配置模式 :.NET 中 Agent 设置和管理的最佳实践

🎯 涉及的关键概念

Agent 框架原则

  • 自治性 :Agent 如何使用 .NET AI 抽象独立决策
  • 响应性 :响应环境变化和用户输入
  • 主动性 :基于目标和上下文主动采取行动
  • 社交能力 :通过自然语言和对话线程进行交互

技术组件

  • AIAgent :核心 Agent 协调和对话管理 (.NET)
  • 工具函数 :通过 C# 方法和特性扩展 Agent 功能
  • Azure OpenAI 集成 :利用 Azure OpenAI Responses API 调用语言模型
  • 安全配置 :基于环境的端点管理

🔧 技术栈

核心技术

  • Microsoft Agent Framework (.NET)
  • Azure OpenAI(Responses API)集成
  • Azure.AI.OpenAI 客户端模式
  • 使用 DotNetEnv 进行基于环境的配置

Agent 能力

  • 自然语言理解与生成
  • 使用 C# 特性进行函数调用和工具使用
  • 具备上下文感知的对话会话响应
  • 通过依赖注入模式实现可扩展架构

📚 框架比较

本示例演示 Microsoft Agent Framework 方法与其他 Agent 框架的对比:

特性Microsoft Agent Framework其他框架
集成原生微软生态系统兼容性多样
简洁性清晰直观的 API通常设置复杂
可扩展性轻松集成工具依赖于框架
企业级面向生产环境构建取决于框架

🚀 入门

前置条件

所需环境变量

bash
# zsh/bash
export AZURE_OPENAI_ENDPOINT=https://<your-resource>.openai.azure.com
export AZURE_OPENAI_DEPLOYMENT=gpt-5-mini
# 然后登录,以便 AzureCliCredential 能获取令牌
az login
powershell
# PowerShell
$env:AZURE_OPENAI_ENDPOINT = "https://<your-resource>.openai.azure.com"
$env:AZURE_OPENAI_DEPLOYMENT = "gpt-5-mini"
# 然后登录,以便 AzureCliCredential 可以获取令牌
az login

示例代码

运行代码示例,

bash
# zsh/bash
chmod +x ./02-dotnet-agent-framework.cs
./02-dotnet-agent-framework.cs

或使用 dotnet CLI:

bash
dotnet run ./02-dotnet-agent-framework.cs

完整代码请参见 02-dotnet-agent-framework.cs

csharp
#!/usr/bin/dotnet run

#:package Microsoft.Extensions.AI@10.*
#:package Microsoft.Agents.AI.OpenAI@1.*-*
#:package Azure.AI.OpenAI@2.1.0
#:package Azure.Identity@1.13.1

using System.ComponentModel;

using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;

using Azure.AI.OpenAI;
using Azure.Identity;

// Tool Function: Random Destination Generator
// This static method will be available to the agent as a callable tool
// The [Description] attribute helps the AI understand when to use this function
// This demonstrates how to create custom tools for AI agents
[Description("Provides a random vacation destination.")]
static string GetRandomDestination()
{
    // List of popular vacation destinations around the world
    // The agent will randomly select from these options
    var destinations = new List<string>
    {
        "Paris, France",
        "Tokyo, Japan",
        "New York City, USA",
        "Sydney, Australia",
        "Rome, Italy",
        "Barcelona, Spain",
        "Cape Town, South Africa",
        "Rio de Janeiro, Brazil",
        "Bangkok, Thailand",
        "Vancouver, Canada"
    };

    // Generate random index and return selected destination
    // Uses System.Random for simple random selection
    var random = new Random();
    int index = random.Next(destinations.Count);
    return destinations[index];
}

// Azure OpenAI with the Responses API (stable v1 endpoint). Sign in with `az login`.
var azureEndpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT")
    ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set.");
var deployment = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT") ?? "gpt-5-mini";

var azureClient = new AzureOpenAIClient(new Uri(azureEndpoint), new AzureCliCredential());

// Define Agent Identity and Comprehensive Instructions
// Agent name for identification and logging purposes
var AGENT_NAME = "TravelAgent";

// Detailed instructions that define the agent's personality, capabilities, and behavior
// This system prompt shapes how the agent responds and interacts with users
var AGENT_INSTRUCTIONS = """
You are a helpful AI Agent that can help plan vacations for customers.

Important: When users specify a destination, always plan for that location. Only suggest random destinations when the user hasn't specified a preference.

When the conversation begins, introduce yourself with this message:
"Hello! I'm your TravelAgent assistant. I can help plan vacations and suggest interesting destinations for you. Here are some things you can ask me:
1. Plan a day trip to a specific location
2. Suggest a random vacation destination
3. Find destinations with specific features (beaches, mountains, historical sites, etc.)
4. Plan an alternative trip if you don't like my first suggestion

What kind of trip would you like me to help you plan today?"

Always prioritize user preferences. If they mention a specific destination like "Bali" or "Paris," focus your planning on that location rather than suggesting alternatives.
""";

// Create AI Agent with Advanced Travel Planning Capabilities
// Get the Responses client for the deployment and create the AI agent
// Configure agent with name, detailed instructions, and available tools
// This demonstrates the .NET agent creation pattern with full configuration
AIAgent agent = azureClient
    .GetChatClient(deployment)
    .AsAIAgent(
        name: AGENT_NAME,
        instructions: AGENT_INSTRUCTIONS,
        tools: [AIFunctionFactory.Create(GetRandomDestination)]
    );

// Create New Session for Context Management.
// Initialize a new conversation session to maintain context across multiple interactions
// Sessions enable the agent to remember previous exchanges and maintain conversational state
// This is essential for multi-turn conversations and contextual understanding
AgentSession session = await agent.CreateSessionAsync();

// Execute Agent: First Travel Planning Request
// Run the agent with an initial request that will likely trigger the random destination tool
// The agent will analyze the request, use the GetRandomDestination tool, and create an itinerary
// Using the session parameter maintains conversation context for subsequent interactions
await foreach (var update in agent.RunStreamingAsync("Plan me a day trip", session))
{
    await Task.Delay(10);
    Console.Write(update);
}

Console.WriteLine();

// Execute Agent: Follow-up Request with Context Awareness
// Demonstrate contextual conversation by referencing the previous response
// The agent remembers the previous destination suggestion and will provide an alternative
// This showcases the power of conversation sessions and contextual understanding in .NET agents
await foreach (var update in agent.RunStreamingAsync("I don't like that destination. Plan me another vacation.", session))
{
    await Task.Delay(10);
    Console.Write(update);
}

🎓 关键要点

  1. Agent 架构 :Microsoft Agent Framework 提供用于在 .NET 中构建 AI Agent 的清晰、类型安全的方法
  2. 工具集成 :带有 [Description] 特性的函数成为 Agent 可用的工具
  3. 对话上下文 :会话管理支持多轮对话的完整上下文感知
  4. 配置管理 :环境变量和安全凭证处理遵循 .NET 最佳实践
  5. Azure OpenAI Responses API :Agent 通过 Azure.AI.OpenAI SDK 使用 Azure OpenAI Responses API

🔗 其他资源


基于 Microsoft AI Agents for Beginners · 非官方中文学习版

100%