Class ChatMessageTexts

java.lang.Object
dev.langchain4j.test.internal.ChatMessageTexts

public final class ChatMessageTexts extends Object
Describes chat messages as text, for judges and failure messages.
  • Method Details

    • role

      public static String role(ChatMessage message)
      Returns who sent the message: system, user, assistant, tool or custom.
    • text

      public static String text(ChatMessage message)
      Returns the text of the message. Contents that are not text, such as images, are described by their type, for example [image]. The tool calls of an assistant message are described by the tool name, the call id (when there is one) and the arguments, and the result of a tool execution starts with the tool name and the call id (and says when it is an error), so that several calls of the same tool can be told apart.
    • write

      public static void write(ChatMessage message, ChatMessageTexts.PartWriter writer)
      Writes the parts of the message in their order: the same text as text(ChatMessage), with each content that is not text given to ChatMessageTexts.PartWriter.media(Content) at its position instead of being described.
    • forJudge

      public static Content forJudge(Content content)
      Returns the content to send to a judge model: an image whose detail level was not chosen when it was created (see ImageContent.detailLevelSpecified()) is copied with ImageContent.DetailLevel.AUTO; any other content is returned as it is.
    • forJudge

      public static ChatMessage forJudge(ChatMessage message)
      Returns the message to send to a judge model, with its contents changed by forJudge(Content). A message without such contents is returned as it is.
    • userText

      public static String userText(ChatMessage message)
      Returns the text of the user messages, or null for another message.
    • describe

      public static String describe(List<? extends ChatMessage> messages)
      Describes the messages, one per line (\n, the same on every operating system), each starting with its role. Contents that are not text are shown by numbered markers, for example [image 1], the same way a ChatModelJudge marks them for the judge model.
    • describe

      public static String describe(List<? extends ChatMessage> messages, int maxTextLength)
      Describes the messages like describe(List), cutting the text of each message to the given length.
    • truncate

      public static String truncate(String text, int maxLength)
      Returns the text, cut to the given length, followed by how many characters were cut, for example ...(250 more characters). A character made of two chars (such as an emoji) is not split.