第 14 章 Google ADKPythonTypeScript

附录 A ADK 2.0 五语言速查

附录 A ADK 2.0 五语言速查

ADK 是多语言 SDK,支持 Python、TypeScript、Go、Java、Kotlin 五门语言。本附录给出每种语言的最小可用示例,方便你在选型或迁移时快速对照。

A.1 语言现状

语言 状态 版本基线 工具链
Python 最成熟,主语言 2.0 GA 2026-05-19 adk CLI
TypeScript 2.0 GA 2026-08-21 v0.2.0+ npx adk
Go 2.0 GA 2026-06-30 v2.0.0 内嵌 launcher + adkgo
Java 支持 v1.6.0 Maven/Gradle
Kotlin 较新/实验性 v0.1.0 Gradle

A.2 创建一个最简 Agent

Python

from google.adk.agents import Agent

root_agent = Agent(
    model="gemini-flash-latest",
    name="hello_agent",
    description="A simple hello agent.",
    instruction="You are a helpful assistant.",
)

TypeScript

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

const rootAgent = new LlmAgent({
  name: "hello_agent",
  model: "gemini-flash-latest",
  description: "A simple hello agent.",
  instruction: "You are a helpful assistant.",
});

Go

package main

import (
    "context"
    "google.golang.org/adk/v2/agent/llmagent"
    "google.golang.org/adk/v2/model/geminimodel"
)

func main() {
    llm, _ := geminimodel.NewModel(context.Background(), geminimodel.GeminiFlashLatest)
    agent, _ := llmagent.New(llmagent.Config{
        Name:        "hello_agent",
        Model:       llm,
        Instruction: "You are a helpful assistant.",
    })
    _ = agent
}

Java

import com.google.adk.agents.LlmAgent;

LlmAgent rootAgent = LlmAgent.builder()
    .name("hello_agent")
    .model("gemini-flash-latest")
    .instruction("You are a helpful assistant.")
    .build();

Kotlin

import com.google.adk.agents.Agent

val rootAgent = Agent(
    name = "hello_agent",
    model = "gemini-flash-latest",
    instruction = "You are a helpful assistant."
)

A.3 注册一个工具

Python

from google.adk import Agent
from google.adk.tools import FunctionTool

def get_weather(city: str) -> dict:
    """Returns the current weather for a city."""
    return {"status": "success", "city": city, "weather": "sunny"}

root_agent = Agent(
    model="gemini-flash-latest",
    name="weather_agent",
    tools=[FunctionTool(func=get_weather)],
)

TypeScript

import { FunctionTool } from "@google/adk";
import { z } from "zod";

const weatherTool = new FunctionTool({
  name: "get_weather",
  description: "Returns the current weather for a city.",
  parameters: z.object({
    city: z.string().describe("The city name"),
  }),
  execute: async ({ city }) => ({ status: "success", city, weather: "sunny" }),
});

Go

import "google.golang.org/adk/v2/tools/functiontool"

type WeatherArgs struct {
    City string `json:"city"`
}

tool, _ := functiontool.New(functiontool.Config{
    Name:        "get_weather",
    Description: "Returns the current weather for a city.",
}, func(ctx context.Context, args WeatherArgs) (any, error) {
    return map[string]any{"status": "success", "city": args.City, "weather": "sunny"}, nil
})

A.4 多智能体(协调者 + 子 Agent)

Python

from google.adk import Agent

order_agent = Agent(
    name="order_agent",
    mode="task",
    tools=[get_order_status],
)

root_agent = Agent(
    name="yunxiao_cs",
    model="gemini-flash-latest",
    sub_agents=[order_agent],
)

TypeScript

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

const orderAgent = new LlmAgent({
  name: "order_agent",
  tools: [getOrderStatus],
});

const rootAgent = new LlmAgent({
  name: "yunxiao_cs",
  model: "gemini-flash-latest",
  subAgents: [orderAgent],
});

A.5 图工作流

Python

from google.adk import Agent, Workflow, Event

def step_a(node_input: str) -> str:
    return node_input.upper()

def done(node_input: str) -> Event:
    return Event(message=f"Done: {node_input}")

root_agent = Workflow(
    name="my_workflow",
    edges=[("START", step_a, done)],
)

TypeScript

import { Workflow, Event } from "@google/adk";

const stepA = ({ nodeInput }: any) => nodeInput.toUpperCase();
const done = ({ nodeInput }: any) => new Event({ message: `Done: ${nodeInput}` });

const rootAgent = new Workflow({
  name: "my_workflow",
  edges: [["START", stepA, done]],
});

A.6 模型配置(不绑 Google)

Python:LiteLlm 接任意模型

from google.adk.agents import LlmAgent
from google.adk.models.lite_llm import LiteLlm

agent = LlmAgent(
    model=LiteLlm(model="openai/gpt-4o"),
    name="openai_agent",
    instruction="You are a helpful assistant.",
)

Python:Ollama 本地

from google.adk.models.lite_llm import LiteLlm

agent = Agent(
    model=LiteLlm(model="ollama_chat/gemma3:latest"),
    name="local_agent",
    instruction="You are a helpful assistant.",
)

A.7 运行方式速查

操作 Python TypeScript Go
命令行交互 adk run agent npx adk run agent.ts go run agent.go
Web 界面 adk web npx adk web go run agent.go web
API Server adk api_server npx adk api_server go run agent.go web api
部署 Cloud Run adk deploy cloud_run ... npx adk deploy cloud_run ... adkgo deploy cloudrun ...

提示:本书所有代码均以 Python 为例(它是 ADK 最成熟的语言)。其他语言的 API 与 Python 一一对应,只是语法不同。官方文档提供完整的各语言参考(见附录 B)。