Vertex AI 托管的 ADK 智能体模型¶
For enterprise-grade scalability, reliability, and integration with Google Cloud's MLOps ecosystem, you can use models deployed to Vertex AI Endpoints. This includes models from Model Garden or your own fine-tuned models.
集成方法: 将完整的 Vertex AI Endpoint 资源字符串
(projects/PROJECT_ID/locations/LOCATION/endpoints/ENDPOINT_ID) 直接传递给
LlmAgent 的 model 参数。
Vertex AI 设置¶
Ensure your environment is configured for Vertex AI:
-
身份验证: 使用应用程序默认凭据 (ADC):
-
环境变量: 设置您的项目和位置:
-
启用 Vertex 后端: 关键是确保
google-genai库 指向 Vertex AI:
Model Garden 部署¶
You can deploy various open and proprietary models from the Vertex AI Model Garden to an endpoint.
示例:
from google.adk.agents import LlmAgent
from google.genai import types # For config objects
# --- 使用从 Model Garden 部署的 Llama 3 模型的智能体示例 ---
# 替换为您的实际 Vertex AI Endpoint 资源名称
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 Vertex AI.",
generate_content_config=types.GenerateContentConfig(max_output_tokens=2048),
# ... other agent parameters
)
微调模型端点¶
Deploying your fine-tuned models (whether based on Gemini or other architectures supported by Vertex AI) results in an endpoint that can be used directly.
示例:
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.",
# ... other agent parameters
)
Vertex AI 上的 Anthropic Claude¶
Some providers, like Anthropic, make their models available directly through Vertex AI.
集成方法: 使用直接的模型字符串 (例如,
"claude-3-sonnet@20240229"), 但需要在 ADK 中手动注册。
为什么需要注册? ADK 的注册表会自动识别 gemini-* 字符串
和标准的 Vertex AI 端点字符串 (projects/.../endpoints/...) 并
通过 google-genai 库路由它们。对于直接通过 Vertex AI 使用的其他模型类型
(如 Claude),您必须明确告诉 ADK 注册表哪个特定的包装器类
(在这种情况下是 Claude) 知道如何使用 Vertex AI 后端处理该模型标识符字符串。
设置:
-
Vertex AI 环境: 确保完整的 Vertex AI 设置 (ADC、环境 变量、
GOOGLE_GENAI_USE_VERTEXAI=TRUE) 已完成。 -
安装提供程序库: 安装为 Vertex AI 配置的必要客户端库。
-
注册模型类: 在应用程序开始附近添加此代码, 在使用 Claude 模型字符串创建智能体之前:
示例:
from google.adk.agents import LlmAgent
from google.adk.models.anthropic_llm import Claude # Import needed for registration
from google.adk.models.registry import LLMRegistry # Import needed for registration
from google.genai import types
# --- 注册 Claude 类 (在启动时执行一次) ---
LLMRegistry.register(Claude)
# --- 使用 Vertex AI 上的 Claude 3 Sonnet 的智能体示例 ---
# Vertex AI 上 Claude 3 Sonnet 的标准模型名称
claude_model_vertexai = "claude-3-sonnet@20240229"
agent_claude_vertexai = LlmAgent(
model=claude_model_vertexai, # 注册后传递直接字符串
name="claude_vertexai_agent",
instruction="You are an assistant powered by Claude 3 Sonnet on Vertex AI.",
generate_content_config=types.GenerateContentConfig(max_output_tokens=4096),
# ... other agent parameters
)
集成方法: 直接实例化提供程序特定的模型类 (例如, com.google.adk.models.Claude) 并使用 Vertex AI 后端进行配置。
为什么需要直接实例化? Java ADK 的 LlmRegistry 默认主要处理 Gemini 模型。对于 Vertex AI 上的第三方模型如 Claude,您直接向 LlmAgent 提供 ADK 包装器类的实例 (例如, Claude)。此包装器类负责通过其特定的客户端库与模型交互,为 Vertex AI 进行配置。
设置:
-
Vertex AI 环境:
- 确保您的 Google Cloud 项目和区域设置正确。
- 应用程序默认凭据 (ADC): 确保在您的环境中正确配置 ADC。这通常通过运行
gcloud auth application-default login完成。Java 客户端库将使用这些凭据与 Vertex AI 进行身份验证。按照有关 ADC 的 Google Cloud Java 文档 进行详细设置。
-
提供程序库依赖项:
- 第三方客户端库 (通常是传递的): ADK 核心库通常包含 Vertex AI 上常见第三方模型 (如 Anthropic 所需的类) 所需的客户端库作为传递依赖项。这意味着您可能不需要在
pom.xml或build.gradle中为 Anthropic Vertex SDK 添加单独的依赖项。
- 第三方客户端库 (通常是传递的): ADK 核心库通常包含 Vertex AI 上常见第三方模型 (如 Anthropic 所需的类) 所需的客户端库作为传递依赖项。这意味着您可能不需要在
-
实例化并配置模型: 创建
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's wrapper for Claude
import com.google.auth.oauth2.GoogleCredentials;
import java.io.IOException;
// ... other imports
public class ClaudeVertexAiAgent {
public static LlmAgent createAgent() throws IOException {
// Vertex AI 上 Claude 3 Sonnet 的模型名称 (或其他版本)
String claudeModelVertexAi = "claude-3-7-sonnet"; // Or any other Claude model
// 使用 VertexBackend 配置 AnthropicOkHttpClient
AnthropicClient anthropicClient = AnthropicOkHttpClient.builder()
.backend(
VertexBackend.builder()
.region("us-east5") // 指定您的 Vertex AI 区域
.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 Vertex AI.")
// .generateContentConfig(...) // 可选: 如果需要添加生成配置
// ... other agent parameters
.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();
}
}
}