aboutsummaryrefslogtreecommitdiffstats
path: root/packages/shared
diff options
context:
space:
mode:
authorMohamedBassem <me@mbassem.com>2024-09-29 01:24:04 +0000
committerMohamedBassem <me@mbassem.com>2024-09-29 01:24:04 +0000
commit36fb5a4c63aada8e8107b8e9d97a6ba128d13494 (patch)
treec1bb803b55a751cf97766a0be08691c5589aef55 /packages/shared
parent57f5faa7b5ba7a43bb09555741a207c0113e9d62 (diff)
downloadkarakeep-36fb5a4c63aada8e8107b8e9d97a6ba128d13494.tar.zst
feature(web): Add the ability to customize the inference prompts. Fixes #170
Diffstat (limited to 'packages/shared')
-rw-r--r--packages/shared/config.ts3
-rw-r--r--packages/shared/prompts.ts33
-rw-r--r--packages/shared/types/prompts.ts26
3 files changed, 62 insertions, 0 deletions
diff --git a/packages/shared/config.ts b/packages/shared/config.ts
index 21cdb1c8..b87babbd 100644
--- a/packages/shared/config.ts
+++ b/packages/shared/config.ts
@@ -113,6 +113,9 @@ export const clientConfig = {
auth: {
disableSignups: serverConfig.auth.disableSignups,
},
+ inference: {
+ inferredTagLang: serverConfig.inference.inferredTagLang,
+ },
serverVersion: serverConfig.serverVersion,
disableNewReleaseCheck: serverConfig.disableNewReleaseCheck,
};
diff --git a/packages/shared/prompts.ts b/packages/shared/prompts.ts
new file mode 100644
index 00000000..cf6d48b6
--- /dev/null
+++ b/packages/shared/prompts.ts
@@ -0,0 +1,33 @@
+export function buildImagePrompt(lang: string, customPrompts: string[]) {
+ return `
+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}.
+- 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")}
+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 `
+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}.
+- 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")}
+CONTENT START HERE
+${content}
+CONTENT END HERE
+You must respond in JSON with the key "tags" and the value is an array of string tags.`;
+}
diff --git a/packages/shared/types/prompts.ts b/packages/shared/types/prompts.ts
new file mode 100644
index 00000000..a288c65d
--- /dev/null
+++ b/packages/shared/types/prompts.ts
@@ -0,0 +1,26 @@
+import { z } from "zod";
+
+const MAX_PROMPT_TEXT_LENGTH = 100;
+
+export const zAppliesToEnumSchema = z.enum(["all", "text", "images"]);
+
+export const zPromptSchema = z.object({
+ id: z.string(),
+ text: z.string(),
+ enabled: z.boolean(),
+ appliesTo: zAppliesToEnumSchema,
+});
+
+export type ZPrompt = z.infer<typeof zPromptSchema>;
+
+export const zNewPromptSchema = z.object({
+ text: z.string().min(1).max(MAX_PROMPT_TEXT_LENGTH),
+ appliesTo: zAppliesToEnumSchema,
+});
+
+export const zUpdatePromptSchema = z.object({
+ promptId: z.string(),
+ text: z.string().max(MAX_PROMPT_TEXT_LENGTH).optional(),
+ appliesTo: zAppliesToEnumSchema.optional(),
+ enabled: z.boolean().optional(),
+});