Class AiMessage

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

public class AiMessage extends Object implements ChatMessage
Represents a response message from an AI (language model). The message can contain either a textual response or a request to execute one/multiple tool(s). In the case of tool execution, the response to this message should be one/multiple ToolExecutionResultMessage.
  • 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.
  • Method Details

    • text

      public String text()
      Get the text of the message.
      Specified by:
      text in interface ChatMessage
      Returns:
      the text of the message.
    • 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.
    • 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
    • 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.