ข้ามไปยังเนื้อหา
AggregaAggregaAggregaAggrega
เลือกภาษา: ไทย
หน้านี้ถูกแปลด้วยเครื่อง อ่านต้นฉบับภาษาอังกฤษ

นำเข้าเอเจนต์ Google ADK

บทแนะนำทีละขั้นตอนนี้นำเข้า Agent Config ของแอปพลิเคชันที่สร้างบน Agent Development Kit (ADK) ของ Google ซึ่งเป็นเอเจนต์หนึ่งตัวที่ตอบคำถามของลูกค้าเกี่ยวกับคำสั่งซื้อของพวกเขา และบิลด์แอปพลิเคชันสำหรับ Docker Compose การนำเข้าร่างทุกไฟล์ที่บิลด์อ่าน ได้แก่ เอเจนต์และโมเดล Gemini ของเอเจนต์ คีย์ที่เอเจนต์ยื่นให้โมเดลในรูปแบบการอ้างอิงตัวแปร และการทำให้เป็นรูปธรรมของแต่ละอย่าง การนำเข้าถามเพียงครั้งเดียวถึงค่าเดียวที่ไม่มีการกำหนดค่าใดเก็บไว้ คืออิมเมจที่แอปพลิเคชันรันอยู่ ทุกไฟล์และทุกบล็อกเอาต์พุตด้านล่างมาจากการรันจริง การกำหนดค่าและแต่ละขั้นตอนของบทแนะนำอยู่ในตัวอย่าง import-google-adk ซึ่งสคริปต์ตรวจสอบรันการนำเข้าอีกครั้ง เปรียบเทียบสิ่งที่การนำเข้าเขียน สิ่งที่พิมพ์ และบล็อกที่หน้านี้อ้างถึงกับไฟล์ที่เก็บไว้ที่นั่น แล้วตรวจสอบความถูกต้องและบิลด์สิ่งที่การนำเข้าเขียน

ร่างตัวอย่าง

คุณรันการนำเข้านี้ด้วยคำสั่งในหน้านี้ได้ หรือ ให้ผู้ช่วย AI ของคุณรันให้

เขียนตัวอย่างลงในโฟลเดอร์ใหม่ที่ตั้งชื่อตามตัวอย่าง:

Terminal window
aggrega draft --example import-google-adk

จากนั้นจะพิมพ์คำสั่งที่ต้องรันต่อ คำสั่งแรกเปลี่ยนเข้าโฟลเดอร์ใหม่ ส่วนคำสั่งถัดไปด้านล่างเปลี่ยนเข้าโฟลเดอร์ source ซึ่งมีสิ่งที่หน้านี้นำเข้าและเป็นที่ที่การนำเข้ารัน:

Terminal window
cd import-google-adk

โฟลเดอร์ source ของตัวอย่างที่ร่างไว้เปิดอยู่ใน Visual Studio Code แสดงการกำหนดค่าของเอเจนต์: support_agent บน gemini-2.5-flash พร้อมเครื่องมือ google_search

โปรเจกต์

ADK ประกาศเอเจนต์ของแอปพลิเคชันในไฟล์ Agent Config แทนโค้ดได้ โดย adk create --type=config เขียน root_agent.yaml ซึ่งเป็นการกำหนดค่าของเอเจนต์ราก และไฟล์สภาพแวดล้อมไว้ข้างกัน ไฟล์นี้ซึ่งเขียนขึ้นสำหรับบทแนะนำนี้ ประกาศเอเจนต์หนึ่งตัวบน gemini-2.5-flash ของ Gemini พร้อมเครื่องมือ Google Search ที่ ADK มีมาให้:

# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json
name: support_agent
model: gemini-2.5-flash
description: 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

การนำเข้าอ้างอิง Components เอเจนต์ ADK และโมเดล Gemini ของแค็ตตาล็อก และ --target ร่าง Manifest จาก Recipes ของแค็ตตาล็อก พื้นที่ทำงานไม่ระบุโฟลเดอร์แค็ตตาล็อก การนำเข้าจึงอ่านแค็ตตาล็อกไลบรารีมาตรฐานจากรีจิสทรีที่ดึงตัวนำเข้ามา คือสแนปช็อตที่ลงลายเซ็นของแค็ตตาล็อกที่เผยแพร่ ซึ่งการนำเข้าครั้งแรกดึงมา ยืนยัน และเก็บไว้ในแคชเนื้อหาของพื้นที่ทำงาน และระบุด้วยไดเจสต์ในบันทึกที่การนำเข้าเขียนไว้ข้างร่าง พื้นที่ทำงานที่ระบุโฟลเดอร์แค็ตตาล็อกของตนเอง ใน AGGREGA_CATALOG_DIRS หรือใต้ workspace.catalog_dirs จะอ่านโฟลเดอร์เหล่านั้นก่อน โดยมีสแนปช็อตที่เผยแพร่รองรับอยู่ข้างหลัง

