Skip to main content

Anthropic

Maven Dependency

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-anthropic</artifactId>
<version>0.30.0</version>
</dependency>

AnthropicChatModel

AnthropicChatModel model = AnthropicChatModel.withApiKey(System.getenv("ANTHROPIC_API_KEY"));
String answer = model.generate("Say 'Hello World'");
System.out.println(answer);

Customizing

AnthropicChatModel model = AnthropicChatModel.builder()
.baseUrl(...)
.apiKey(...)
.version(...)
.beta(...)
.modelName(...)
.temperature(...)
.topP(...)
.topK(...)
.maxTokens(...)
.stopSequences(...)
.timeout(...)
.maxRetries(...)
.logRequests(...)
.logResponses(...)
.build();

See the description of some of the parameters above here.

AnthropicStreamingChatModel

AnthropicStreamingChatModel model = AnthropicStreamingChatModel.withApiKey(System.getenv("ANTHROPIC_API_KEY"));

model.generate("Say 'Hello World'", new StreamingResponseHandler<AiMessage>() {

@Override
public void onNext(String token) {
// this method is called when a new token is available
}

@Override
public void onComplete(Response<AiMessage> response) {
// this method is called when the model has completed responding
}

@Override
public void onError(Throwable error) {
// this method is called when an error occurs
}
});

Customizing

Identical to the AnthropicChatModel, see above.

Tools

Anthropic supports tools, but only in a non-streaming mode.

Anthropic documentation on tools can be found here.

Quarkus

TODO

Spring Boot

Import Spring Boot starter for Anthropic:

<dependency>
<groupId>dev.langchain4j</groupId>
<artifactId>langchain4j-anthropic-spring-boot-starter</artifactId>
<version>0.30.0</version>
</dependency>

Configure AnthropicChatModel bean:

langchain4j.anthropic.chat-model.api-key = ${ANTHROPIC_API_KEY}

Configure AnthropicStreamingChatModel bean:

langchain4j.anthropic.streaming-chat-model.api-key = ${ANTHROPIC_API_KEY}