匯入 Google ADK 代理程式
本逐步說明會匯入以 Google 的 Agent Development Kit(ADK)建置之應用程式的 Agent Config——一個回答客戶訂單問題的代理程式——並為 Docker Compose 建置該應用程式。匯入會草擬建置所讀取的每個檔案:代理程式與其 Gemini 模型、代理程式以變數參照出示給模型的金鑰,以及各自的實現。它只詢問一次沒有任何設定保存的那個值,也就是應用程式執行所用的映像檔。下方的每個檔案與每段輸出都來自真實執行;該設定與逐步說明的每個階段都在 import-google-adk 範例中,其中的檢查指令碼會再次執行匯入,將其寫出的內容、印出的內容,以及本頁引用的區塊與該處保存的檔案比對,並驗證與建置匯入所寫出的內容。
草擬範例
你可以用本頁的命令執行這個匯入,或是讓你的 AI 助理替你執行。
把範例寫入一個以它命名的新資料夾:
aggrega draft --example import-google-adk接著它會印出下一步要執行的命令。第一個切換到新資料夾;其後的那一個(見下方)切換到它的 source 資料夾,其中存放本頁所匯入的內容,也是匯入執行的位置:
cd import-google-adk
專案
ADK 可以在 Agent Config 檔案而非程式碼中宣告應用程式的代理程式:adk create --type=config 會寫出 root_agent.yaml,即根代理程式的設定,以及它旁邊的環境檔案。這個為本逐步說明撰寫的設定,宣告了一個使用 Gemini 的 gemini-2.5-flash 的代理程式,並帶有 ADK 隨附的 Google Search 工具:
# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.jsonname: support_agentmodel: gemini-2.5-flashdescription: Answers a customer's question about their order.instruction: | You answer customers' questions about their orders. When a customer asks where a parcel is, search the web for the tracking page of the company shipping it.tools: - name: google_search匯入從不讀取環境檔案,而該檔案通常保存應用程式傳送給 Gemini 的金鑰,所以範例中沒有保存任何金鑰。在專案的根目錄,.aggrega-config.yaml 讓該資料夾成為工作區:
# The workspace key marks this directory as the workspace root, so an import run# here writes its drafts beside support_agent/.workspace: {}# The registry the importer is fetched from the first time an import needs it.default_registry: hub.aggrega.ai匯入會參照目錄的 ADK 代理程式與 Gemini 模型 Components,而 --target 會從其 Recipes 草擬 Manifest。工作區沒有指定目錄資料夾,所以匯入會從它取得匯入器的登錄庫讀取標準程式庫目錄:那是已發布目錄的簽署快照,由第一次匯入取得、驗證並保存在工作區的內容快取中,並以其摘要記載於匯入寫在草稿旁的記錄中。若工作區在 AGGREGA_CATALOG_DIRS 或 workspace.catalog_dirs 之下指定了自己的目錄資料夾,則會先讀取那些資料夾,再讀取其後的已發布快照。
匯入要詢問什麼
Agent Config 會指名代理程式的模型,但從不指名應用程式執行所用的映像檔:該映像檔是你從專案建置的。目錄沒有為以 ADK 建置的應用程式提供預設映像檔,所以匯入會詢問它。在終端機中它會詢問,並將答案寫入草稿,以及寫入它所匯入之目錄 support_agent/ 內的答案檔 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 ADK, serving its API on infra.port on every # interface. spec.components.support-agent.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.support-agent.uses.model.gemini-2-5-flash.features.credential.infra.origin: "google-api-key"第二個答案用於 Kubernetes 的建置,使用 --target helm:在那裡代理程式從 Kubernetes Secret 讀取 Gemini API 金鑰,而答案是該 Secret 的名稱 google-api-key,絕不是金鑰本身。Docker Compose 的建置改為從你的 shell 讀取金鑰,所以這次匯入不使用該答案。匯入答案檔說明了該檔案、問題,以及機密答案的規則。
aggrega import 列出該命令接受的每個旗標,其中包括 --answers,它可指名保存在別處的答案檔。
切換到 source:
cd source執行匯入
指名代理程式的目錄。匯入會讀取其 root_agent.yaml:
aggrega import support_agent --target compose在標準輸出上,它會列出所寫出的內容:
wrote aggregate.yamlwrote release.yamlwrote manifest.yamlwrote .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 google-adk format, with drafted paths relative to <workspace>info: the answers file <workspace>/support_agent/import-answers.yaml also answers spec.components.support-agent.uses.model.gemini-2-5-flash.features.credential.infra.origin in release.yaml, which these drafts do not leave empty; those answers are kept there and not appliedmapped support_agent (agent) to core/ai-agent/google-adk as a reference, NONEmapped gemini-2.5-flash (model) to core/ai-model/gemini as a reference, NONEmade no choice for google_search (tool)Pull complete: <workspace>/release.lock (entities=8, source_artifacts=0, external_inputs=0)
info那一行指出 Docker Compose 草稿不使用的答案,它被保存在答案檔中,供針對 Kubernetes 的建置使用。mapped行說明代理程式是標準程式庫 ADK 代理程式core/ai-agent/google-adk的一個實例,而它所指名的模型是core/ai-model/gemini的一個實例。它們的階層顯示為NONE:ADK 匯入是依設定所指名的內容參照這些 Components,而不是將每個與目錄比對權衡。made no choice行說明 Google Search 工具被草擬為空無一物:它是 ADK 隨附的工具,在應用程式內部執行。Pull complete那一行是匯入在擷取草擬 Release 所依賴的內容,就像aggrega pull所做的那樣,寫入release.lock,讓草稿立即就能驗證與建置。在--offline下,匯入不拉取任何東西,並指出該執行的aggrega pull。
它寫出什麼
Aggregate、Release 與 Manifest 都命名為 imported/support-agent,取自根代理程式的 name,即 support_agent,簡化為 support-agent,位於匯入器的 target_namespace 設定所指定的命名空間之下,除非你另行設定,否則為 imported(Settings)。
aggregate.yaml——代理程式,連接到模型,其出示的金鑰宣告在連線上,以及模型,開頭的註解說明了讀取了什麼與沒有讀取什麼:
# Google ADK agent configurations read:# - root_agent.yaml# Not read: the application's code, which defines the tools, callbacks and agents a configuration names by their code, nor the environment file ADK's scaffold writes beside root_agent.yaml; the application runs in the image the Release names for each instance, and loads its configurations itself.# ADK states that its Agent Config is experimental. An agent's model is a Gemini model by default; ADK serves some other providers' models by name through classes of its own, such as claude-sonnet-4-5 and gpt-4o, and others through a LiteLLM model string.apiVersion: aggrega.ai/v1beta1kind: Aggregatemetadata: name: imported/support-agent version: 0.1.0 description: Draft Aggregate imported from a source in the google-adk format.spec: components: # The agent tree of root_agent.yaml, served from its root agent support_agent. # Tools ADK ships, which run inside the application, drafted as no Component: google_search. support-agent: type: core/ai-agent/google-adk values: description: "Answers a customer's question about their order." uses: model: gemini-2-5-flash: target: gemini-2-5-flash # For the compose target, the lines below declare credential on gemini-2-5-flash: 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: GOOGLE_API_KEY # The model gemini-2.5-flash. gemini-2-5-flash: type: core/ai-model/gemini values: model: "gemini-2.5-flash"ADK 從根代理程式提供應用程式服務,所以草稿為每個代理程式樹保存一個實例,以其根代理程式命名。代理程式的 instruction 不會被帶入:應用程式從映像檔所保存的自身設定中讀取它。
ADK 透過其 Gemini 類別提供 gemini-2.5-flash,該類別會將請求傳送到 Gemini API,所以草稿將模型連接到代理程式的 model 插槽,該插槽只接受 Gemini API。Gemini 是託管服務,所以代理程式的 Component 會拒絕不帶任何認證的連線;該連線在鍵 GOOGLE_API_KEY 之下宣告一個。ADK 以任何其他方式提供的模型——例如 ADK 透過自己的類別提供的 gpt-4o 這類名稱,或 openai/gpt-4o 這類 LiteLLM 模型字串——不會連接到任何東西:實例上方的註解會指名它,而連線則留給你處理,如 What an agent tree becomes 所述。
release.yaml — 環境、映像檔、發布的連接埠,以及金鑰的來源:
apiVersion: aggrega.ai/v1beta1kind: Releasemetadata: name: imported/support-agent # 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 google-adk format.spec: aggregate: imported/support-agent manifest: imported/support-agent components: support-agent: infra: # Left for the Release, since the source cannot supply it: infra.image, the container image running the application built on ADK, holding its configurations 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: gemini-2-5-flash: features: credential: type: core/secret-value infra: # Attached by reference, never as a value: the credential support-agent presents to gemini-2-5-flash on its model connection (the source names no variable for it, so it is read from the one Google's Gemini API's client libraries read the API key from), read from the variable GOOGLE_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: "${GOOGLE_API_KEY}" gemini-2-5-flash: {}環境 development 是匯入寫入的預設值,因為沒有任何設定指名環境;若 Release 部署到其他地方,請變更它。映像檔是匯入從 support_agent/import-answers.yaml 讀取的答案。published_port 是 8000,也就是應用程式監聽的連接埠,所以它在你機器上的 http://127.0.0.1:8000 回應;如其註解所說,這部機器上的任何處理程序都能在那裡連到它,其他則都不能,而刪除該行會讓應用程式只留在堆疊的網路上。設定沒有指名金鑰的變數,所以連線從 ${GOOGLE_API_KEY} 讀取它,也就是 Gemini API 的用戶端程式庫所讀取的變數,作為變數參照,絕不是值。
manifest.yaml——每個型別的 Compose Recipe,以及將金鑰的變數參照依原樣寫入 Compose 檔案的實現:
apiVersion: aggrega.ai/v1beta1kind: Manifestmetadata: name: imported/support-agent version: 0.1.0spec: mappings: components: type: core/ai-agent/google-adk: compose/google-adk core/ai-model/gemini: compose/gemini features: type: core/secret-value: compose/dotenv-secretrelease.lock 以摘要記錄 Release 所解析到的目錄實體,而 .aggrega-import.json 是匯入所寫內容的記錄,之後的 aggrega import --sync 會讀取它。
驗證並建置
aggrega validate --release release.yamlaggrega build --release release.yaml

