Allow custom prompts through environment variables #1029
+264
−104
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR introduces prompt customization through environment variables mentioned in #1018.
It solves issues with local language models being incompatible with prompts hardcoded inside the application.
This feature is backward compatible - if none of new environment variables is defined, Hoarder should behave exactly as it behaved before.
Technical details
3 new optional environment variables are defined:
TAGGING_PROMPT
- tag generation for text content,IMAGE_PROMPT
- tag generation for images,SUMMARIZATION_PROMPT
- summarizing text.Default prompts are moved into
config.ts
and they are used if corresponding environment variables were not defined.Filling prompts with data
In current solution, content is being inflated into the prompt text using string interpolation. Since there is no string interpolation in environment variables, I introduced markers which are replaced by actual values when building a prompt. The marker values are:
tags
,aiTags
,userTags
,lang
,customPrompts
(tagging rules),content
.Values
tags
,aiTags
,userTags
existed before and were used in "Tagging Rules" feature.However, I decided to change current markers from using dollar sign (
$variableName
) to doubled curly braces ({{ variableName }}
), because dollar sign would have to be escaped when defined inside.env
file (I'm not insisting on this, we can discuss other templating solutions). To keep backward compatibility, markerstags
,aiTags
anduserTags
can also be defined with dollar-sign notation.Custom prompts through env variables vs custom prompts using tagging rules
Hoarder already allows some customization in prompts using "tagging rules" in AI settings. However, tagging rules don't change the entire prompt. But I don't think these features would be in conflict with each other - custom prompt defined through environment variable sets the prompt template for the entire Hoarder instance, while tagging rules are additional per-user settings which could be inflated into the global prompt template with
{{ customPrompts }}
marker. In this way they supplement each other.