Class GPTChatMessagesTemplate
- java.lang.Object
- 
- com.composum.ai.backend.base.service.chat.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 SummaryFields Modifier and Type Field Description static PatternMESSAGE_SEPARATORmatches the lines like ---------- system ---------- that separate the individual messages in the templatestatic StringPLACEHOLDER_MARKERIf that's in a string, it likely contains placeholders.static PatternPLACEHOLDER_PATTERNstatic StringTEMPLATEDIRstatic StringTEMPLATESUFFIX
 - 
Constructor SummaryConstructors Constructor Description GPTChatMessagesTemplate(InputStream stream, String name)Reads the template from the stream, which is closed afterwards.GPTChatMessagesTemplate(ClassLoader classLoader, String name)Reads the template from the classloader.GPTChatMessagesTemplate(org.osgi.framework.Bundle bundle, String templateName)Reads the template from an OSGI bundle.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected static InputStreamgetGetTemplateStreamFromBundle(org.osgi.framework.Bundle bundle, String templateName)protected static InputStreamgetGetTemplateStreamFromClassloader(ClassLoader classLoader, String name)protected static List<List<String>>getMessageLineBlocks(Iterator<String> lineiterator)split the lines into blocks of lines where the first line is matching MESSAGE_SEPARATORList<GPTChatMessage>getMessages(Map<String,String> placeholderValues)Returns a list of messages with the placeholders replaced.protected voidprocessBlocks(List<List<String>> blocks)
 
- 
- 
- 
Field Detail- 
TEMPLATEDIRpublic static final String TEMPLATEDIR - See Also:
- Constant Field Values
 
 - 
TEMPLATESUFFIXpublic static final String TEMPLATESUFFIX - See Also:
- Constant Field Values
 
 - 
MESSAGE_SEPARATORpublic static final Pattern MESSAGE_SEPARATOR matches the lines like ---------- system ---------- that separate the individual messages in the template
 - 
PLACEHOLDER_MARKERpublic static final String PLACEHOLDER_MARKER If that's in a string, it likely contains placeholders.- See Also:
- Constant Field Values
 
 - 
PLACEHOLDER_PATTERNpublic static final Pattern PLACEHOLDER_PATTERN 
 
- 
 - 
Constructor Detail- 
GPTChatMessagesTemplatepublic 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
 
 - 
GPTChatMessagesTemplatepublic 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
 
 - 
GPTChatMessagesTemplatepublic 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- 
getGetTemplateStreamFromClassloaderprotected static InputStream getGetTemplateStreamFromClassloader(@Nullable ClassLoader classLoader, @Nonnull String name) 
 - 
getGetTemplateStreamFromBundleprotected static InputStream getGetTemplateStreamFromBundle(org.osgi.framework.Bundle bundle, String templateName) 
 - 
getMessageLineBlocksprotected static List<List<String>> getMessageLineBlocks(Iterator<String> lineiterator) split the lines into blocks of lines where the first line is matching MESSAGE_SEPARATOR
 - 
getMessagespublic 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.
 
- 
 
-