驗證會印出:
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>/distnotice 那一行僅供參考:機密 Feature 從 Aggregate 的 values 或 Release 的 infra 接受它的 origin,而該通知回報有一個葉節點同時在兩者中宣告。它不改變任何裁定。
代理程式在 dist/compose/docker-compose.yaml 中的服務:
services: support-agent: image: ghcr.io/example/support-agent:1.0.0 environment: PORT: "8000" GOOGLE_API_KEY: ${GOOGLE_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應用程式會以 PORT 接收其伺服器監聽的連接埠,並以 GOOGLE_API_KEY 接收金鑰,ADK 的 Gemini 用戶端會將它傳送到 Gemini API。模型沒有自己的服務:用戶端將其請求傳送到 Gemini API 自己的位址,並且不會從其環境讀取其他位址。金鑰是變數參照,在堆疊啟動時從你的 shell 填入。只有在 Release 於代理程式的 infra 中設定 published_port 時,ADK 代理程式的 Compose Recipe 才會在你的機器上發布連接埠,而且僅限回送位址 127.0.0.1;沒有它時,該服務只能在堆疊的網路上連到。
搭配 AI 助理
設定了 Aggrega 工具的 AI 助理也能做同樣的事:把這段提示貼給它。
只使用 Aggrega 工具,且不要自行建立任何資料夾。如果 Workspace 資料夾中還沒有 `import-google-adk` 資料夾,請呼叫一次 `draft` 工具,將 `example` 設為 `import-google-adk`,且不帶 `dir`:它會將範例寫入新的 `import-google-adk` 資料夾,其 `source` 資料夾保存本頁所匯入的內容。接著呼叫 `import` 工具,將 `workspace_root` 設為 `import-google-adk/source`、`source` 設為 `support_agent`、`target` 設為 `compose`:它會從 `support_agent` 資料夾內的 `import-answers.yaml` 讀取所需的答案,將草稿寫入 `import-google-adk/source`,並拉取它們所需的內容。接著呼叫 `validate` 工具,將 `workspace_root` 設為 `import-google-adk/source`、`release` 設為 `release.yaml`,並呼叫 `build` 工具,將 `workspace_root` 設為 `import-google-adk/source`、`release` 設為 `release.yaml`、`write` 設為 true。告訴我建置寫出了哪些檔案;它們位於 `import-google-adk/source/dist/` 之下。在 VS Code 中開啟
安裝 Aggrega extension for Visual Studio Code 後,在 VS Code 中開啟資料夾 import-google-adk/source,匯入就是把草稿寫在那裡。該資料夾保存一個 Release,即草稿的 release.yaml,所以編輯器會替你選取它,並在你編輯草稿時依該 Release 檢查它們,在檔案與 Problems 面板中標示所發現的問題。命令選擇區中的 Aggrega: Show topology graph 會繪出該 Release 的元件以及它們之間的連線。Aggrega: Build、Aggrega: Preview build 與 Aggrega: Pull 都作用於同一個 Release,而且每一個也是活動列中 Aggrega 檢視的 Commands 面板上的按鈕;Build 會將其檔案寫入該資料夾的 dist/。其餘內容請見 Use Aggrega in Visual Studio Code。
執行
Docker Compose 會將你的映像檔作為上方渲染的服務執行,包含 PORT。映像檔必須在每個介面上監聽該連接埠,而不是只在 127.0.0.1 上,後者是 ADK 的 API 伺服器的預設位址。ADK 自己的 API 伺服器 adk api_server 會依原樣提供設定目錄,所以映像檔不需要你的任何程式碼。專案根目錄中、support_agent/ 旁的 Dockerfile:
FROM python:3.12-slimRUN pip install --no-cache-dir google-adk==2.13.0COPY support_agent/ /app/agents/support_agent/CMD ["sh", "-c", "exec adk api_server --host 0.0.0.0 --port \"$PORT\" --auto_create_session /app/agents"]adk api_server 會提供它所指名之資料夾中的代理程式目錄,而命令會將 PORT 傳給它。--auto_create_session 會在第一次有對 /run 的請求指名某個工作階段時開啟該工作階段,所以一個請求就能連到代理程式。建置映像檔,在 support_agent/import-answers.yaml 中以其名稱取代 ghcr.io/example/support-agent:1.0.0,並以 --sync 再次匯入,這會將新的答案寫入草稿並再次拉取;然後建置:
docker build -t support-agent:local .aggrega import support_agent --syncaggrega build --release release.yaml然後在你的 shell 中設定金鑰並啟動堆疊:
export GOOGLE_API_KEY='your Gemini API key'docker compose -p support-agent -f dist/compose/docker-compose.yaml up -d --wait-p 為專案命名,如 How an import runs 的執行步驟所說明。--wait 在容器執行後返回,因為建置沒有為該服務提供健康檢查,而伺服器稍後才會回應,所以請重複被它拒絕的請求。
測試
在 http://127.0.0.1:8000,也就是 published_port 所發布的位址,傳送訊息給代理程式。請求會指名一位使用者與一個工作階段,而 /run 會在其第一則訊息時開啟該工作階段:
curl -sS -X POST http://127.0.0.1:8000/run -H 'Content-Type: application/json' \ -d '{"app_name": "support_agent", "user_id": "u1", "session_id": "s1", "new_message": {"role": "user", "parts": [{"text": "Where is my order 1234? Answer in one short sentence."}]}}'Gemini 的回覆會以該回合新增到工作階段中的事件清單形式回來。該次執行對金鑰使用了預留位置,所以 Gemini 拒絕了它:請求回應 HTTP 500,Internal Server Error,而服務的記錄,即 docker compose -p support-agent -f dist/compose/docker-compose.yaml logs support-agent,會指出原因:
google.genai.errors.ClientError: 400 INVALID_ARGUMENT. {'error': {'code': 400, 'message': 'API key not valid. Please pass a valid API key.', 'status': 'INVALID_ARGUMENT', [… omitted: the error's details]}}curl -sS http://127.0.0.1:8000/list-apps 回應 ["support_agent"],也就是伺服器所保存的代理程式。
以下列命令停止堆疊:
docker compose -p support-agent -f dist/compose/docker-compose.yaml down在 Kubernetes 上
同一個代理程式可用 --target helm 匯入到 Kubernetes。叢集從登錄庫拉取其映像檔,所以請將你建置的映像檔推送到叢集可讀取的登錄庫,並在 support_agent/import-answers.yaml 中以其名稱取代所回答的映像檔;該次執行將它推送到叢集自己機器上的登錄庫,為 localhost:5000/support-agent:1.0.0。那裡的第二個答案指名代理程式讀取 Gemini API 金鑰的 Secret,即 google-api-key。供 Docker Compose 使用的草稿已經在 source 中,所以請將這些草稿草擬到它們自己的資料夾,並從那裡建置:
aggrega import support_agent --target helm --output-dir helmcd helmaggrega build --release release.yaml建置會將一般的 Kubernetes 資訊清單寫在工作區根目錄 source 的 dist/helm/ 之下,從 helm 看來是 ../dist/helm/。在你套用到的命名空間中建立 Secret google-api-key,在資料鍵 value 之下保存金鑰,並套用該資料夾,如 How an import runs 的執行步驟所說明。代理程式的 Service 是 support-agent-default:將本機連接埠轉送到它,並讓該命令持續執行:
kubectl port-forward -n <namespace> svc/support-agent-default 8000:8000在另一個終端機中,上面的訊息會在同一位址原封不動地連到代理程式。該次執行得到同樣的 Internal Server Error,而 kubectl logs -n <namespace> deploy/support-agent 指出對預留位置金鑰的同樣拒絕。