สิ่งที่การนำเข้าถาม

Agent Config ระบุโมเดลของเอเจนต์ ไม่ระบุอิมเมจที่แอปพลิเคชันรันอยู่เลย คุณบิลด์อิมเมจนั้นจากโปรเจกต์ของคุณ แค็ตตาล็อกไม่มีอิมเมจค่าเริ่มต้นสำหรับแอปพลิเคชันที่สร้างบน ADK การนำเข้าจึงถามหาอิมเมจนั้น ที่เทอร์มินัลการนำเข้าจะถามและเขียนคำตอบลงในร่างและลงในไฟล์คำตอบ import-answers.yaml ภายในไดเรกทอรีที่นำเข้า คือ support_agent/ การนำเข้าครั้งถัดไปจึงอ่านคำตอบจากที่นั่นและไม่ถามอะไร เมื่อไม่มีเทอร์มินัล เช่นในการรวมระบบต่อเนื่อง หรือผ่านไปป์ การนำเข้าไม่ถามอะไร แต่เขียนไฟล์โดยปล่อยคีย์ว่างแล้วหยุด พร้อมระบุชื่อไฟล์ ตัวอย่างมีไฟล์นี้อยู่แล้ว โดยตอบอิมเมจไว้เป็น 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 ซึ่งเอเจนต์อ่านคีย์ Gemini API จาก Secret ของ Kubernetes และคำตอบคือชื่อของ Secret นั้น google-api-key ไม่ใช่คีย์ บิลด์สำหรับ Docker Compose อ่านคีย์จาก shell ของคุณแทน การนำเข้านี้จึงไม่ใช้คำตอบนั้น ไฟล์คำตอบของการนำเข้า อธิบายไฟล์ คำถาม และกฎสำหรับคำตอบของข้อมูลลับ

aggrega import ระบุทุกแฟล็กที่คำสั่งรับ รวมถึง --answers ซึ่งระบุไฟล์คำตอบที่เก็บไว้ที่อื่น

เปลี่ยนเข้าโฟลเดอร์ source:

Terminal window
cd source

รันการนำเข้า

ระบุไดเรกทอรีของเอเจนต์ การนำเข้าจะอ่าน root_agent.yaml ของไดเรกทอรีนั้น:

Terminal window
aggrega import support_agent --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 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 applied
mapped support_agent (agent) to core/ai-agent/google-adk as a reference, NONE
mapped gemini-2.5-flash (model) to core/ai-model/gemini as a reference, NONE
made no choice for google_search (tool)
Pull complete: <workspace>/release.lock (entities=8, source_artifacts=0, external_inputs=0)

การนำเข้าที่รันจากตัวแก้ไข: Aggregate, Release และ Manifest ที่ร่างไว้ปรากฏข้างการกำหนดค่าของเอเจนต์ Aggrega output แสดงรายการวิธีที่เอเจนต์และโมเดลของเอเจนต์ถูกแมปและไฟล์ที่เขียน และการแจ้งเตือนระบุว่าการนำเข้าร่างไฟล์ 3 ไฟล์ โดยรู้จักเป็น google-adk

  • บรรทัด 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 การนำเข้าไม่ pull สิ่งใดและระบุ aggrega pull ที่ต้องรัน

สิ่งที่การนำเข้าเขียน

Aggregate, Release และ Manifest ตั้งชื่อว่า imported/support-agent ตาม name ของเอเจนต์ราก คือ support_agent ที่ย่อเป็น support-agent ภายใต้เนมสเปซที่การตั้งค่า target_namespace ของตัวนำเข้ากำหนด คือ imported เว้นแต่คุณตั้งค่าอื่น (การตั้งค่า)

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/v1beta1
kind: Aggregate
metadata:
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-2.5-flash ผ่านคลาส Gemini ซึ่งส่งคำขอไปยัง Gemini API ร่างจึงเชื่อมโยงโมเดลกับซ็อกเก็ต model ของเอเจนต์ ซึ่งรับเฉพาะ Gemini API เท่านั้น Gemini เป็นบริการที่โฮสต์ไว้ Component ของเอเจนต์จึงปฏิเสธการเชื่อมต่อไปยัง Gemini ที่ไม่มีข้อมูลรับรอง การเชื่อมต่อจึงประกาศข้อมูลรับรองไว้ใต้คีย์ GOOGLE_API_KEY โมเดลที่ ADK ให้บริการด้วยวิธีอื่นใด เช่นชื่ออย่าง gpt-4o ที่ ADK ให้บริการผ่านคลาสของตนเอง หรือสตริงโมเดล LiteLLM อย่าง openai/gpt-4o จะไม่เชื่อมโยงกับสิ่งใดเลย: ความคิดเห็นเหนืออินสแตนซ์ระบุชื่อโมเดลนั้น และปล่อยการเชื่อมต่อไว้ให้คุณจัดการ ตามที่ สิ่งที่ต้นไม้เอเจนต์กลายเป็น อธิบายไว้

