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 Summary
Fields 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 Summary
Constructors 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 Summary
All 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
-
TEMPLATEDIR
public static final String TEMPLATEDIR
- See Also:
- Constant Field Values
-
TEMPLATESUFFIX
public static final String TEMPLATESUFFIX
- See Also:
- Constant Field Values
-
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 fromname- 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 defaultname- 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 fromtemplateName- the name of the template, for error messages- Throws:
GPTException- if the template is not found
-
-
Method Detail
-
getGetTemplateStreamFromClassloader
protected static InputStream getGetTemplateStreamFromClassloader(@Nullable ClassLoader classLoader, @Nonnull String name)
-
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
-
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.
-
-