Skip to content

ADK 智能体的 Agent Platform 托管模型

为了实现企业级的可扩展性、可靠性以及与 Google Cloud MLOps 生态系统的集成, 你可以使用部署到 Agent Platform 端点的模型。 这包括来自 Model Garden 的模型或你自己的微调模型。

集成方式: 将完整的 Agent Platform 端点资源字符串 (projects/PROJECT_ID/locations/LOCATION/endpoints/ENDPOINT_ID)直接传递给 LlmAgentmodel 参数。

Agent Platform 设置

有关将 ADK 智能体连接到 Google Cloud 托管模型和服务的更多详情, 包括 Gemini Enterprise Agent Platform,请参阅 连接到 Google Cloud 和 Agent Platform 指南。

Model Garden 部署

Supported in ADKPython v0.2.0Java v0.1.0

你可以从 Model Garden 部署各种开源和专有模型到端点。

示例:

from google.adk.agents import LlmAgent
from google.genai import types # 用于配置对象

# --- 使用从 Model Garden 部署的 Llama 3 模型的示例智能体 ---

# 替换为你的实际 Agent Platform 端点资源名称
llama3_endpoint = "projects/YOUR_PROJECT_ID/locations/us-central1/endpoints/YOUR_LLAMA3_ENDPOINT_ID"

agent_llama3_vertex = LlmAgent(
    model=llama3_endpoint,
    name="llama3_vertex_agent",
    instruction="You are a helpful assistant based on Llama 3, hosted on Agent Platform.",
    generate_content_config=types.GenerateContentConfig(max_output_tokens=2048),
    # ... 其他智能体参数
)
import com.google.adk.agents.LlmAgent;
import com.google.adk.models.Gemini;
import com.google.genai.types.GenerateContentConfig;

// ...

// 替换为你的实际 Agent Platform 端点资源名称
String llama3Endpoint = "projects/YOUR_PROJECT_ID/locations/us-central1/endpoints/YOUR_LLAMA3_ENDPOINT_ID";

LlmAgent agentLlama3Vertex = LlmAgent.builder()
    .model(Gemini.builder()
        .modelName(llama3Endpoint)
        .build())
    .name("llama3_vertex_agent")
    .instruction("You are a helpful assistant based on Llama 3, hosted on Agent Platform.")
    .generateContentConfig(GenerateContentConfig.builder()
        .maxOutputTokens(2048)
        .build())
    // ... 其他智能体参数
    .build();

微调模型端点

Supported in ADKPython v0.2.0Java v0.1.0

部署你的微调模型(无论是基于 Gemini 还是 Agent Platform 支持的其他架构) 会生成一个可以直接使用的端点。

示例:

from google.adk.agents import LlmAgent

# --- 使用微调 Gemini 模型端点的示例智能体 ---

# 替换为你的微调模型端点资源名称
finetuned_gemini_endpoint = "projects/YOUR_PROJECT_ID/locations/us-central1/endpoints/YOUR_FINETUNED_ENDPOINT_ID"

agent_finetuned_gemini = LlmAgent(
    model=finetuned_gemini_endpoint,
    name="finetuned_gemini_agent",
    instruction="You are a specialized assistant trained on specific data.",
    # ... 其他智能体参数
)
import com.google.adk.agents.LlmAgent;
import com.google.adk.models.Gemini;

// ...

// 替换为你的微调模型端点资源名称
String finetunedGeminiEndpoint = "projects/YOUR_PROJECT_ID/locations/us-central1/endpoints/YOUR_FINETUNED_ENDPOINT_ID";

LlmAgent agentFinetunedGemini = LlmAgent.builder()
    .model(Gemini.builder()
        .modelName(finetunedGeminiEndpoint)
        .build())
    .name("finetuned_gemini_agent")
    .instruction("You are a specialized assistant trained on specific data.")
    // ... 其他智能体参数
    .build();

Agent Platform 上的 Anthropic Claude

Supported in ADKPython v0.2.0Java v0.1.0

一些提供商(如 Anthropic)直接通过 Agent Platform 提供其模型。

示例:

Integration Method: Uses the direct model string (e.g., "claude-3-sonnet@20240229").

How Resolution Works: ADK's registry automatically recognizes gemini-* strings and standard Agent Platform endpoint strings (projects/.../locations/.../endpoints/...) and routes them via the google-genai library. Claude model strings matching claude-3-* or claude-*-4* route to the Claude wrapper class the same way. For a Claude model identifier that does not match those patterns, import Claude from google.adk.models and pass an instance instead of a string: LlmAgent(model=Claude(model="..."), ...).

设置:

  1. Agent Platform 环境: 确保完成统一的 Agent Platform 设置(ADC、环境变量、 GOOGLE_GENAI_USE_ENTERPRISE=TRUE)。

  2. 安装提供商库: 安装为 Agent Platform 配置的 必要客户端库。

    pip install "anthropic[vertex]"
    
  3. Create the Agent: Pass the Claude model string to LlmAgent:

from google.adk.agents import LlmAgent
from google.genai import types

# --- Example Agent using Claude 3 Sonnet on Agent Platform ---

# Agent Platform 上 Claude 3 Sonnet 的标准模型名称
claude_model_vertexai = "claude-3-sonnet@20240229"

agent_claude_vertexai = LlmAgent(
    model=claude_model_vertexai, # Pass the direct model string
    name="claude_vertexai_agent",
    instruction="You are an assistant powered by Claude 3 Sonnet on Agent Platform.",
    generate_content_config=types.GenerateContentConfig(max_output_tokens=4096),
    # ... 其他智能体参数
)

集成方式: 直接实例化提供商特定的模型类(例如 com.google.adk.models.Claude)并配置 Agent Platform 后端。

