當你建立好代理後,你需要架設它,讓使用者和其他代理能夠互動。
主機選項
| 選項 | Description | 適用對象 |
|---|---|---|
| A2A 協議 | 透過代理對代理協定(Agent-to-Agent)暴露代理 | 多重代理程式系統 |
| OpenAI-Compatible 端點 | 透過聊天完成或回應 API 來暴露代理 | OpenAI 相容用戶端 |
| 耐用延長件 | 讓 C# 和 Python 代理程式及工作流程在 Azure Functions 或自架運算上都具備耐用性 | 長時間運行且可靠的工作負載 |
| AG-UI 協議 | 建立基於網頁的 AI 代理應用程式 | 網頁前端 |
ASP.NET Core 託管
代理框架提供託管函式庫,讓你能將 AI 代理整合進 ASP.NET 核心應用程式。 這些函式庫簡化了透過各種協定註冊、設定及暴露代理程式的過程。
如同《 代理概述》所述, AIAgent 是代理框架的基本概念。 它定義了一個「LLM 包裝器」,負責處理使用者輸入、做出決策、呼叫工具,並執行額外工作以執行動作與產生回應。 從你的 ASP.NET Core 應用程式中公開 AI 代理並非易事。 託管函式庫透過在相依注入容器中註冊 AI 代理來解決這個問題,讓你能在應用服務中解析並使用它們。 它們還使您可以在同一容器中管理代理相依性,例如工具和會話儲存空間。 代理可以與你的應用程式基礎設施並存,獨立於它們所使用的協定。 同樣地,工作流程也可以被託管,並利用你應用程式的共用基礎設施。
核心託管函式庫
該 Microsoft.Agents.AI.Hosting 函式庫是 ASP.NET Core 中 AI 代理的基礎。
IHostApplicationBuilder 提供擴充功能以註冊和配置 AI 代理和工作流程。 在 ASP.NET Core 中, IHostApplicationBuilder 是代表託管應用程式與服務建構者的基本類型,管理設定、日誌記錄、生命週期等。
在設定代理或工作流程前,先在相依注入容器中註冊一個 IChatClient。 在以下範例中,它以鍵控單例形式註冊,名稱為 chat-model:
// endpoint is your Microsoft Foundry project endpoint
// deploymentName is 'gpt-4o-mini' for example
IChatClient chatClient = new AIProjectClient(
new Uri(endpoint),
new DefaultAzureCredential())
.GetProjectOpenAIClient()
.GetProjectResponsesClient()
.AsIChatClient(deploymentName);
builder.Services.AddSingleton(chatClient);
警告
DefaultAzureCredential 開發方便,但在生產過程中需謹慎考量。 在生產環境中,建議使用特定的憑證(例如 ManagedIdentityCredential),以避免延遲問題、意外的憑證探測,以及備援機制帶來的安全風險。
AddAIAgent
註冊一個帶有依賴注入的 AI 代理:
var pirateAgent = builder.AddAIAgent(
"pirate",
instructions: "You are a pirate. Speak like a pirate",
description: "An agent that speaks like a pirate.",
chatClientServiceKey: "chat-model");
該 AddAIAgent() 方法回傳一個 IHostedAgentBuilder,該IHostedAgentBuilder提供擴充方法以設定代理。 例如,你可以在代理中新增工具:
var pirateAgent = builder.AddAIAgent("pirate", instructions: "You are a pirate. Speak like a pirate")
.WithAITool(new MyTool()); // MyTool is a custom type derived from AITool
你也可以設定會話儲存(用於對話資料的儲存):
var pirateAgent = builder.AddAIAgent("pirate", instructions: "You are a pirate. Speak like a pirate")
.WithInMemorySessionStore();
AddWorkflow
登錄協調多個客服人員的工作流程。 工作流程本質上是一個「圖」,每個節點都是 AIAgent,代理人彼此通訊。
在這個例子中,兩個代理人依序工作。 使用者輸入首先被送至 agent-1,該回應產生並傳送至 agent-2。 工作流程接著輸出最終回應。 也有一種 BuildConcurrent 方法可以建立並行的代理工作流程。
builder.AddAIAgent("agent-1", instructions: "you are agent 1!");
builder.AddAIAgent("agent-2", instructions: "you are agent 2!");
var workflow = builder.AddWorkflow("my-workflow", (sp, key) =>
{
var agent1 = sp.GetRequiredKeyedService<AIAgent>("agent-1");
var agent2 = sp.GetRequiredKeyedService<AIAgent>("agent-2");
return AgentWorkflowBuilder.BuildSequential(key, [agent1, agent2]);
});
以 AIAgent 形式公開工作流程
若要將協定整合(如 A2A 或 OpenAI)與工作流程結合,請將其轉換成獨立代理。 目前,工作流程本身尚未提供類似的整合能力,因此必須進行以下轉換步驟:
var workflowAsAgent = builder
.AddWorkflow("science-workflow", (sp, key) => { ... })
.AddAsAIAgent(); // Now the workflow can be used as an agent
實作細節
主機函式庫作為協定介面卡,橋接外部通訊協定與代理框架內部 AIAgent 實作。 當你使用主機整合函式庫時,函式庫會取得從依賴注入的註冊 AIAgent 資料,並用協定專用的中介層軟體包裝,以轉換進入的請求和發出的回應,然後呼叫 AIAgent 來處理請求。 此架構使您的代理實作不依賴任何特定的協定。
例如,使用 ASP.NET Core 主機函式庫搭配 A2A 協定轉接器:
// Register the agent
var pirateAgent = builder.AddAIAgent("pirate",
instructions: "You are a pirate. Speak like a pirate",
description: "An agent that speaks like a pirate.");
// Expose via a protocol (e.g. A2A)
builder.Services.AddA2AServer();
var app = builder.Build();
app.MapA2AServer();
app.Run();
小提示
請參閱 Durable Agents 範例 以獲取Azure Functions及自架範例。
Azure Functions 係一個自我管理嘅主機選項。 如需比較 Microsoft 管理的 Foundry Hosted Agents、自行託管,以及 Durable Azure Functions 工作負載,請參閱裝載 Agent Framework 應用程式。
安裝 Azure Functions 主機套件、Foundry 客戶端及 Azure 認證套件:
pip install agent-framework-azurefunctions agent-framework-foundry azure-identity
建立代理人:
使用AgentFunctionApp註冊代理人:
在本地執行 Azure Functions Core Tools:
az login
pip install -r requirements.txt
# Start Azurite and copy local.settings.json.template to local.settings.json first.
func start
接著呼叫:
curl -X POST http://localhost:7071/api/agents/Joker/run \
-H "Content-Type: text/plain" \
-d "Tell me a short joke about cloud computing."
小提示
請參閱 完整範例 以獲取完整可執行文件,並檢視 Azure Functions 託管範例 以了解更多模式。
使用 A2A 協定進行託管
Go 版本透過 a2aprovider 提供 A2A 託管功能,將代理封裝為與 Agent-to-Agent 通訊協定相容的 HTTP 處理常式。
Note
目前 Go 還沒有 Durable Extension 主機。 欲了解最新的 Go SDK 狀態,請參閱 Agent Framework Go 倉庫。
建立代理人:
import (
"github.com/microsoft/agent-framework-go/agent"
"github.com/microsoft/agent-framework-go/provider/a2aprovider"
"github.com/microsoft/agent-framework-go/provider/foundryprovider"
"github.com/a2aproject/a2a-go/v2/a2a"
"github.com/a2aproject/a2a-go/v2/a2asrv"
)
a := foundryprovider.NewAgent(endpoint, token, foundryprovider.ModelDeployment(model), foundryprovider.AgentConfig{
Instructions: "You are a helpful assistant.",
Config: agent.Config{
},
})
透過 A2A 暴露代理人:
url := "http://localhost:5000"
card := &a2a.AgentCard{
Name: "MyAgent",
Description: "A helpful assistant.",
Version: "1.0.0",
DefaultInputModes: []string{"text"},
DefaultOutputModes: []string{"text"},
Capabilities: a2a.AgentCapabilities{Streaming: false},
SupportedInterfaces: []*a2a.AgentInterface{
a2a.NewAgentInterface(url, a2a.TransportProtocolJSONRPC),
},
}
mux := http.NewServeMux()
requestHandler := a2asrv.NewHandler(
a2aprovider.NewExecutor(a, a2aprovider.ExecutorConfig{}),
a2asrv.WithExtendedAgentCard(card),
)
mux.Handle("/", a2asrv.NewJSONRPCHandler(requestHandler))
mux.Handle(a2asrv.WellKnownAgentCardPath, a2asrv.NewStaticAgentCardHandler(card))
log.Println("A2A server listening on :5000")
http.ListenAndServe(":5000", mux)
小提示
完整可執行範例請參閱 完整的 A2A 客戶端-伺服器範例 。
後續步驟
深入探討:
- A2A 協議 — 透過 A2A 公開並使用代理
- Durable Extension — 耐用的 C# 與 Python 代理程式及工作流程主機
- AG-UI 協定 — 基於網頁的代理使用者介面
- 主機總覽 — 選擇 Foundry 託管代理、自家託管或耐用主機
- Foundry Hosted Agents 文件 — 了解 Microsoft Foundry 中的託管代理
- Foundry Hosted Agents 範例(Python) — 執行完整的 Agent Framework 託管代理範例