-
-
Notifications
You must be signed in to change notification settings - Fork 466
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Allow the user to configure fully custom LLM prompts
- Loading branch information
1 parent
09e0659
commit 124af70
Showing
2 changed files
with
55 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,64 @@ | ||
export function buildImagePrompt(lang: string, customPrompts: string[]) { | ||
return ` | ||
import serverConfig from "./config"; | ||
|
||
const DEFAULT_IMAGE_PROMPT = ` | ||
You are a bot in a read-it-later app and your responsibility is to help with automatic tagging. | ||
Please analyze the attached image and suggest relevant tags that describe its key themes, topics, and main ideas. The rules are: | ||
- Aim for a variety of tags, including broad categories, specific keywords, and potential sub-genres. | ||
- The tags language must be in ${lang}. | ||
- The tags language must be in {{lang}}. | ||
- If the tag is not generic enough, don't include it. | ||
- Aim for 10-15 tags. | ||
- If there are no good tags, don't emit any. | ||
${customPrompts && customPrompts.map((p) => `- ${p}`).join("\n")} | ||
{{customPrompts}} | ||
You must respond in valid JSON with the key "tags" and the value is list of tags. Don't wrap the response in a markdown code.`; | ||
} | ||
|
||
export function buildTextPrompt( | ||
lang: string, | ||
customPrompts: string[], | ||
content: string, | ||
) { | ||
return ` | ||
const DEFAULT_TEXT_PROMPT = ` | ||
You are a bot in a read-it-later app and your responsibility is to help with automatic tagging. | ||
Please analyze the text between the sentences "CONTENT START HERE" and "CONTENT END HERE" and suggest relevant tags that describe its key themes, topics, and main ideas. The rules are: | ||
- Aim for a variety of tags, including broad categories, specific keywords, and potential sub-genres. | ||
- The tags language must be in ${lang}. | ||
- The tags language must be in {{lang}}. | ||
- If it's a famous website you may also include a tag for the website. If the tag is not generic enough, don't include it. | ||
- The content can include text for cookie consent and privacy policy, ignore those while tagging. | ||
- Aim for 3-5 tags. | ||
- If there are no good tags, leave the array empty. | ||
${customPrompts && customPrompts.map((p) => `- ${p}`).join("\n")} | ||
{{customPrompts}} | ||
CONTENT START HERE | ||
${content} | ||
{{content}} | ||
CONTENT END HERE | ||
You must respond in JSON with the key "tags" and the value is an array of string tags.`; | ||
|
||
function renderTemplate(template: string, variables: Map<string, string>): string { | ||
let result = template; | ||
|
||
for (const [name, value] of variables.entries()) { | ||
const placeholder = "\\{\\{" + name + "\\}\\}"; // Don't parse the {{}} as regex! | ||
result = result.replace(new RegExp(placeholder, 'g'), value); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
export function buildImagePrompt(lang: string, customPrompts: string[]) { | ||
const promptTemplate = serverConfig.inference.imageTagPrompt || DEFAULT_IMAGE_PROMPT; | ||
const customPromptsRendered = [...customPrompts].map((p) => `- ${p}`).join("\n"); | ||
|
||
return renderTemplate(promptTemplate, new Map([ | ||
['lang', lang], | ||
['customPrompts', customPromptsRendered], | ||
])); | ||
} | ||
|
||
export function buildTextPrompt( | ||
lang: string, | ||
customPrompts: string[], | ||
content: string, | ||
) { | ||
const promptTemplate = serverConfig.inference.textTagPrompt || DEFAULT_TEXT_PROMPT; | ||
const customPromptsRendered = [...customPrompts].map((p) => `- ${p}`).join("\n"); | ||
|
||
return renderTemplate(promptTemplate, new Map([ | ||
['lang', lang], | ||
['customPrompts', customPromptsRendered], | ||
['content', content], | ||
])); | ||
} |