Anthropic
Maven 依赖
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-anthropic</artifactId>
<version>1.3.0</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(...)
.cacheSystemMessages(...)
.cacheTools(...)
.thinkingType(...)
.thinkingBudgetTokens(...)
.returnThinking(...)
.sendThinking(...)
.timeout(...)
.maxRetries(...)
.logRequests(...)
.logResponses(...)
.listeners(...)
.defaultRequestParameters(...)
.build();
上述部分参数的说明见 这里。
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) {
// 当有新的部分响应可用时调用该方法。它可能包含一个或多个 token。
}
@Override
public void onCompleteResponse(ChatResponse completeResponse) {
// 当模型完成响应时调用该方法
}
@Override
public void onError(Throwable error) {
// 当发生错误时调用该方法
}
});
自定义 AnthropicStreamingChatModel
与 AnthropicChatModel
相同,见上文。
工具
Anthropic 在流式和非流式模式下均支持 工具。
Anthropic 关于工具的文档请见 这里。
缓存
AnthropicChatModel
和 AnthropicStreamingChatModel
支持缓存系统消息和工具。
默认情况下缓存是禁用的。
可以通过设置 cacheSystemMessages
和 cacheTools
参数来启用。
启用后,cache_control
块将分别添加到最后一个系统消息和工具中。
要使用缓存,请设置 beta("prompt-caching-2024-07-31")
。
AnthropicChatModel
和 AnthropicStreamingChatModel
在响应中返回 AnthropicTokenUsage
,其中包含 cacheCreationInputTokens
和 cacheReadInputTokens
。
更多关于缓存的信息请见 这里。
思考 (Thinking)
AnthropicChatModel
和 AnthropicStreamingChatModel
都支持 思考 (thinking) 功能。
它由以下参数控制:
thinkingType
和thinkingBudgetTokens
:启用思考功能,详情见 这里。returnThinking
:控制是否在AiMessage.thinking()
中返回思考内容,以及在使用BedrockStreamingChatModel
时,是否调用StreamingChatResponseHandler.onPartialThinking()
和TokenStream.onPartialThinking()
回调。
默认禁用。若启用,思考签名也会存储并返回在AiMessage.attributes()
中。sendThinking
:控制是否在后续请求中,将AiMessage
中存储的思考和签名发送给 LLM。默认启用。
以下是配置思考功能的示例:
ChatModel model = AnthropicChatModel.builder()
.apiKey(System.getenv("ANTHROPIC_API_KEY"))
.modelName(CLAUDE_3_7_SONNET_20250219)
.thinkingType("enabled")
.thinkingBudgetTokens(1024)
.maxTokens(1024 + 100)
.returnThinking(true)
.sendThinking(true)
.build();
Quarkus
更多详情请见 这里。
Spring Boot
引入 Anthropic 的 Spring Boot starter:
<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-anthropic-spring-boot-starter</artifactId>
<version>1.3.0-beta9</version>
</dependency>
配置 AnthropicChatModel
Bean:
langchain4j.anthropic.chat-model.api-key = ${ANTHROPIC_API_KEY}
配置 AnthropicStreamingChatModel
Bean:
langchain4j.anthropic.streaming-chat-model.api-key = ${ANTHROPIC_API_KEY}