Claude models for ADK agents¶
ADK 智能体的 Claude 模型¶
You can integrate Anthropic's Claude models directly using an Anthropic API key
or from a Vertex AI backend into your Java ADK applications by using the ADK's
Claude wrapper class. You can also access Anthropic models through
Google Cloud Vertex AI services. For more information, see the
Third-Party Models on Vertex AI
section. You can also use Anthropic models through the
LiteLLM library for Python.
您可以使用 Anthropic API 密钥直接将 Anthropic 的 Claude 模型集成,或通过 Vertex AI 后端将其集成到 Java ADK 应用程序中,方法是使用 ADK 的 Claude 包装类。您还可以通过 Google Cloud Vertex AI 服务访问 Anthropic 模型。有关更多信息,请参阅 Vertex AI 上的第三方模型 部分。您还可以通过 Python 的 LiteLLM 库使用 Anthropic 模型。
Get started¶
开始使用¶
The following code examples show a basic implementation for using Gemini models in your agents:
以下代码示例展示了在智能体中使用 Gemini 模型的基本实现:
public static LlmAgent createAgent() {
AnthropicClient anthropicClient = AnthropicOkHttpClient.builder()
.apiKey("ANTHROPIC_API_KEY")
.build();
Claude claudeModel = new Claude(
"claude-3-7-sonnet-latest", anthropicClient
);
return LlmAgent.builder()
.name("claude_direct_agent")
.model(claudeModel)
.instruction("You are a helpful AI assistant powered by Anthropic Claude.")
.build();
}
Prerequisites¶
先决条件¶
-
Dependencies:
- Anthropic SDK Classes (Transitive): The Java ADK's
com.google.adk.models.Claudewrapper relies on classes from Anthropic's official Java SDK. These are typically included as transitive dependencies. For more information, see the Anthropic Java SDK.
- Anthropic SDK Classes (Transitive): The Java ADK's
-
依赖项:
- Anthropic SDK 类 (传递): Java ADK 的
com.google.adk.models.Claude包装器依赖于 Anthropic 官方 Java SDK 中的类。这些通常作为传递依赖项包含在内。有关更多信息,请参阅 Anthropic Java SDK。
- Anthropic SDK 类 (传递): Java ADK 的
-
Anthropic API Key:
- Obtain an API key from Anthropic. Securely manage this key using a secret manager.
-
Anthropic API 密钥:
- 从 Anthropic 获取 API 密钥。使用密钥管理器安全管理此密钥。
Example implementation¶
示例实现¶
Instantiate com.google.adk.models.Claude, providing the desired Claude model name and
an AnthropicOkHttpClient configured with your API key. Then, pass the Claude instance
to your LlmAgent, as shown in the following example:
实例化 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; // From Anthropic's SDK
public class DirectAnthropicAgent {
private static final String CLAUDE_MODEL_ID = "claude-3-7-sonnet-latest"; // Or your preferred Claude model
public static LlmAgent createAgent() {
// It's recommended to load sensitive keys from a secure config
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("You are a helpful AI assistant powered by Anthropic Claude.")
// ... other LlmAgent configurations
.build();
}
public static void main(String[] args) {
try {
LlmAgent agent = createAgent();
System.out.println("Successfully created direct Anthropic agent: " + agent.name());
} catch (IllegalStateException e) {
System.err.println("Error creating agent: " + e.getMessage());
}
}
}