release.yaml — สภาพแวดล้อม อิมเมจ พอร์ตที่เผยแพร่ และที่มาของคีย์:

apiVersion: aggrega.ai/v1beta1
kind: Release
metadata:
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 ประกอบด้วย Recipe สำหรับ Compose ของแต่ละชนิด และการทำให้เป็นรูปธรรมที่เขียนการอ้างอิงตัวแปรของคีย์ลงในไฟล์ Compose ตามที่เขียนไว้:

apiVersion: aggrega.ai/v1beta1
kind: Manifest
metadata:
name: imported/support-agent
version: 0.1.0
spec:
mappings:
components:
type:
core/ai-agent/google-adk: compose/google-adk
core/ai-model/gemini: compose/gemini
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 output และการแจ้งเตือนต่างรายงานว่าไม่พบปัญหา

บิลด์ของ Release ที่ร่างไว้: โฟลเดอร์ dist ปรากฏใน Explorer โดยเอาต์พุตของ Aggrega แสดงรายการไฟล์ Docker Compose ที่เขียน และการแจ้งเตือนระบุว่าเขียนไฟล์แล้ว

การตรวจสอบความถูกต้องพิมพ์:

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 ของข้อมูลลับรับ origin จาก values ของ Aggregate หรือ infra ของ Release และการแจ้งให้ทราบรายงานว่ามีค่าระดับใบหนึ่งค่าที่ประกาศไว้ทั้งสองที่ ซึ่งไม่เปลี่ยนคำตัดสิน

บริการของเอเจนต์ใน 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 ซึ่งไคลเอนต์ Gemini ของ ADK ส่งไปยัง Gemini API โมเดลไม่มีบริการของตัวเอง: ไคลเอนต์ส่งคำขอไปยังที่อยู่ของ Gemini API เอง และไม่อ่านที่อยู่อื่นจากสภาพแวดล้อมของตน คีย์เป็นการอ้างอิงตัวแปรที่เติมจาก shell ของคุณเมื่อสแตกเริ่มทำงาน Recipe สำหรับ Compose ของเอเจนต์ ADK เผยแพร่พอร์ตบนเครื่องของคุณเฉพาะเมื่อ Release ตั้ง published_port ใน infra ของเอเจนต์ และเผยแพร่บนที่อยู่ลูปแบ็ก 127.0.0.1 เท่านั้น หากไม่ตั้งค่านั้น จะเข้าถึงบริการได้บนเครือข่ายของสแตกเท่านั้น

กับผู้ช่วย AI

ผู้ช่วย AI ที่ ตั้งค่าด้วยเครื่องมือของ Aggrega ทำสิ่งเดียวกันได้: วางพรอมต์นี้ลงไป

ใช้เฉพาะเครื่องมือของ 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`: เครื่องมืออ่านคำตอบที่ต้องใช้จาก `import-answers.yaml` ในโฟลเดอร์ `support_agent` เขียนร่างลงใน `import-google-adk/source` และ pull สิ่งที่ร่างเหล่านั้นต้องใช้ จากนั้นเรียกเครื่องมือ `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 สำหรับ Visual Studio Code แล้ว เปิดโฟลเดอร์ import-google-adk/source ใน VS Code ซึ่งเป็นที่ที่การนำเข้าเขียนร่างไว้ โฟลเดอร์มี Release หนึ่งรายการคือ release.yaml ที่ร่างไว้ ตัวแก้ไขจึงเลือก Release นั้นให้คุณ และตรวจสอบร่างเทียบกับ Release นั้นขณะที่คุณแก้ไข โดยทำเครื่องหมายสิ่งที่พบในไฟล์และในแผง Problems Aggrega: Show topology graph ใน Command Palette วาดส่วนประกอบของ Release และการเชื่อมต่อระหว่างส่วนประกอบเหล่านั้น Aggrega: Build, Aggrega: Preview build และ Aggrega: Pull ทำงานกับ Release เดียวกัน และแต่ละคำสั่งยังเป็นปุ่มบนแผง Commands ของมุมมอง Aggrega ในแถบกิจกรรม Build เขียนไฟล์ลงใน dist/ ของโฟลเดอร์ ใช้ Aggrega ใน Visual Studio Code ครอบคลุมส่วนที่เหลือ

วิธีรัน

