跳到內容
AggregaAggregaAggregaAggrega
選擇語言: 繁體中文
本頁為機器翻譯。閱讀英文原文。

匯入 Microsoft Agent Framework 宣告式代理程式

本逐步說明匯入 Microsoft Agent Framework 的一個宣告式代理程式——一個根據它透過 MCP 伺服器搜尋的文件回答問題的助理——並為 Docker Compose 建置該應用程式。匯入會草擬建置所讀取的每個檔案:代理程式與其模型、代理程式以變數參照呈現給模型的金鑰,以及兩者各自的實現。它只詢問一次任何宣告式代理程式都無法提供的那個值,也就是你的應用程式執行所用的映像檔。下面的每個檔案與每一段輸出都來自一次真實的執行;宣告式代理程式與逐步說明的每個階段都在 import-agent-framework 範例中,其中有一個檢查指令碼會再次執行匯入,把它寫出的內容、它印出的內容,以及本頁所引用的區塊,與保存在那裡的檔案比較,並驗證與建置匯入所寫出的內容。

草擬範例

你可以用本頁的命令執行這個匯入,或是讓你的 AI 助理替你執行。

把範例寫入一個以它命名的新資料夾:

Terminal window
aggrega draft --example import-agent-framework

接著它會印出下一步要執行的命令。第一個切換到新資料夾;其後的那一個(見下方)切換到它的 source 資料夾,其中存放本頁所匯入的內容,也是匯入執行的位置:

Terminal window
cd import-agent-framework

在 Visual Studio Code 中開啟的草擬範例 source 資料夾,顯示 prompt 代理程式 SupportAgent、它的 OpenAI 模型與它的 Microsoft Learn MCP 工具。

專案

建置於 Agent Framework 的應用程式,可以從宣告式代理程式載入代理程式:種類為 Prompt 的 YAML 文件,指名代理程式的模型、指示與工具。這一個是為本逐步說明而寫的,指名了 OpenAI 的 gpt-4.1-mini,以及 Microsoft Learn 發布的 MCP 伺服器:

kind: Prompt
name: SupportAgent
displayName: Support Assistant
description: Answers a customer's question about the product from its documentation.
instructions: You answer customers' questions about the product, from its documentation only.
model:
id: gpt-4.1-mini
provider: OpenAI
apiType: Responses
options:
temperature: 0.2
connection:
kind: key
key: =Env.OPENAI_API_KEY
tools:
- kind: mcp
name: microsoft_learn
description: Search Microsoft's documentation on Microsoft Learn.
url: https://learn.microsoft.com/api/mcp
approvalMode:
kind: never
allowedTools:
- microsoft_docs_search
- microsoft_docs_fetch

模型的金鑰寫成 =Env.OPENAI_API_KEY,所以檔案中不含金鑰。只有在應用程式以 AgentFactory(safe_mode=False)(Agent Framework 自己的安全性設定)載入宣告式代理程式,且存在 .NET 執行階段時,Agent Framework 才會從環境變數 OPENAI_API_KEY 讀取該運算式;否則金鑰就是該運算式照寫的文字。載入宣告式代理程式的應用程式程式碼,匯入絕不會讀取,也不是範例的一部分。在該檔案旁,一個 .aggrega-config.yaml 讓這個資料夾成為工作區:

# The workspace key marks this directory as the workspace root, so an import run
# here writes its drafts beside support-agent.yaml.
workspace: {}
# The registry the importer is fetched from the first time an import needs it.
default_registry: hub.aggrega.ai

匯入會參照目錄中的 Agent Framework 代理程式與 OpenAI 模型 Components,而 --target 會從它們的 Recipes 草擬 Manifest。工作區沒有指名目錄資料夾,所以匯入會從它擷取匯入器的登錄庫讀取標準程式庫目錄:已發布目錄的簽署快照,由第一次匯入擷取、驗證並保存在工作區的內容快取中,並在匯入寫於草稿旁的記錄中以其摘要來指名。指名自己目錄資料夾的工作區——在 AGGREGA_CATALOG_DIRS 中,或在 workspace.catalog_dirs 之下——會先讀取那些資料夾,再讀取其後的已發布快照。

