Class AIGenerationTask
- java.lang.Object
-
- net.stoerr.ai.aigenpipeline.framework.task.AIGenerationTask
-
- All Implemented Interfaces:
Cloneable
public class AIGenerationTask extends Object implements Cloneable
We support the generation of files using an AI, specifically ChatGPT. A generation task can have several input files. Some of them can be prompt files with task descriptions, and some of them source files to be processed. The output of each task is one text file. A complex task can have several steps leading to several intermediate files.Since ChatGPT is not particularily fast not free and the generation results have to be manually checked, this is heavily cached. Into each output file we write the versions of all the input files from which it was generated into a comment. When the tasks are run, we compare the versions of all the input files with the versions recorded in the comment, and only regenerate the output file if the versions have changed. An input file can have a version comment that explicitly states the version, or we take the an abbreviated SHA256 hash of the input file as version. It is possible to explicitly state the versions in version comments in the input files to avoid regenerating all files if minor details e.g. in a prompt file are changed - only when the prompt file version comment is changed everything is regenerated.
A version comment can e.g. look like this:
// AIGenVersion(ourversion, inputfile1@version1, inputfile2@version2, ...)
where ourversion and version1 and version2 are hashes. ourversion is the hash of the original output of the AI. The comment syntax (in this case //) is ignored - we just look for the AIGenVersion via regex.
Normally the intermediate and final results should be checked in with Git. That ensures manual checks when they are regenerated, and minimizes regeneration.
-
-
Field Summary
Fields Modifier and Type Field Description static String
FIXME
A marker that can be inserted by the AI when something is wrong / unclear.protected List<AIInOut>
hints
protected List<AIInOut>
inputFiles
protected static Logger
LOG
protected Integer
maxTokens
protected AIInOut
output
protected static Pattern
PATTERN_INFILEPROMPT
A pattern matching infile prompts like this:protected static Pattern
PATTERN_LICENCE
A pattern that matches the license header, which we want to remove to avoid clutter.protected Map<String,String>
placeholdersAndValues
protected String
prompt
The actual prompt created from prompt files and parameters.protected List<AIInOut>
promptInputs
protected RegenerationCheckStrategy
regenerationCheckStrategy
protected String
systemMessage
protected AIInOut
systemMessageInput
protected boolean
updateRequested
protected WritingStrategy
writingStrategy
-
Constructor Summary
Constructors Constructor Description AIGenerationTask()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description AIGenerationTask
addHint(AIInOut hint)
AIGenerationTask
addInput(AIInOut input)
AIGenerationTask
addInputFile(File file)
AIGenerationTask
addInputFiles(List<File> files)
AIGenerationTask
addInputs(List<AIInOut> inputs)
AIGenerationTask
addOptionalInput(AIInOut input)
AIGenerationTask
addOptionalInputFile(File file)
AIGenerationTask
addPrompt(File promptInput, String... placeholdersAndValues)
The actual prompt to be executed.AIGenerationTask
addPrompt(File promptFile, Map<String,String> placeholdersAndValues)
The actual prompt to be executed.AIGenerationTask
addPrompt(AIInOut promptInput, String... placeholdersAndValues)
The actual prompt to be executed.AIGenerationTask
addPrompt(AIInOut promptFile, Map<String,String> placeholdersAndValues)
The actual prompt to be executed.protected Object
clone()
AIGenerationTask
copy()
Creates a deep copy of the task.AIGenerationTask
execute(Supplier<AIChatBuilder> chatBuilderFactory, File rootDirectory)
Execute the task if necessary.String
explain(Supplier<AIChatBuilder> chatBuilderFactory, File rootDirectory, String question)
Ask a question about the previous task execution.protected List<String>
getAdditionalMarkers()
protected List<AIInOut>
getAllInputs()
protected String
getFileContent(File file)
boolean
hasToBeRun()
protected AIChatBuilder
makeChatBuilder(Supplier<AIChatBuilder> chatBuilderFactory, File rootDirectory)
AIGenerationTask
maxTokens(Integer maxTokens)
protected String
relativePath(File file, File rootDirectory)
AIGenerationTask
setOutput(AIInOut output)
AIGenerationTask
setOutputFile(File file)
AIGenerationTask
setRegenerationCheckStrategy(RegenerationCheckStrategy strategy)
Sets the strategy to check whether the generation has to be run.AIGenerationTask
setSystemMessage(File systemMessageFile)
AIGenerationTask
setSystemMessage(AIInOut systemMessageFile)
AIGenerationTask
setUpdateRequested(boolean updateRequested)
If true, adds the output file as a special input with a hint that this is the current state of the output that has to be updated with minimal changes.AIGenerationTask
setWritingStrategy(WritingStrategy strategy)
Sets the strategy to deal with writing the output file.String
toJson(Supplier<AIChatBuilder> chatBuilderFactory, File rootDirectory)
For debugging purposes: returns the JSON that would be sent to the AI.String
toString()
static String
unclutter(String content)
-
-
-
Field Detail
-
LOG
protected static final Logger LOG
-
FIXME
public static final String FIXME
A marker that can be inserted by the AI when something is wrong / unclear. We will make sure the user sees that by aborting.- See Also:
- Constant Field Values
-
PATTERN_LICENCE
protected static final Pattern PATTERN_LICENCE
A pattern that matches the license header, which we want to remove to avoid clutter.
-
PATTERN_INFILEPROMPT
protected static final Pattern PATTERN_INFILEPROMPT
A pattern matching infile prompts like this:<%-- AIGenPromptStart(tablefromdatacopied) Make a markdown table from the data, with columns "Name" and "Profession". AIGenCommand(tablefromdatacopied) -f -m copy tablefromdata.md AIGenPromptEnd(tablefromdatacopied) --%>
This matches a line containing AIGenPromptStart with an id until the corresponding AIGenPromptEnd.
-
output
protected AIInOut output
-
updateRequested
protected boolean updateRequested
-
prompt
protected String prompt
The actual prompt created from prompt files and parameters.
-
systemMessage
protected String systemMessage
-
systemMessageInput
protected AIInOut systemMessageInput
-
maxTokens
protected Integer maxTokens
-
regenerationCheckStrategy
protected RegenerationCheckStrategy regenerationCheckStrategy
-
writingStrategy
protected WritingStrategy writingStrategy
-
-
Method Detail
-
copy
public AIGenerationTask copy()
Creates a deep copy of the task.
-
maxTokens
public AIGenerationTask maxTokens(Integer maxTokens)
-
addOptionalInput
public AIGenerationTask addOptionalInput(@Nullable AIInOut input)
-
addOptionalInputFile
public AIGenerationTask addOptionalInputFile(@Nullable File file)
-
addInputFiles
public AIGenerationTask addInputFiles(List<File> files)
-
addInputs
public AIGenerationTask addInputs(List<AIInOut> inputs)
-
addInput
public AIGenerationTask addInput(AIInOut input)
-
addInputFile
public AIGenerationTask addInputFile(File file)
-
addHint
public AIGenerationTask addHint(AIInOut hint)
-
setOutput
public AIGenerationTask setOutput(@Nonnull AIInOut output)
-
setOutputFile
public AIGenerationTask setOutputFile(@Nonnull File file)
-
setRegenerationCheckStrategy
public AIGenerationTask setRegenerationCheckStrategy(RegenerationCheckStrategy strategy)
Sets the strategy to check whether the generation has to be run. Default isRegenerationCheckStrategy.VERSIONMARKER
.
-
setWritingStrategy
public AIGenerationTask setWritingStrategy(WritingStrategy strategy)
Sets the strategy to deal with writing the output file. Default isWritingStrategy.WITHVERSION
.
-
setUpdateRequested
public AIGenerationTask setUpdateRequested(boolean updateRequested)
If true, adds the output file as a special input with a hint that this is the current state of the output that has to be updated with minimal changes.
-
hasToBeRun
public boolean hasToBeRun()
-
addPrompt
public AIGenerationTask addPrompt(@Nonnull AIInOut promptInput, String... placeholdersAndValues)
The actual prompt to be executed. The prompt file content can contain placeholders that are replaced by the values given: placeholdersAndValues contain alternatingly placeholder names and values for them.- Returns:
- this
-
addPrompt
public AIGenerationTask addPrompt(@Nonnull File promptInput, String... placeholdersAndValues)
The actual prompt to be executed. The prompt file content can contain placeholders that are replaced by the values given: placeholdersAndValues contain alternatingly placeholder names and values for them.- Returns:
- this
-
addPrompt
public AIGenerationTask addPrompt(@Nonnull AIInOut promptFile, Map<String,String> placeholdersAndValues)
The actual prompt to be executed. The prompt file content can contain placeholders that are replaced by the values given.- Returns:
- this
-
addPrompt
public AIGenerationTask addPrompt(@Nonnull File promptFile, Map<String,String> placeholdersAndValues)
The actual prompt to be executed. The prompt file content can contain placeholders that are replaced by the values given.- Returns:
- this
-
setSystemMessage
public AIGenerationTask setSystemMessage(@Nonnull AIInOut systemMessageFile)
-
setSystemMessage
public AIGenerationTask setSystemMessage(@Nonnull File systemMessageFile)
-
execute
public AIGenerationTask execute(@Nonnull Supplier<AIChatBuilder> chatBuilderFactory, @Nonnull File rootDirectory)
Execute the task if necessary. If the output file is already there and up to date, nothing is done.
-
toJson
public String toJson(@Nonnull Supplier<AIChatBuilder> chatBuilderFactory, @Nonnull File rootDirectory)
For debugging purposes: returns the JSON that would be sent to the AI.
-
makeChatBuilder
@Nonnull protected AIChatBuilder makeChatBuilder(@Nonnull Supplier<AIChatBuilder> chatBuilderFactory, @Nonnull File rootDirectory)
-
explain
public String explain(@Nonnull Supplier<AIChatBuilder> chatBuilderFactory, @Nonnull File rootDirectory, @Nonnull String question)
Ask a question about the previous task execution. We assume it was previously run (hasToBeRun()
== false), add the result of the previous execution to the chat, and ask the AI the given question about it. This can be used e.g. to see why the AI did something, or in the process of improving the prompt, etc.- Returns:
- the answer of the AI - not written to a file!
-
clone
protected Object clone() throws CloneNotSupportedException
- Overrides:
clone
in classObject
- Throws:
CloneNotSupportedException
-
-