为什么要直接实例化? Java ADK 的 LlmRegistry 默认主要处理 Gemini 模型。对于 Agent Platform 上的第三方模型(如 Claude),你需要直接向 LlmAgent 提供 ADK 包装类(例如 Claude)的实例。此包装类负责通过其特定的客户端库与模型交互,并配置为使用 Agent Platform。

设置:

  1. Agent Platform 环境:

    • 确保你的 Google Cloud 项目和区域已正确设置。
    • 应用默认凭据(ADC): 确保你的环境中正确配置了 ADC。通常通过运行 gcloud auth application-default login 来完成。Java 客户端库使用这些凭据对 Agent Platform 进行身份验证。请参阅 Google Cloud Java ADC 文档 了解详细设置。
  2. 提供商库依赖:

    • 第三方客户端库(通常是传递依赖): ADK 核心库通常将 Agent Platform 上常见第三方模型(如 Anthropic 所需的类)的必要客户端库作为传递依赖包含在内。这意味着你可能不需要在 pom.xmlbuild.gradle 中显式添加 Anthropic Vertex SDK 的单独依赖。
  3. 实例化并配置模型: 创建 LlmAgent 时,实例化 Claude 类(或其他提供商的等效类)并配置其 VertexBackend

import com.anthropic.client.AnthropicClient;
import com.anthropic.client.okhttp.AnthropicOkHttpClient;
import com.anthropic.vertex.backends.VertexBackend;
import com.google.adk.agents.LlmAgent;
import com.google.adk.models.Claude; // ADK 的 Claude 包装类
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;

// ... 其他导入

public class ClaudeVertexAiAgent {

    public static LlmAgent createAgent() throws IOException {
        // Agent Platform 上 Claude 3 Sonnet 的模型名称(或其他版本)
        String claudeModelVertexAi = "claude-3-7-sonnet"; // 或任何其他 Claude 模型

        // 使用 VertexBackend 配置 AnthropicOkHttpClient
        AnthropicClient anthropicClient = AnthropicOkHttpClient.builder()
            .backend(
                VertexBackend.builder()
                    .region("us-east5") // 指定你的 Agent Platform 区域
                    .project("your-gcp-project-id") // 指定你的 GCP 项目 ID
                    .googleCredentials(GoogleCredentials.getApplicationDefault())
                    .build())
            .build();

        // 使用 ADK Claude 包装类实例化 LlmAgent
        LlmAgent agentClaudeVertexAi = LlmAgent.builder()
            .model(new Claude(claudeModelVertexAi, anthropicClient)) // 传递 Claude 实例
            .name("claude_vertexai_agent")
            .instruction("You are an assistant powered by Claude 3 Sonnet on Agent Platform.")
            // .generateContentConfig(...) // 可选:如果需要可添加生成配置
            // ... 其他智能体参数
            .build();

        return agentClaudeVertexAi;
    }

    public static void main(String[] args) {
        try {
            LlmAgent agent = createAgent();
            System.out.println("Successfully created agent: " + agent.name());
            // 通常在这里设置 Runner 和 Session 来与智能体交互
        } catch (IOException e) {
            System.err.println("Failed to create agent: " + e.getMessage());
            e.printStackTrace();
        }
    }
}

自适应思考

Supported in ADKPython v1.34.0

较新的 Claude 模型支持自适应扩展思考,模型会自行选择推理深度,而不是使用固定的 token 预算。在原生 Claude 路径上,负的 thinking_budget 会映射为自适应思考。

控制推理深度的推荐方式是使用 AnthropicGenerateContentConfig 上的 effort 字段:

from google.adk.agents import LlmAgent
from google.adk.models import AnthropicGenerateContentConfig

agent = LlmAgent(
    model="claude-sonnet-4@20250514",  # 你的 Agent Platform Claude 模型 ID。
    name="claude_reasoning_agent",
    instruction="You are a helpful assistant.",
    generate_content_config=AnthropicGenerateContentConfig(
        effort="high",  # 可选值:"low"、"medium"、"high"、"xhigh"、"max"。
    ),
)
  • The standard thinking_config.thinking_level is not supported for Claude. Setting it on AnthropicGenerateContentConfig raises a validation error; on a plain types.GenerateContentConfig it is ignored with a warning. Use effort instead.

Agent Platform 上的开放模型

Supported in ADKPython v0.1.0Java v0.1.0

Agent Platform 通过模型即服务(MaaS)提供精选的开源模型选择,如 Meta Llama。这些模型可通过托管 API 访问,使你无需管理底层基础设施即可部署和扩展。有关可用选项的完整列表,请参阅 Agent Platform 开放模型 MaaS 文档。

你可以使用 LiteLLM 库来访问 Agent Platform MaaS 上的开放模型,如 Meta 的 Llama。

集成方式: 使用 LiteLlm 包装类并将其设置为 LlmAgentmodel 参数。请确保查阅 ADK 智能体的 LiteLLM 模型连接器 文档了解如何在 ADK 中使用 LiteLLM。

设置:

  1. Agent Platform 环境: 确保完成统一的 Agent Platform 设置(ADC、环境变量、 GOOGLE_GENAI_USE_ENTERPRISE=TRUE)。

  2. Install LiteLLM: ADK requires litellm>=1.84.

    pip install "litellm>=1.84"
    

示例:

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

# --- 使用 Meta 的 Llama 4 Scout 的示例智能体 ---
agent_llama_vertexai = LlmAgent(
    model=LiteLlm(model="vertex_ai/meta/llama-4-scout-17b-16e-instruct-maas"), # LiteLLM 模型字符串格式
    name="llama4_agent",
    instruction="You are a helpful assistant powered by Llama 4 Scout.",
    # ... 其他智能体参数
)