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 class
AITemplatingServiceImpl.AITemplatingRetryableException
An exception that says something is wrong with the response, but that might be temporary and can be retried.protected static class
AITemplatingServiceImpl.Replacement
-
Field Summary
Fields Modifier and Type Field Description protected GPTChatCompletionService
chatCompletionService
protected AIConfigurationService
configurationService
protected com.google.gson.Gson
gson
protected static Pattern
HAS_WHITESPACE
protected static Pattern
IGNORED_PROPERTIES
A pattern for property names which we ignore.protected static org.slf4j.Logger
LOG
protected ApproximateMarkdownService
markdownService
protected static Pattern
PAGEPROMPT
Pattern for a PAGEPROMPT: ...protected static String
PREFIX_INFORMATIONALLY
protected static String
PREFIX_PROMPT
protected static Pattern
PROMPTFIELD
Matches a text with PROMPTFIELD start and determines the id if there is one given.static 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.protected static Pattern
RICHTEXT_PATTERN
Heuristics to identify richtext properties: start and end with a HTML tag.protected static Pattern
SEPARATOR_PATTERN
protected static Pattern
SOURCEURL
Pattern for aSOURCEURL(https://example.com/)
to extract the URL - group "url".protected static String
SYSMSG
protected static String
THE_END_COMMAND
protected static Pattern
THE_END_PATTERN
Matches a properly executedTHE_END_COMMAND
.protected static Type
TYPE_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 void
collectPrompts(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 void
executeReplacements(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 String
joinText(Map<String,String> prompts)
protected @NotNull GPTChatRequest
makeRequest(org.apache.sling.api.resource.Resource resource, List<String> urls, List<String> pagePrompts, Map<String,String> prompts, String backgroundInformation)
protected org.apache.sling.api.resource.Resource
normalize(org.apache.sling.api.resource.Resource resource)
boolean
replacePromptsInResource(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.boolean
resetToPrompts(org.apache.sling.api.resource.Resource resource)
Sets all properties from its backup copies starting withPROPERTY_PREFIX_PROMPT
and 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, IOException
Description copied from interface:AITemplatingService
Replaces all prompts in the resource with the response of the AI.- Specified by:
replacePromptsInResource
in 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:
URISyntaxException
IOException
-
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) throws IOException, URISyntaxException
- Throws:
IOException
URISyntaxException
-
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.PersistenceException
Sets all properties from its backup copies starting withPROPERTY_PREFIX_PROMPT
and removes the backups.- Specified by:
resetToPrompts
in 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)
-
-