Claude
Supported in ADKPython v0.1.0Java v0.2.0
你可以在 Python 和 Java 中使用 Anthropic 的 Claude 模型与 ADK 配合工作。请在下方选择与你的语言和后端匹配的路径。
Python¶
你可以在 Python 中通过以下方式使用 Claude 模型:
- Native, on Agent Platform: Pass a Claude model string directly; ADK's
registry routes it to the
Claudewrapper. See Anthropic Claude on Agent Platform. - Direct Anthropic API, via LiteLLM: Use the
LiteLlmconnector with an Anthropic API key. See LiteLLM.
Java¶
在 Java 中,你可以使用 Anthropic API 密钥直接集成 Claude 模型,也可以使用 ADK 的 Claude 包装器类配合 Agent Platform 后端。你还可以通过 Google Cloud Agent Platform 服务访问 Claude;参见 Third-Party
Models on Agent Platform。
快速开始¶
以下代码示例展示了在你的智能体中使用 Claude 模型的基本实现:
public static LlmAgent createAgent() {
AnthropicClient anthropicClient = AnthropicOkHttpClient.builder()
.apiKey("ANTHROPIC_API_KEY")
.build();
Claude claudeModel = new Claude(
"claude-sonnet-4-6", anthropicClient
);
return LlmAgent.builder()
.name("claude_direct_agent")
.model(claudeModel)
.instruction("你是一个由 Anthropic Claude 驱动的得力 AI 助手。")
.build();
}
前提条件¶
- 依赖项: Java ADK 的
com.google.adk.models.Claude包装器依赖于 Anthropic 官方 Java SDK 中的类,这些类通常作为传递依赖包含。更多信息请参见 Anthropic Java SDK。 - Anthropic API 密钥: 从 Anthropic 获取 API 密钥,并使用密钥管理器安全地管理它。
示例实现¶
实例化 com.google.adk.models.Claude,提供所需的 Claude 模型名称和使用你的 API 密钥配置的 AnthropicOkHttpClient。然后,将 Claude 实例传递给你的 LlmAgent,如下例所示:
import com.anthropic.client.AnthropicClient;
import com.google.adk.agents.LlmAgent;
import com.google.adk.models.Claude;
import com.anthropic.client.okhttp.AnthropicOkHttpClient; // 来自 Anthropic SDK
public class DirectAnthropicAgent {
private static final String CLAUDE_MODEL_ID = "claude-sonnet-4-6"; // 或你首选的 Claude 模型
public static LlmAgent createAgent() {
// 建议从安全配置中加载敏感密钥
AnthropicClient anthropicClient = AnthropicOkHttpClient.builder()
.apiKey("ANTHROPIC_API_KEY")
.build();
Claude claudeModel = new Claude(
CLAUDE_MODEL_ID,
anthropicClient
);
return LlmAgent.builder()
.name("claude_direct_agent")
.model(claudeModel)
.instruction("你是一个由 Anthropic Claude 驱动的得力 AI 助手。")
// ... 其他 LlmAgent 配置
.build();
}
public static void main(String[] args) {
try {
LlmAgent agent = createAgent();
System.out.println("成功创建 Anthropic 直连智能体:" + agent.name());
} catch (IllegalStateException e) {
System.err.println("创建智能体时出错:" + e.getMessage());
}
}
}