當你建立好代理後,你需要架設它,讓使用者和其他代理能夠互動。
主機選項
| 選項 | Description | 適用對象 |
|---|---|---|
| A2A 協議 | 透過代理對代理協定(Agent-to-Agent)暴露代理 | 多重代理程式系統 |
| OpenAI-Compatible 端點 | 透過聊天完成或回應 API 來暴露代理 | OpenAI 相容用戶端 |
| Azure Functions (Durable) | 將代理程式作為持久的 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 of 'https://<your-own-foundry-endpoint>.openai.azure.com/' format
// deploymentName is 'gpt-4o-mini' for example
IChatClient chatClient = new AzureOpenAIClient(
new Uri(endpoint),
new DefaultAzureCredential())
.GetChatClient(deploymentName)
.AsIChatClient();
builder.Services.AddSingleton(chatClient);
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 Azure Functions 範例 ,了解無伺服器主機的範例。
安裝 Azure Functions 主機套件:
pip install agent-framework-azurefunctions --pre
建立代理人:
from agent_framework.azure import AgentFunctionApp, AzureOpenAIChatClient
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
# Load environment variables from .env file
load_dotenv()
# 1. Instantiate the agent with the chosen deployment and instructions.
def _create_agent() -> Any:
"""Create the Joker agent."""
return AzureOpenAIChatClient(credential=AzureCliCredential()).as_agent(
name="Joker",
instructions="You are good at telling jokes.",
)
使用AgentFunctionApp註冊代理人:
# 2. Register the agent with AgentFunctionApp so Azure Functions exposes the required triggers.
app = AgentFunctionApp(agents=[_create_agent()], enable_health_check=True, max_poll_retries=50)
在本地執行 Azure Functions Core Tools:
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 協議 — 透過 A2A 公開並使用代理
- Azure Functions — serverless agent hosting
- AG-UI 協定 — 基於網頁的代理使用者介面
- Foundry Hosted Agents 文件 — 認識 Azure AI Foundry 中的託管代理
- Foundry Hosted Agents 範例(Python) — 執行完整的 Agent Framework 託管代理範例