Class AITemplatingServiceImpl
- java.lang.Object
 - 
- com.composum.ai.backend.slingbase.experimential.impl.AITemplatingServiceImpl
 
 
- 
- All Implemented Interfaces:
 AITemplatingService
public class AITemplatingServiceImpl extends Object implements AITemplatingService
This implementation uses the markers:- A field that is a prompt begins with 
PROMPTFIELD: - A field that is referenced by other fields begins with 
PROMPTFIELD#ID:where ID is a unique identifier for the field - A URL source is added as 
SOURCEURL(https://example.com/)after that. - A prompt that applies to the whole page can be put into a multi line field; it begins on a line with 
PAGEPROMPT: 
 
- 
- 
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classAITemplatingServiceImpl.AITemplatingRetryableExceptionAn exception that says something is wrong with the response, but that might be temporary and can be retried.protected static classAITemplatingServiceImpl.Replacement 
- 
Field Summary
Fields Modifier and Type Field Description protected GPTChatCompletionServicechatCompletionServiceprotected AIConfigurationServiceconfigurationServiceprotected com.google.gson.Gsongsonprotected static PatternHAS_WHITESPACEprotected static PatternIGNORED_PROPERTIESA pattern for property names which we ignore.protected static org.slf4j.LoggerLOGprotected ApproximateMarkdownServicemarkdownServiceprotected static PatternPAGEPROMPTPattern for a PAGEPROMPT: ...protected static StringPREFIX_INFORMATIONALLYprotected static StringPREFIX_PROMPTprotected static PatternPROMPTFIELDMatches a text with PROMPTFIELD start and determines the id if there is one given.static StringPROPERTY_PREFIX_PROMPTProperty prefix: if a property contains a prompt, it's copied into a property with this prefix so that we can redo and undo the process.protected static PatternRICHTEXT_PATTERNHeuristics to identify richtext properties: start and end with a HTML tag.protected static PatternSEPARATOR_PATTERNprotected static PatternSOURCEURLPattern for aSOURCEURL(https://example.com/)to extract the URL - group "url".protected static StringSYSMSGprotected static StringTHE_END_COMMANDprotected static PatternTHE_END_PATTERNMatches a properly executedTHE_END_COMMAND.protected static TypeTYPE_MAP_STRING_STRING 
- 
Constructor Summary
Constructors Constructor Description AITemplatingServiceImpl() 
- 
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected @NotNull List<AITemplatingServiceImpl.Replacement>collectPossibleReplacements(org.apache.sling.api.resource.Resource resource)protected static voidcollectPrompts(List<AITemplatingServiceImpl.Replacement> replacements, Map<String,AITemplatingServiceImpl.Replacement> ids, Map<String,String> prompts)protected Stream<org.apache.sling.api.resource.Resource>descendantsStream(org.apache.sling.api.resource.Resource resource)protected static voidexecuteReplacements(Map<String,String> responses, Map<String,AITemplatingServiceImpl.Replacement> ids)protected static List<String>extractPagePrompts(List<AITemplatingServiceImpl.Replacement> replacements)protected static Map<String,String>extractParts(String response)Splits the response at the %%%%%%%% ID %%%%%%%% separators and puts the items into a map.protected static List<String>extractSourceUrls(List<AITemplatingServiceImpl.Replacement> replacements)protected static @NotNull StringjoinText(Map<String,String> prompts)protected @NotNull GPTChatRequestmakeRequest(org.apache.sling.api.resource.Resource resource, List<String> urls, List<String> pagePrompts, Map<String,String> prompts, String backgroundInformation, String additionalPrompt)protected org.apache.sling.api.resource.Resourcenormalize(org.apache.sling.api.resource.Resource resource)booleanreplacePromptsInResource(org.apache.sling.api.resource.Resource resource, String additionalPrompt, List<URI> additionalUrls, String backgroundInformation)Replaces all prompts in the resource with the response of the AI.booleanresetToPrompts(org.apache.sling.api.resource.Resource resource)Sets all properties from its backup copies starting withPROPERTY_PREFIX_PROMPTand removes the backups. 
 - 
 
- 
- 
Field Detail
- 
PROPERTY_PREFIX_PROMPT
public static final String PROPERTY_PREFIX_PROMPT
Property prefix: if a property contains a prompt, it's copied into a property with this prefix so that we can redo and undo the process.- See Also:
 - Constant Field Values
 
 
- 
IGNORED_PROPERTIES
protected static final Pattern IGNORED_PROPERTIES
A pattern for property names which we ignore. We only care about String properties that have whitespace, anyway. 
- 
HAS_WHITESPACE
protected static final Pattern HAS_WHITESPACE
 
- 
PROMPTFIELD
protected static final Pattern PROMPTFIELD
Matches a text with PROMPTFIELD start and determines the id if there is one given. Either it'sPROMPTFIELD: ...orPROMPTFIELD#ID: .... 
- 
PREFIX_INFORMATIONALLY
protected static final String PREFIX_INFORMATIONALLY
- See Also:
 - Constant Field Values
 
 
- 
SOURCEURL
protected static final Pattern SOURCEURL
Pattern for aSOURCEURL(https://example.com/)to extract the URL - group "url". 
- 
PAGEPROMPT
protected static final Pattern PAGEPROMPT
Pattern for a PAGEPROMPT: ... - all lines to the end of the field are in group "url". 
- 
RICHTEXT_PATTERN
protected static final Pattern RICHTEXT_PATTERN
Heuristics to identify richtext properties: start and end with a HTML tag. 
- 
SYSMSG
protected static final String SYSMSG
- See Also:
 - Constant Field Values
 
 
- 
PREFIX_PROMPT
protected static final String PREFIX_PROMPT
- See Also:
 - Constant Field Values
 
 
- 
SEPARATOR_PATTERN
protected static final Pattern SEPARATOR_PATTERN
 
- 
THE_END_COMMAND
protected static final String THE_END_COMMAND
- See Also:
 - Constant Field Values
 
 
- 
THE_END_PATTERN
protected static final Pattern THE_END_PATTERN
Matches a properly executedTHE_END_COMMAND. 
- 
TYPE_MAP_STRING_STRING
protected static final Type TYPE_MAP_STRING_STRING
 
- 
gson
protected final com.google.gson.Gson gson
 
- 
LOG
protected static final org.slf4j.Logger LOG
 
- 
chatCompletionService
protected GPTChatCompletionService chatCompletionService
 
- 
markdownService
protected ApproximateMarkdownService markdownService
 
- 
configurationService
protected AIConfigurationService configurationService
 
 - 
 
- 
Method Detail
- 
normalize
protected org.apache.sling.api.resource.Resource normalize(org.apache.sling.api.resource.Resource resource)
 
- 
replacePromptsInResource
public boolean replacePromptsInResource(org.apache.sling.api.resource.Resource resource, String additionalPrompt, List<URI> additionalUrls, String backgroundInformation) throws URISyntaxException, IOExceptionDescription copied from interface:AITemplatingServiceReplaces all prompts in the resource with the response of the AI.- Specified by:
 replacePromptsInResourcein interfaceAITemplatingService- Parameters:
 resource- the resource to replace the prompts inadditionalPrompt- optionally, an additional prompt to add to the AI requestadditionalUrls- optionally, additional URLs with background information to provide data to the AIbackgroundInformation- optionally, additional background information to provide data to the AI (not a prompt!)- Returns:
 - true if the resource was changed, false if it was not changed; no commit yet.
 - Throws:
 URISyntaxExceptionIOException
 
- 
extractParts
@Nonnull protected static Map<String,String> extractParts(String response)
Splits the response at the %%%%%%%% ID %%%%%%%% separators and puts the items into a map. Inverse ofjoinText(Map). 
- 
executeReplacements
protected static void executeReplacements(Map<String,String> responses, Map<String,AITemplatingServiceImpl.Replacement> ids)
 
- 
makeRequest
@NotNull protected @NotNull GPTChatRequest makeRequest(org.apache.sling.api.resource.Resource resource, List<String> urls, List<String> pagePrompts, Map<String,String> prompts, String backgroundInformation, String additionalPrompt) throws IOException, URISyntaxException
- Throws:
 IOExceptionURISyntaxException
 
- 
collectPossibleReplacements
@NotNull protected @NotNull List<AITemplatingServiceImpl.Replacement> collectPossibleReplacements(org.apache.sling.api.resource.Resource resource)
 
- 
collectPrompts
protected static void collectPrompts(List<AITemplatingServiceImpl.Replacement> replacements, Map<String,AITemplatingServiceImpl.Replacement> ids, Map<String,String> prompts)
 
- 
extractPagePrompts
protected static List<String> extractPagePrompts(List<AITemplatingServiceImpl.Replacement> replacements)
 
- 
extractSourceUrls
protected static List<String> extractSourceUrls(List<AITemplatingServiceImpl.Replacement> replacements)
 
- 
resetToPrompts
public boolean resetToPrompts(org.apache.sling.api.resource.Resource resource) throws org.apache.sling.api.resource.PersistenceExceptionSets all properties from its backup copies starting withPROPERTY_PREFIX_PROMPTand removes the backups.- Specified by:
 resetToPromptsin interfaceAITemplatingService- Returns:
 - Throws:
 org.apache.sling.api.resource.PersistenceException
 
- 
descendantsStream
protected Stream<org.apache.sling.api.resource.Resource> descendantsStream(org.apache.sling.api.resource.Resource resource)
 
 - 
 
 -