Class AnthropicChatModel

java.lang.Object
dev.langchain4j.model.anthropic.AnthropicChatModel
All Implemented Interfaces:
ChatLanguageModel

public class AnthropicChatModel extends Object implements ChatLanguageModel
Represents an Anthropic language model with a Messages (chat) API.
More details are available here.

It supports tools. See more information here.

It supports Images as inputs. UserMessages can contain one or multiple ImageContents. Images must not be represented as URLs; they should be Base64-encoded strings and include a mimeType.

The content of SystemMessages is sent using the "system" parameter. If there are multiple SystemMessages, they are concatenated with a double newline (\n\n).

Sanitization is performed on the ChatMessages provided to conform to Anthropic API requirements. This process includes verifying that the first message is a UserMessage and removing any consecutive UserMessages. Any messages removed during sanitization are logged as warnings and not submitted to the API.
  • Method Details

    • withApiKey

      public static AnthropicChatModel withApiKey(String apiKey)
      Creates an instance of AnthropicChatModel with the specified API key.
      Parameters:
      apiKey - the API key for authentication
      Returns:
      an AnthropicChatModel instance
    • generate

      public Response<AiMessage> generate(List<ChatMessage> messages)
      Description copied from interface: ChatLanguageModel
      Generates a response from the model based on a sequence of messages. Typically, the sequence contains messages in the following order: System (optional) - User - AI - User - AI - User ...
      Specified by:
      generate in interface ChatLanguageModel
      Parameters:
      messages - A list of messages.
      Returns:
      The response generated by the model.
    • generate

      public Response<AiMessage> generate(List<ChatMessage> messages, List<ToolSpecification> toolSpecifications)
      Description copied from interface: ChatLanguageModel
      Generates a response from the model based on a list of messages and a list of tool specifications. The response may either be a text message or a request to execute one of the specified tools. Typically, the list contains messages in the following order: System (optional) - User - AI - User - AI - User ...
      Specified by:
      generate in interface ChatLanguageModel
      Parameters:
      messages - A list of messages.
      toolSpecifications - A list of tools that the model is allowed to execute. The model autonomously decides whether to use any of these tools.
      Returns:
      The response generated by the model. AiMessage can contain either a textual response or a request to execute one of the tools.