匯入要詢問什麼

宣告式代理程式會指名它的模型,但絕不會指名應用程式執行所用的映像檔:你要從你的專案建置那個映像檔。目錄沒有為建置於 Agent Framework 的應用程式提供預設映像檔,所以匯入會詢問它。在沒有現成答案的終端機上執行時,它會問:

The drafts leave values empty that the source cannot hold. Answer each one, or press Enter to leave it for later in the answers file:
release.yaml spec.components.supportagent.infra.image
Container image running the application built on Microsoft Agent Framework.
>

並把答案寫入草稿,以及 support-agent.yaml 旁的 import-answers.yaml,所以下一次匯入會從那裡讀取它,什麼也不問。沒有終端機時——在持續整合中,或透過管道——它什麼也不問:它會寫出鍵為空的檔案,然後停下來,並指出該檔案。範例已經有這個檔案,映像檔的答案是 ghcr.io/example/support-agent:1.0.0,它代表你所建置的映像檔:

# Values the import could not draft from the source, keyed by the drafted file and the key path inside
# it. Write each one beside its key; an import of the same source, and a sync, read them from here and
# ask nothing. A secret is never written here: its key asks where the secret comes from — a variable
# name, ${NAME}, or the name of a Kubernetes Secret.
release.yaml:
# Container image running the application built on Microsoft Agent Framework.
spec.components.supportagent.infra.image: "ghcr.io/example/support-agent:1.0.0"
# The origin of the secret value, written by the operator in the Release. Read exactly as
# values.origin is. Write it here or in values.origin, never in both. A secret: name where it comes
# from, never the value.
spec.components.supportagent.uses.model.gpt-4-1-mini.features.credential.infra.origin: "openai-api-key"

第二個答案是用於針對 Kubernetes 的建置,也就是 --target helm:此時代理程式從 Kubernetes Secret 讀取 OpenAI 金鑰,而答案是該 Secret 的名稱 openai-api-key,絕不是金鑰。針對 Docker Compose 的建置則改從你的 shell 讀取金鑰,所以這次匯入不使用該答案。匯入答案檔說明該檔案、問題,以及機密答案的規則。

aggrega import 列出該命令接受的每個旗標,其中包括 --answers,它可指名保存在別處的答案檔。

切換到 source:

Terminal window
cd source

執行匯入

指名宣告式代理程式的檔案。匯入把每個宣告式代理程式當作各自的檔案來讀取,絕不讀取目錄:

Terminal window
aggrega import support-agent.yaml --target compose

在標準輸出上,它會列出所寫出的內容:

wrote aggregate.yaml
wrote release.yaml
wrote manifest.yaml
wrote .aggrega-import.json

在標準錯誤上,其中 <workspace> 代表你執行它所在的資料夾:

[… omitted: a warning that the importer runs unverified, printed where its module is read from beside its Plugin entity in a catalog folder rather than fetched]
read as the agent-framework format, with drafted paths relative to <workspace>
warn: the value of tools.microsoft_learn.url of node SupportAgent is carried as written in the Release the draft writes: the field belongs to an MCP server's configuration, which can carry a key, and setting empty_credentials to marked or all writes it empty
info: the answers file <workspace>/import-answers.yaml also answers spec.components.supportagent.uses.model.gpt-4-1-mini.features.credential.infra.origin in release.yaml, which these drafts do not leave empty; those answers are kept there and not applied
mapped SupportAgent (agent) to core/ai-agent/agent-framework as a reference, NONE
mapped gpt-4.1-mini (model) to core/ai-model/openai as a reference, NONE
made no choice for SupportAgent#microsoft_learn (tool)
Pull complete: <workspace>/release.lock (entities=8, source_artifacts=0, external_inputs=0)

