Anthropic
Maven 依赖
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-anthropic</artifactId>
<version>1.18.1</version>
</dependency>
AnthropicChatModel
AnthropicChatModel model = AnthropicChatModel.builder()
.apiKey(System.getenv("ANTHROPIC_API_KEY"))
.modelName(CLAUDE_3_5_SONNET_20240620)
.build();
String answer = model.chat("Say 'Hello World'");
System.out.println(answer);
自定义 AnthropicChatModel
AnthropicChatModel model = AnthropicChatModel.builder()
.httpClientBuilder(...)
.baseUrl(...)
.apiKey(...)
.version(...)
.beta(...)
.modelName(...)
.temperature(...)
.topP(...)
.topK(...)
.maxTokens(...)
.stopSequences(...)
.toolSpecifications(...)
.toolChoice(...)
.toolChoiceName(...)
.disableParallelToolUse(...)
.serverTools(...)
.returnServerToolResults(...)
.toolMetadataKeysToSend(...)
.cacheSystemMessages(...)
.cacheTools(...)
.returnCacheDiagnostics(...)
.thinkingType(...)
.thinkingBudgetTokens(...)
.thinkingDisplay(...)
.returnThinking(...)
.sendThinking(...)
.midConversationSystemMessages(...)
.timeout(...)
.maxRetries(...)
.logRequests(...)
.logResponses(...)
.listeners(...)
// You can also specify default chat request parameters using ChatRequestParameters or AnthropicChatRequestParameters
.defaultRequestParameters(...)
.userId(...)
.customParameters(...)
.build();
上方部分参数的说明见 此处。
按请求参数
上文所示的 Anthropic 特有选项(cacheSystemMessages、cacheTools、returnCacheDiagnostics、
thinkingType、thinkingBudgetTokens、sendThinking、returnThinking、midConversationSystemMessages、
toolChoiceName、disableParallelToolUse 以及 userId),以及 previousMessageId(仅请求级,见
缓存诊断),
也可通过 AnthropicChatRequestParameters 按请求设置,从而覆盖模型构建器上配置的值。
这样可以用同一个共享模型实例在不同调用间改变这些选项——例如,
在长时间运行的智能体循环中启用提示缓存,而在廉价的一次性补全中跳过缓存,而无需
构建第二个模型:
AnthropicChatModel model = AnthropicChatModel.builder()
.apiKey(System.getenv("ANTHROPIC_API_KEY"))
.modelName(CLAUDE_3_5_SONNET_20240620)
.build();
AnthropicChatRequestParameters parameters = AnthropicChatRequestParameters.builder()
.cacheSystemMessages(true)
.cacheTools(true)
.build();
ChatRequest chatRequest = ChatRequest.builder()
.messages(systemMessage, userMessage)
.parameters(parameters)
.build();
ChatResponse chatResponse = model.chat(chatRequest);
请求上未设置的任何参数都会回退到模型构建器上配置的值。
AnthropicStreamingChatModel
AnthropicStreamingChatModel model = AnthropicStreamingChatModel.builder()
.apiKey(System.getenv("ANTHROPIC_API_KEY"))
.modelName(CLAUDE_3_5_SONNET_20240620)
.build();
model.chat("Say 'Hello World'", new StreamingChatResponseHandler() {
@Override
public void onPartialResponse(String partialResponse) {
// this method is called when a new partial response is available. It can consist of one or more tokens.
}
@Override
public void onCompleteResponse(ChatResponse completeResponse) {
// this method is called when the model has completed responding
}
@Override
public void onError(Throwable error) {
// this method is called when an error occurs
}
});
自定义 AnthropicStreamingChatModel
与 AnthropicChatModel 相同,见上文。
Batch API
Message Batches API 可异步处理大量聊天请求,
价格为标准按 token 计费的 50%。AnthropicBatchChatModel 实现了核心的 BatchChatModel
接口(submit、retrieve、cancel、list)。每个请求提交时使用的参数与
AnthropicChatModel 调用相同。
AnthropicBatchChatModel model = AnthropicBatchChatModel.builder()
.apiKey(System.getenv("ANTHROPIC_API_KEY"))
.modelName("claude-sonnet-4-5")
.maxTokens(1024)
.build();
// Submit a batch of requests
BatchResponse<ChatResponse> submitted = model.submit(new BatchRequest<>(List.of(
ChatRequest.builder().messages(UserMessage.from("What is the capital of France?")).build(),
ChatRequest.builder().messages(UserMessage.from("What is the capital of Germany?")).build())));
String batchId = submitted.batchId();
// Poll until the batch reaches a terminal state (typically well under an hour)
BatchResponse<ChatResponse> batch = model.retrieve(batchId);
while (!batch.state().isTerminal()) {
TimeUnit.SECONDS.sleep(30); // throws InterruptedException
batch = model.retrieve(batchId);
}
// Read the per-request results, in submission order
for (BatchItemResult<ChatResponse> result : batch.results()) {
if (result.isSuccess()) {
System.out.println(result.response().aiMessage().text());
} else {
System.out.println("Failed: " + result.error().message());
}
}
使用 model.list(...) 分页查看近期批次,使用 model.cancel(batchId) 取消仍在处理中的批次。
你取消的批次在 Anthropic 侧也会以 ended 状态结束,并报告为 BatchState.CANCELLED;
其中仍可能包含取消生效前已完成请求的结果。
thinking 或提示缓存等 Anthropic 特有选项通过 defaultRequestParameters(...) 配置,
方式与 AnthropicChatModel 完全相同,并可按请求覆盖:
AnthropicBatchChatModel model = AnthropicBatchChatModel.builder()
.apiKey(System.getenv("ANTHROPIC_API_KEY"))
.modelName("claude-sonnet-4-5")
.maxTokens(4096)
.defaultRequestParameters(AnthropicChatRequestParameters.builder()
.thinkingType("enabled")
.thinkingBudgetTokens(2000)
.cacheSystemMessages(true)
.build())
.returnThinking(true) // store the returned thinking in AiMessage.thinking()
.build();