Class AiMessage

java.lang.Object
dev.langchain4j.data.message.AiMessage
All Implemented Interfaces:
ChatMessage

public class AiMessage extends Object implements ChatMessage
Represents a message generated by AI (language model). This message can contain:
 - text(): textual content
 - thinking(): thinking/reasoning content
 - toolExecutionRequests(): requests to execute tools
 - attributes(): additional attributes, typically provider-specific
 

In case this message contains tool execution requests, the response to this message should be one ToolExecutionResultMessage for each tool execution request.

  • Constructor Details

    • AiMessage

      public AiMessage(String text)
      Create a new AiMessage with the given text.
      Parameters:
      text - the text of the message.
    • AiMessage

      public AiMessage(List<ToolExecutionRequest> toolExecutionRequests)
      Create a new AiMessage with the given tool execution requests.
      Parameters:
      toolExecutionRequests - the tool execution requests of the message.
    • AiMessage

      public AiMessage(String text, List<ToolExecutionRequest> toolExecutionRequests)
      Create a new AiMessage with the given text and tool execution requests.
      Parameters:
      text - the text of the message.
      toolExecutionRequests - the tool execution requests of the message.
    • AiMessage

      public AiMessage(AiMessage.Builder builder)
      Since:
      1.2.0
  • Method Details

    • text

      public String text()
      Get the text of the message.
      Returns:
      the text of the message.
    • thinking

      @Experimental public String thinking()
      Get the thinking/reasoning text of the message.
      Returns:
      the thinking/reasoning text of the message.
      Since:
      1.2.0
    • toolExecutionRequests

      public List<ToolExecutionRequest> toolExecutionRequests()
      Get the tool execution requests of the message.
      Returns:
      the tool execution requests of the message.
    • hasToolExecutionRequests

      public boolean hasToolExecutionRequests()
      Check if the message has ToolExecutionRequests.
      Returns:
      true if the message has ToolExecutionRequests, false otherwise.
    • attributes

      @Experimental public Map<String,Object> attributes()
      Returns additional attributes, typically provider-specific.
      Since:
      1.2.0
      See Also:
    • attribute

      @Experimental public <T> T attribute(String key, Class<T> type)
      Returns additional attribute by it's key.
      Since:
      1.2.0
      See Also:
    • type

      public ChatMessageType type()
      Description copied from interface: ChatMessage
      The type of the message.
      Specified by:
      type in interface ChatMessage
      Returns:
      the type of the message
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • builder

      public static AiMessage.Builder builder()
    • from

      public static AiMessage from(String text)
      Create a new AiMessage with the given text.
      Parameters:
      text - the text of the message.
      Returns:
      the new AiMessage.
    • from

      public static AiMessage from(ToolExecutionRequest... toolExecutionRequests)
      Create a new AiMessage with the given tool execution requests.
      Parameters:
      toolExecutionRequests - the tool execution requests of the message.
      Returns:
      the new AiMessage.
    • from

      public static AiMessage from(List<ToolExecutionRequest> toolExecutionRequests)
      Create a new AiMessage with the given tool execution requests.
      Parameters:
      toolExecutionRequests - the tool execution requests of the message.
      Returns:
      the new AiMessage.
    • from

      public static AiMessage from(String text, List<ToolExecutionRequest> toolExecutionRequests)
      Create a new AiMessage with the given text and tool execution requests.
      Parameters:
      text - the text of the message.
      toolExecutionRequests - the tool execution requests of the message.
      Returns:
      the new AiMessage.
    • aiMessage

      public static AiMessage aiMessage(String text)
      Create a new AiMessage with the given text.
      Parameters:
      text - the text of the message.
      Returns:
      the new AiMessage.
    • aiMessage

      public static AiMessage aiMessage(ToolExecutionRequest... toolExecutionRequests)
      Create a new AiMessage with the given tool execution requests.
      Parameters:
      toolExecutionRequests - the tool execution requests of the message.
      Returns:
      the new AiMessage.
    • aiMessage

      public static AiMessage aiMessage(List<ToolExecutionRequest> toolExecutionRequests)
      Create a new AiMessage with the given tool execution requests.
      Parameters:
      toolExecutionRequests - the tool execution requests of the message.
      Returns:
      the new AiMessage.
    • aiMessage

      public static AiMessage aiMessage(String text, List<ToolExecutionRequest> toolExecutionRequests)
      Create a new AiMessage with the given text and tool execution requests.
      Parameters:
      text - the text of the message.
      toolExecutionRequests - the tool execution requests of the message.
      Returns:
      the new AiMessage.