從編輯器執行的匯入:草擬的 Aggregate、Release 與 Manifest 出現在宣告式代理程式旁,Aggrega 輸出列出代理程式與其模型是如何對應的以及所寫出的檔案,通知則說明匯入草擬了 3 個檔案,辨識為 agent-framework。

  • warn 那一行指出一個可能帶有金鑰的值——MCP 伺服器的位址——並說明它被照原樣帶入。這個位址不帶任何金鑰。
  • info 那一行指出 Docker Compose 草稿不使用的答案,它被保存在答案檔中,供針對 Kubernetes 的建置使用。
  • mapped 那幾行說明代理程式是標準程式庫 Agent Framework 代理程式 core/ai-agent/agent-framework 的實例,而它的模型是 core/ai-model/openai 的實例。它們的層級顯示為 NONE:Agent Framework 匯入是依據宣告式代理程式所指名的內容來參照這些 Components,而不是把每一個與目錄比較評分。
  • made no choice 那一行是 MCP 伺服器,匯入不把它草擬成任何 Component,原因見 aggregate.yaml 中的註解。
  • Pull complete 那一行是匯入在擷取草擬 Release 所依賴的內容,就像 aggrega pull 所做的那樣,寫入 release.lock,讓草稿立即就能驗證與建置。在 --offline 下,匯入不拉取任何東西,並指出該執行的 aggrega pull。

它寫出什麼

Aggregate、Release 與 Manifest 都命名為 imported/supportagent,來自代理程式的 name,也就是 SupportAgent,簡化為 supportagent——它的 displayName 不會被讀取——位於匯入器的 target_namespace 設定所設的命名空間之下,除非你設定別的,否則為 imported(設定)。

aggregate.yaml — 代理程式,連到它的模型,並在連線上宣告它所呈現的金鑰,以及模型:

# Not read: the application's code, which loads each declarative agent and binds the functions of its function tools; the application runs in the image the Release names for each instance, and loads its declarative agents itself.
apiVersion: aggrega.ai/v1beta1
kind: Aggregate
metadata:
name: imported/supportagent
version: 0.1.0
description: Draft Aggregate imported from a source in the agent-framework format.
spec:
components:
# The declarative agent SupportAgent.
# Read by the application from the declarative agent, and taken by no key of core/ai-agent/agent-framework: instructions, the options of its model requests (temperature).
# The MCP server microsoft_learn, drafted as no Component and no connection, since Agent Framework sends it to the model's provider as a hosted tool, and the provider, not the application, reaches it at its url, which must therefore be reachable from the provider; its url is written above supportagent's entry in the Release.
# Stated by the declarative agent for microsoft_learn: its description, "Search Microsoft's documentation on Microsoft Learn."; its approval mode, never; the tools of the server it allows, microsoft_docs_search, microsoft_docs_fetch.
supportagent:
type: core/ai-agent/agent-framework
values:
display_name: "Support Assistant"
description: "Answers a customer's question about the product from its documentation."
uses:
model:
gpt-4-1-mini:
target: gpt-4-1-mini
# For the compose target, the lines below declare credential on gpt-4-1-mini: a secret the Release alone attaches arrives under the key value, so each is declared with a key of its own and no two arrive under one; the Release attaches its origin, naming this type or none.
features:
credential:
type: core/secret-value
values:
key: OPENAI_API_KEY
# The model gpt-4.1-mini, which SupportAgent names with the OpenAI provider.
gpt-4-1-mini:
type: core/ai-model/openai
values:
model: "gpt-4.1-mini"

MCP 伺服器不是 Component,也不是代理程式的連線:Agent Framework 把它當作託管工具傳送給模型的提供者,由提供者而不是應用程式在它的 url 觸及它,因此該位址必須能從提供者觸及。指示、模型請求的選項,以及伺服器的描述、核准模式與允許的工具,都留在應用程式所讀取的宣告式代理程式中;註解會指出每一項。OpenAI 的 API 是託管服務,所以代理程式的 Component 會拒絕不帶認證而連到它的連線;連線會宣告一個,鍵為 OPENAI_API_KEY,也就是宣告式代理程式讀取其金鑰所用的變數。

