框架將聊天客戶端包裹在支架上,讓客服人員能完成漫長且多步驟的任務——規劃/執行模式、待辦清單、情境壓縮、檔案記憶體、檔案存取,以及「別再問」工具核准。 你不是自己組裝那些零件,而是自己製作一個安全帶代理,然後從盒子裡拿出來。
使用 IChatClient 擴充方法從任何 AsHarnessAgent 建立 harness agent。 因為 harness 會透過多個步驟以互動方式執行任務,所以通常會透過對話迴圈來驅動它:保留一個 AgentSession,讓 harness 的狀態(計畫、待辦事項和歷史)能在多輪對話間持續保存,讀取使用者的下一個指示,並在代理程式產生輸出時以串流方式傳送。
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 線束範例。
使用 create_harness_agent factory 建立一個 Harness 代理程式。 因為 harness 會以互動方式在多個步驟中推進任務,因此通常會透過對話迴圈來驅動它:保留同一個工作階段,讓 harness 的狀態(計畫、待辦事項和歷史)能跨回合持續保留,讀取使用者的下一個指示,並在代理產生輸出時即時串流傳送。
from agent_framework import create_harness_agent
from agent_framework.openai import OpenAIChatClient
agent = create_harness_agent(
OpenAIChatClient(model="gpt-4o"),
)
# A session carries the harness state (plan, todos, history) across turns.
session = agent.create_session()
print("Harness agent ready. Type 'exit' to quit.")
while True:
user_input = input("> ")
if user_input.strip().lower() in {"exit", "quit"}:
break
# Stream this turn's output as the harness plans and works through the request.
async for chunk in agent.run(user_input, session=session, stream=True):
if chunk.text:
print(chunk.text, end="", flush=True)
print()
這個背帶負責整個對話的規劃、待辦事項追蹤和歷史紀錄的保存。 如需功能完整的主控台——包含工具核准提示訊息、待辦事項/模式顯示,以及斜線指令——請參閱 終端機 UX 範例。
Tip
完整可執行應用程式請參閱 Python 線束範例。
Note
Go的特工背帶支援即將推出。 最新狀態請參閱 Agent Framework Go 倉庫 。
下一步
深入探討: