Class DecisionModelInputUtils

java.lang.Object
dev.langchain4j.internal.DecisionModelInputUtils

public class DecisionModelInputUtils extends Object
Converts chat messages into the input of a DecisionModel, the same way for all components that use one.
  • Method Details

    • text

      public static String text(ChatMessage message)
      The text of a user or assistant message. Content of a user message other than text is represented by a marker, such as [attached image]. Other messages, and assistant messages without text, give an empty string.
    • hasText

      public static boolean hasText(UserMessage userMessage)
      Whether the user message contains text, not only other content such as images.
    • window

      public static List<ChatMessage> window(List<ChatMessage> messages, Predicate<? super ChatMessage> filter, int maxMessages, int maxMessageLength)
      The messages a decision model receives about a conversation: the system messages that pass the filter, and the last maxMessages other messages that pass the filter, without the tool results at their start whose AI message is not among them, plus the last user message if it is older (unless maxMessages is 0). Contents other than text are replaced by a marker, such as [attached image]. The text of messages other than system messages, and the arguments of tool calls, that are longer than maxMessageLength characters are shortened in the middle (see truncate(String, int)). Custom messages, and AI messages with neither text nor tool calls, are left out.
    • truncate

      public static String truncate(String text, int maxLength)
      Shortens a text longer than maxLength characters by keeping its beginning (two thirds) and its end (one third), with a note of about 90 characters in between saying how many characters were left out. A text that would not get shorter is returned as it is. The note says that the assistant saw the full text, so that a decision model does not take the missing part as missing information: in tests, a plain marker made decision models reject answers that were based on the full text.
    • transcript

      public static DecisionModelInputUtils.Transcript transcript(List<ChatMessage> messages, String provider)
      A conversation as a transcript: one map per message, with the images of the conversation referred to by their position, for example [image 1]. Decision models without native support for conversations send it as JSON. The thinking of assistant messages is left out, and assistant messages with neither text nor tool calls are skipped.
      Parameters:
      provider - the name of the decision model, used in error messages.
      Throws:
      UnsupportedFeatureException - if a message or a content is neither text nor an image.
      IllegalArgumentException - if nothing is left to send.
    • transcript

      public static DecisionModelInputUtils.Transcript transcript(List<ChatMessage> messages)
      A conversation as a transcript, see transcript(List, String).