release.yaml — 環境、映像檔、發布的連接埠,以及金鑰的來源:

apiVersion: aggrega.ai/v1beta1
kind: Release
metadata:
name: imported/supportagent
# environment is a drafted default, since no source names the environment this Release deploys to; change it to name the one it deploys to.
environment: development
version: 0.1.0
description: Draft Release imported from a source in the agent-framework format.
spec:
aggregate: imported/supportagent
manifest: imported/supportagent
components:
# The declarative agent also states these values for its model and its MCP servers, written here for review:
# - model.connection.key, written as =Env.OPENAI_API_KEY, which Agent Framework reads from the environment variable OPENAI_API_KEY only where the application loads the declarative agent with AgentFactory(safe_mode=False), Agent Framework's own security setting, and a .NET runtime is present; otherwise the value is the expression's text as written
# - tools.microsoft_learn.url: "https://learn.microsoft.com/api/mcp"
supportagent:
infra:
# Left for the Release, since the source cannot supply it: infra.image, the container image running the application built on Agent Framework, holding its declarative agent
image: "ghcr.io/example/support-agent:1.0.0"
# Published on this machine's loopback address, 127.0.0.1: reachable from this machine only, by any process running on it. Delete this line to keep the service internal.
published_port: 8000
uses:
model:
gpt-4-1-mini:
features:
credential:
type: core/secret-value
infra:
# Attached by reference, never as a value: the credential supportagent presents to gpt-4-1-mini on its model connection, read from the variable OPENAI_API_KEY, which a target substituting variable references, as Docker Compose does when the stack starts, reads from the environment, so no file carries the value.
origin: "${OPENAI_API_KEY}"
gpt-4-1-mini: {}

環境 development 是匯入寫入的預設值,因為沒有任何宣告式代理程式指名環境;若 Release 要部署到別處,請更改它。映像檔是匯入從 import-answers.yaml 讀取的答案。代理程式發布在你機器的回送位址上,連接埠為 8000,轉送到它的程式所監聽的連接埠 infra.port,除非你另外設定,否則為 8000。金鑰是變數參照 ${OPENAI_API_KEY},以宣告式代理程式所讀取的變數命名,絕不是值。MCP 伺服器的位址照宣告式代理程式所陳述的寫入,供檢閱,且不設定在其他任何地方:Agent Framework 會在它對模型的請求中把它交給提供者。

manifest.yaml — 代理程式與模型的 Compose Recipe,以及把金鑰的變數參照照原樣寫入 Compose 檔案的實現:

apiVersion: aggrega.ai/v1beta1
kind: Manifest
metadata:
name: imported/supportagent
version: 0.1.0
spec:
mappings:
components:
type:
core/ai-agent/agent-framework: compose/agent-framework
core/ai-model/openai: compose/openai
features:
type:
core/secret-value: compose/dotenv-secret

release.lock 以摘要記錄 Release 所解析到的目錄實體,而 .aggrega-import.json 是匯入所寫內容的記錄,之後的 aggrega import --sync 會讀取它。

驗證並建置

Terminal window
aggrega validate --release release.yaml
aggrega build --release release.yaml

驗證後在編輯器中開啟的草擬 Release,Aggrega 輸出與通知都回報沒有發現問題。

草擬 Release 的建置:Explorer 中出現 dist 資料夾,Aggrega 輸出列出所寫出的 Docker Compose 檔案,通知說明寫出了 1 個檔案。

驗證會印出:

notice: LINT: Feature "core/secret-value" at core/secret-value:1.0.0 declares property "origin" in both values: and infra: pillars (path: origin)
release dependency validation passed: release.yaml (schema-pillar + dependency pillars)

而建置(其中 <workspace> 代表你執行它所在的資料夾)會印出:

notice: LINT: Feature "core/secret-value" at core/secret-value:1.0.0 declares property "origin" in both values: and infra: pillars (path: origin)
[… omitted: a total build time, which differs per run]
Build completed: 1 file written to <workspace>/dist

