File Output
Reliable file generation, whether new or updates, is one of the most challenging parts of working with LLMs. The GenAIScript script supports a few approaches and formats to generate files: for small files, regenerating the entire content is typically more efficient. For large files, generating edits is more efficient.
How it works
GenAIScript automatically adds a system message that teaches the LLM how to format the output files.
Let’s start with a script that generates a poem and asks the GenAIScript to save it to a text file.
Since no system prompt is specified, GenAIScript adds the default set of system prompts, including the system.files prompt. This prompt instructs the LLM to generate a file with the output of the script. The LLM responds with a code section that also mentions a filename. This is the format that GenAIScript can automatically parse out.
By default, file edits are not applied automatically. In Visual Studio Code, a refactoring preview is opened and the user can accept or reject the changes.
In the CLI, the changes are silently ignored unless the --apply-edits
flag is used.
Changelog format
The full regeneration of files only works for small files.
For large files, GenAIScript uses a custom changelog
format that is designed to minimize hallucinations.
When we run the script on a source file, the LLM generates a changelog that contains the changes to the file. GenAIScript will parse this output and generate a file edit similar to a full file update.\
As you can see, the changelog format is much more heavyweight in terms of token; however, it is more reliable at producing edits in large files.
Declaring file outputs
The defFileOutput
function lets you declare file output paths and the purpose of those files. This function is used to specify the output files that are generated by the script.
In our example, we tell the LLM to produce the poem at poem.txt
and it also allows GenAIScript to validate the file location and automatically apply the changes.
In the background, GenAIScript adds a system message that looks like this and tells the LLM where files should be.
Schema Validation
You can associate a JSON schema with the file output. This schema is used to validate the content of the file before it is written to disk.
File output post processing
You can register a callback to programmaticaly manipulate the generate files using defOutputProcessor.
System prompts
The support for generating files is defined in a few system prompts. These prompts are typically automatically added but you may need to add them back if you specify a custom set of system prompts.
- system.files, instructs the “full” file format
- system.changelog, instructs the “changelog” file format
- system.files, instructs JSON schema in file generation