Class GPTChatMessagesTemplate


  • public class GPTChatMessagesTemplate
    extends Object
    A template for chat messages, with placeholders.

    The template can be read from classpath, a stream or an OSGI bundle. The file is read as UTF-8 text, and the lines are split into messages by lines that match the pattern "^-{5,}\\s+(?system|user|assistant)\\s+-{5,}$". The role is one of "system", "user", "assistant".

    The messages can contain placeholders like ${name}, which are replaced by the values in the map given to getMessages. If a placeholder is missing, we throw an error, as this is a misusage of the template.

    • Field Detail

      • MESSAGE_SEPARATOR

        public static final Pattern MESSAGE_SEPARATOR
        matches the lines like ---------- system ---------- that separate the individual messages in the template
      • PLACEHOLDER_MARKER

        public static final String PLACEHOLDER_MARKER
        If that's in a string, it likely contains placeholders.
        See Also:
        Constant Field Values
      • PLACEHOLDER_PATTERN

        public static final Pattern PLACEHOLDER_PATTERN
    • Constructor Detail

      • GPTChatMessagesTemplate

        public GPTChatMessagesTemplate​(InputStream stream,
                                       @Nonnull
                                       String name)
                                throws GPTException
        Reads the template from the stream, which is closed afterwards.
        Parameters:
        stream - the stream to read from
        name - the name of the template, for error messages
        Throws:
        GPTException - if the stream is null
      • GPTChatMessagesTemplate

        public GPTChatMessagesTemplate​(@Nullable
                                       ClassLoader classLoader,
                                       @Nonnull
                                       String name)
                                throws GPTException
        Reads the template from the classloader.
        Parameters:
        classLoader - the classloader to use, or null for the default
        name - the name of the template, for error messages
        Throws:
        GPTException - if the template is not found
      • GPTChatMessagesTemplate

        public GPTChatMessagesTemplate​(@Nonnull
                                       org.osgi.framework.Bundle bundle,
                                       @Nonnull
                                       String templateName)
                                throws GPTException
        Reads the template from an OSGI bundle.
        Parameters:
        bundle - the bundle to read from
        templateName - the name of the template, for error messages
        Throws:
        GPTException - if the template is not found
    • Method Detail

      • getGetTemplateStreamFromBundle

        protected static InputStream getGetTemplateStreamFromBundle​(org.osgi.framework.Bundle bundle,
                                                                    String templateName)
      • getMessageLineBlocks

        protected static List<List<String>> getMessageLineBlocks​(Iterator<String> lineiterator)
        split the lines into blocks of lines where the first line is matching MESSAGE_SEPARATOR
      • processBlocks

        protected void processBlocks​(List<List<String>> blocks)
      • getMessages

        public List<GPTChatMessage> getMessages​(Map<String,​String> placeholderValues)
        Returns a list of messages with the placeholders replaced. If a placeholder is missing, we throw an error, as this is a misusage of the template.