使用 Google 的 ADK for TypeScript 实现大规模 Agent 流程

发布: (2025年12月21日 GMT+8 06:08)
6 min read
原文: Dev.to

Source: Dev.to

Agent Flows At Scale with Google’s ADK for TypeScript 的封面图片

代码优先革命

长期以来,构建 AI 代理一直像是抽象提示工程的练习。适用于 TypeScript 的代理开发套件(ADK) 通过允许开发者直接在 TypeScript 中定义逻辑、工具和编排,改变了这一范式。正如 Google 2025 年 12 月的公告 所强调的,这种方法使工程师能够将标准的软件开发最佳实践——如版本控制、自动化测试和 CI/CD 集成——应用到 AI 工作流中。

该框架提供端到端的类型安全,这意味着开发者可以在同一种语言中构建代理后端和应用前端,从而大幅降低集成错误。通过使用 AgentsInstructionsTools 等模块化组件,ADK 将复杂的 AI 行为转化为简洁、可读且可扩展的代码。

案例研究: “Chef & Sommelier” 多代理系统

Here 您可以找到完整项目;只需添加您的 Gemini API 密钥。

为了演示 ADK 的强大功能,让我们探索一个实际实现:层级化的 Chef Agent。该项目使用最新的 Gemini 3 模型 gemini-3-pro-preview,打造超越单纯配方生成的烹饪体验。

项目结构

chef-agent/
├── agent.ts               # The Head Chef
└── sommelier-agent/
    └── agent.ts           # The Wine Expert

分层架构

本应用的核心位于 chef-agent/agent.ts。在这里,根代理被定义为 Chef,其指令是接受单一食材输入并生成一款杰作菜肴,包含名称、描述、配方和摆盘说明。

ADK 的真正威力体现在它如何处理子代理。Chef 并非单独工作——代码显式定义了一个 subAgents 数组:

import { LlmAgent } from "@google/adk";
import { sommelierAgent } from "./sommelier-agent/agent";

export const rootAgent = new LlmAgent({
  name: "chef_agent",
  model: "gemini-3-pro-preview",
  description:
    "A chef that creates amazing food based on a single ingredient.",
  instruction: `You are a world‑renowned Chef with a passion for creating culinary masterpieces.
Your specialty is taking a SINGLE INGREDIENT provided by the user and designing a complete, delicious, and amazing dish around it.

When you receive an input (which will be an ingredient):
1. **Conceive a Dish**: Create a unique name for a dish highlighting that ingredient.
2. **Description**: Write a mouth‑watering description.
3. **Recipe**: Provide a detailed recipe including:
   * Ingredients list (quantities and items).
   * Step‑by‑step cooking instructions.
4. **Presentation**: Suggest how to plate the dish for maximum visual appeal.

Be enthusiastic, professional, and creative.
You also have a colleague, "sommelier_agent", who must suggest wine pairings for the dish you create.`,
  subAgents: [sommelierAgent],
});

该配置告知 Chef 代理它可以访问一位同事。当 Chef 生成配方时,它可以自动查询在 sommelier-agent/agent.ts 中定义的 sommelier_agent。该子代理的范围很窄:根据 Chef 刚刚创建的菜肴的风味特征,建议完美的葡萄酒搭配。

酒侍代理代码

import { LlmAgent } from "@google/adk";

export const instruction = `You are an expert Sommelier.
Your goal is to suggest the perfect wine pairing for a given dish.
When provided with a dish name or description:
1. Suggest a specific type of wine (e.g., Cabernet Sauvignon, Chardonnay).
2. Explain why it pairs well with the dish (flavor profile, acidity, etc.).
3. Recommend a specific region if applicable.`;

export const sommelierAgent = new LlmAgent({
  name: "sommelier_agent",
  model: "gemini-3-pro-preview",
  description: "A sommelier that suggests wine pairings for a given dish.",
  instruction,
});

开发者体验

该项目使用 @google/adk 库,提供了流畅的开发者体验。查看 package.json,我们可以看到内置的脚本使用了 ADK 开发工具:

  • 终端模式: pnpm run run:terminal 允许开发者直接在命令行中与 Chef 交互,以实现快速测试。

ADK 终端

Chef 代理图示

  • Web 界面: pnpm run run:web 启动本地服务器(localhost:8000),提供聊天界面以可视化用户、Chef 与隐藏的 Sommelier 子代理之间的交互。

ADK Web

Chef Agent 项目完美展示了为什么 TypeScript 版 ADK 是一个改变游戏规则的工具。它创建了一个结构化的环境,使不同的 AI 人格(Chef 和 Sommelier)通过类型化的合约而不是模糊的提示进行交互。

通过将新 Gemini 3 模型的推理能力与 TypeScript 的可靠性相结合,Google 的 ADK 为下一代软件奠定了基础——在这里,代码不仅执行指令,还能思考、创造和协作。

你可以在 GitHub 上关注我,我正在创建一些酷炫的项目。

希望你喜欢这篇文章。下次见 👋

gioboa 头像

Back to Blog

相关文章

阅读更多 »