notice 那一行僅供參考:機密 Feature 從 Aggregate 的 values 或 Release 的 infra 接受它的 origin,而該通知回報有一個葉節點同時在兩者中宣告。它不改變任何裁定。

代理程式在 dist/compose/docker-compose.yaml 中的服務:

services:
supportagent:
image: ghcr.io/example/support-agent:1.0.0
environment:
OPENAI_BASE_URL: https://api.openai.com/v1
OPENAI_MODEL: gpt-4.1-mini
OPENAI_API_KEY: ${OPENAI_API_KEY}
ports:
- 127.0.0.1:8000:8000
[… omitted: the labels the build writes on every service, naming the Aggregate, the component, the environment and the compiler's version]
networks:
- compose

應用程式會在 Agent Framework 的 OpenAI 用戶端所讀取的變數中,收到模型的位址、名稱與金鑰,所以宣告式代理程式的 =Env.OPENAI_API_KEY 會在應用程式載入它的地方,讀取 Release 所附加的金鑰,如專案所述。沒有任何變數帶有 MCP 伺服器的位址,因為由提供者觸及它。模型沒有自己的服務:代理程式在它自己的位址觸及 OpenAI API。金鑰是變數參照,在堆疊啟動時從你的 shell 填入。服務只在 127.0.0.1 上發布它的連接埠。

搭配 AI 助理

設定了 Aggrega 工具的 AI 助理也能做同樣的事:把這段提示貼給它。

只使用 Aggrega 工具,且不要自己建立任何資料夾。如果工作區資料夾中還沒有 `import-agent-framework` 資料夾,請以 `example` 設為 `import-agent-framework` 且不帶 `dir`,呼叫一次 `draft` 工具:它會把範例寫入新的 `import-agent-framework` 資料夾,其 `source` 資料夾存放本頁所匯入的內容。接著以 `workspace_root` 設為 `import-agent-framework/source`、`source` 設為 `support-agent.yaml`、`target` 設為 `compose` 呼叫 `import` 工具:它會從 `support-agent.yaml` 旁的 `import-answers.yaml` 讀取所需的答案,把草稿寫入 `import-agent-framework/source`,並拉取它們所需的內容。接著以 `workspace_root` 設為 `import-agent-framework/source`、`release` 設為 `release.yaml` 呼叫 `validate` 工具,並以 `workspace_root` 設為 `import-agent-framework/source`、`release` 設為 `release.yaml`、`write` 設為 true 呼叫 `build` 工具。告訴我建置寫出了哪些檔案;它們位於 `import-agent-framework/source/dist/` 之下。

在 VS Code 中開啟

安裝了 Aggrega 的 Visual Studio Code 擴充功能之後,請在 VS Code 中開啟資料夾 import-agent-framework/source,匯入就把草稿寫在那裡。該資料夾只有一個 Release,也就是草擬的 release.yaml,所以編輯器會替你選取它,並在你編輯草稿時依據該 Release 檢查它們,在檔案與 Problems 面板中標示它所發現的問題。命令選擇區(Command Palette)中的 Aggrega: Show topology graph 會繪出該 Release 的元件及它們之間的連線。Aggrega: Build、Aggrega: Preview build 與 Aggrega: Pull 都作用於同一個 Release,而且每一個也都是活動列中 Aggrega 檢視的 Commands 面板上的按鈕;Build 會把它的檔案寫入該資料夾的 dist/。在 Visual Studio Code 中使用 Aggrega 涵蓋其餘部分。

執行

你要從專案以自己的 Dockerfile 建置映像檔:應用程式的程式碼,安裝了 Agent Framework,並且內含宣告式代理程式。Docker Compose 會把映像檔自己的命令當作代理程式的服務來執行,因為 Recipe 沒有設定任何命令,所以那個命令必須持續執行,而且若你想觸及代理程式,它就必須能處理請求。位址、路由與回覆,由你的程式決定。

下面的執行使用了這樣的映像檔:Python,搭配 Agent Framework 的 agent-framework-core、agent-framework-openai 與 agent-framework-declarative 套件、.NET 執行階段——映像檔把 DOTNET_ROOT 設為它的目錄,並把 DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1——support-agent.yaml,以及作為映像檔命令、該程式自己在連接埠 8000 上的小型 HTTP 伺服器。該伺服器以 AgentFactory(safe_mode=False) 載入宣告式代理程式,所以它的 =Env.OPENAI_API_KEY 會讀取建置所傳遞的金鑰,並對每個 POST /ask 執行代理程式一次。在專案根目錄建置你的映像檔,把它的名稱寫在 import-answers.yaml 中 ghcr.io/example/support-agent:1.0.0 的位置,再以 --sync 匯入一次,它會把新的答案寫入草稿並再次拉取;然後建置:

Terminal window
docker build -t docs-assistant:local .
aggrega import support-agent.yaml --sync
aggrega build --release release.yaml

以 docs-assistant:local 作為答案時,同步會在它印出的那幾行中,指出它所更改的那一個值:

changed spec.components.supportagent.infra.image in release.yaml

然後在你的 shell 中設定金鑰並啟動堆疊:

Terminal window
export OPENAI_API_KEY='your OpenAI key'
docker compose -p docs-assistant -f dist/compose/docker-compose.yaml up -d --wait

-p 會指名專案,如匯入如何執行的執行步驟所說明。

測試

下面的請求會觸及本逐步說明的映像檔所執行的程式:在連接埠 8000 上的小型 HTTP 伺服器後方的宣告式代理程式,回應 POST /ask。你自己的程式會自行決定路由與回覆。請在程式開始監聽之後(堆疊啟動後幾秒鐘),把它傳送到 http://127.0.0.1:8000,也就是 published_port 所發布的位址:

Terminal window
curl -sS -X POST http://127.0.0.1:8000/ask -H 'Content-Type: application/json' \
-d '{"question": "In one short sentence: what is Azure Container Apps?"}'
{"error": "the agent failed: ChatClientException: (\"<class 'agent_framework_openai._chat_client.OpenAIChatClient'> service failed to complete the prompt: Error code: 401 - Incorrect API key provided: sk-place************-key. [… omitted: where to find an API key, and the same error repeated as the exception it wraps]"}

該次執行把 OPENAI_API_KEY 設為預留位置,所以回覆是 OpenAI 對它的拒絕,由程式轉達。OpenAI 拒絕的金鑰就是那個預留位置,以遮蔽方式顯示,所以宣告式代理程式的 =Env.OPENAI_API_KEY 是從建置所傳遞的環境中讀取它的。

以下列命令停止堆疊:

Terminal window
docker compose -p docs-assistant -f dist/compose/docker-compose.yaml down

在 Kubernetes 上,請以 --target helm 匯入,並把映像檔推送到你的叢集可從中拉取的登錄庫,在 import-answers.yaml 中指名它。然後建立 Secret openai-api-key 並套用建置,如匯入如何執行的執行步驟所述。代理程式的 Deployment 沒有 Service,所以請把本機連接埠轉送給它。該命令會一直執行到你停止為止:

Terminal window
kubectl port-forward deploy/supportagent 8000:8000
Forwarding from 127.0.0.1:8000 -> 8000
Forwarding from [::1]:8000 -> 8000

從另一個終端機,把相同的請求傳送到轉送的連接埠:

Terminal window
curl -sS -X POST http://127.0.0.1:8000/ask -H 'Content-Type: application/json' \
-d '{"question": "In one short sentence: what is Azure Container Apps?"}'
{"error": "the agent failed: ChatClientException: (\"<class 'agent_framework_openai._chat_client.OpenAIChatClient'> service failed to complete the prompt: Error code: 401 - Incorrect API key provided: sk-place************-key. [… omitted: where to find an API key, and the same error repeated as the exception it wraps]"}