Docker Compose รันอิมเมจของคุณเป็นบริการที่เรนเดอร์ไว้ด้านบน รวมถึง PORT อิมเมจต้องรับฟังที่พอร์ตนั้นบนทุกอินเทอร์เฟซ ไม่ใช่เฉพาะ 127.0.0.1 ซึ่งเป็นที่อยู่เริ่มต้นของเซิร์ฟเวอร์ API ของ ADK เซิร์ฟเวอร์ API ของ ADK เอง คือ adk api_server ให้บริการไดเรกทอรีการกำหนดค่าตามที่เป็นอยู่ อิมเมจจึงไม่ต้องใช้โค้ดของคุณ ไฟล์ Dockerfile ที่รากของโปรเจกต์ ข้าง support_agent/:

FROM python:3.12-slim
RUN pip install --no-cache-dir google-adk==2.13.0
COPY 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 ระบุเซสชันนั้น คำขอเพียงรายการเดียวจึงไปถึงเอเจนต์ได้ บิลด์อิมเมจ เขียนชื่อของอิมเมจแทน ghcr.io/example/support-agent:1.0.0 ใน support_agent/import-answers.yaml และนำเข้าอีกครั้งด้วย --sync ซึ่งเขียนคำตอบใหม่ลงในร่างและ pull อีกครั้ง จากนั้นบิลด์:

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

จากนั้นตั้งคีย์ใน shell ของคุณและเริ่มสแตก:

Terminal window
export GOOGLE_API_KEY='your Gemini API key'
docker compose -p support-agent -f dist/compose/docker-compose.yaml up -d --wait

-p ระบุชื่อโปรเจกต์ ตามที่ขั้นตอนรันใน วิธีที่การนำเข้าทำงาน อธิบายไว้ --wait คืนการทำงานทันทีที่คอนเทนเนอร์รัน เพราะบิลด์ไม่กำหนดการตรวจสอบสถานะให้บริการ และเซิร์ฟเวอร์ตอบกลับในอีกครู่ต่อมา จึงส่งคำขอซ้ำถ้าเซิร์ฟเวอร์ปฏิเสธ

ทดสอบ

ส่งข้อความถึงเอเจนต์ที่ http://127.0.0.1:8000 ซึ่งเป็นที่อยู่ที่ published_port เผยแพร่ คำขอระบุผู้ใช้และเซสชัน และ /run เปิดเซสชันเมื่อได้รับข้อความแรก:

Terminal window
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"] ซึ่งเป็นเอเจนต์ที่เซิร์ฟเวอร์มี

หยุดสแตกด้วย:

Terminal window
docker compose -p support-agent -f dist/compose/docker-compose.yaml down

บน Kubernetes

เอเจนต์เดียวกันนำเข้าสำหรับ Kubernetes ได้ด้วย --target helm คลัสเตอร์ pull อิมเมจจากรีจิสทรี ดังนั้น push อิมเมจที่คุณบิลด์ไปยังรีจิสทรีที่คลัสเตอร์ของคุณอ่านได้ และเขียนชื่ออิมเมจแทนอิมเมจที่ตอบไว้ใน support_agent/import-answers.yaml การรันนี้ push อิมเมจไปยังรีจิสทรีบนเครื่องของคลัสเตอร์เอง เป็น localhost:5000/support-agent:1.0.0 คำตอบที่สองในไฟล์นั้นระบุชื่อ Secret ที่เอเจนต์อ่านคีย์ Gemini API จาก คือ google-api-key ร่างสำหรับ Docker Compose อยู่ใน source แล้ว จึงร่างชุดนี้ลงในโฟลเดอร์ของตัวเอง และบิลด์จากโฟลเดอร์นั้น:

Terminal window
aggrega import support_agent --target helm --output-dir helm
cd helm
aggrega build --release release.yaml

บิลด์เขียนแมนิเฟสต์ Kubernetes แบบธรรมดาไว้ใต้ dist/helm/ ที่รูทของพื้นที่ทำงาน คือ source ซึ่งจาก helm คือ ../dist/helm/ สร้าง Secret google-api-key ในเนมสเปซที่คุณนำไปใช้ โดยเก็บคีย์ไว้ใต้คีย์ข้อมูล value และนำโฟลเดอร์ไปใช้ตามที่ขั้นตอนรันใน วิธีที่การนำเข้าทำงาน อธิบายไว้ Service ของเอเจนต์คือ support-agent-default: ส่งต่อพอร์ตภายในเครื่องไปยัง Service นั้น และปล่อยให้คำสั่งรันต่อไป:

Terminal window
kubectl port-forward -n <namespace> svc/support-agent-default 8000:8000

จากเทอร์มินัลอื่น ข้อความข้างต้นไปถึงเอเจนต์ที่ที่อยู่เดิมโดยไม่ต้องแก้ไข การรันได้รับ Internal Server Error เช่นเดียวกัน และ kubectl logs -n <namespace> deploy/support-agent ระบุการปฏิเสธคีย์ตัวแทนเดียวกัน