工具包为聊天客户端提供了一套框架,其中包含智能体执行冗长多步骤任务所需的各项功能,包括规划/执行模式、用于制定计划的待办事项列表、上下文压缩、文件内存、文件访问,以及不再询问工具审批。 与其自己组装这些部件,不如创建一个工具包智能体,即拿即用。
使用 AsHarnessAgent 扩展方法从任意 IChatClient 创建工具包智能体。 由于工具包是通过交互式方式分多步完成任务的,因此通常需要通过对话循环来驱动它:保留一个 AgentSession,以便工具包的状态(计划、待办事项和历史记录)在各回合间得以保留,读取用户的下一条指令,并在智能体生成输出时实时流式处理其结果。
using System;
using Microsoft.Agents.AI;
using Microsoft.Extensions.AI;
// chatClient is any IChatClient implementation (Foundry, Azure OpenAI, OpenAI, Anthropic, ...).
AIAgent agent = chatClient.AsHarnessAgent();
// A session carries the harness state (plan, todos, history) across turns.
AgentSession session = await agent.CreateSessionAsync();
Console.WriteLine("Harness agent ready. Type 'exit' to quit.");
while (true)
{
Console.Write("> ");
string? input = Console.ReadLine();
if (string.IsNullOrWhiteSpace(input) || input.Equals("exit", StringComparison.OrdinalIgnoreCase))
{
break;
}
// Stream this turn's output as the harness plans and works through the request.
await foreach (var update in agent.RunStreamingAsync(input, session))
{
Console.Write(update);
}
Console.WriteLine();
}
工具包会在整个对话过程中为您制定计划、跟踪待办事项以及保存历史记录。 有关功能齐全的控制台(包含工具审批提示、待办事项/模式呈现和斜杠命令),请参阅 示例终端 UX。
Tip
有关可完整运行的应用程序,请参阅 .NET harness 示例。
完整示例创建一个 Microsoft Foundry 聊天客户端,用create_harness_agent对其进行包装,并在两个轮次之间重复使用一个会话。 该框架增加了规划、待办事项跟踪和压缩功能,而示例则禁用了文件记忆和 Web 搜索以保持专注。
import asyncio
from agent_framework import create_harness_agent
from agent_framework.foundry import FoundryChatClient
from azure.identity import AzureCliCredential
async def main() -> None:
agent = create_harness_agent(
client=FoundryChatClient(
project_endpoint="https://your-account.services.ai.azure.com/api/projects/your-project",
model="gpt-6-luna",
credential=AzureCliCredential(),
),
agent_instructions="Help users plan and complete multi-step tasks.",
disable_file_memory=True,
disable_web_search=True,
)
session = agent.create_session()
print(await agent.run("Plan a weekend trip to Seattle.", session=session))
print(await agent.run("Turn that plan into a checklist.", session=session))
if __name__ == "__main__":
asyncio.run(main())
共享会话在两次调用中保留测试框架状态。 有关功能齐全的控制台(包含工具审批提示、to-do 和模式显示以及斜杠命令),请参阅 示例终端 UX。
Tip
请参阅 完整的入门示例。 有关更多模式,请参阅Python harness 示例。
注释
Go 对智能体工具包的支持即将推出。 有关最新状态,请参阅 Agent Framework Go 存储库 。
后续步骤
更深入: