Skip to content

Commit

Permalink
PR for #111
Browse files Browse the repository at this point in the history
added a $tags,$aiTags and $userTags placeholder that will be replaced with all tags, ai tags or user tags during inference
  • Loading branch information
kamtschatka committed Nov 3, 2024
1 parent 2efc7c8 commit bce4ddb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
68 changes: 68 additions & 0 deletions apps/workers/openaiWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,9 +198,77 @@ async function fetchCustomPrompts(
},
});

if (containsTagsPlaceholder(prompts)) {
return replaceTagsPlaceholders(prompts, userId);
}

return prompts.map((p) => p.text);
}

async function replaceTagsPlaceholders(
prompts: { text: string }[],
userId: string,
): Promise<string[]> {
const tags = await loadTagsForUser(userId);
const tagsString = `[${tags.map((tag) => tag.name).join(",")}]`;
const aiTagsString = `[${tags
.filter((tag) => tag.aiCount > 0)
.map((tag) => tag.name)
.join(",")}]`;
const userTagsString = `[${tags
.filter((tag) => tag.humanCount > 0)
.map((tag) => tag.name)
.join(",")}]`;

return prompts.map((p) =>
p.text
.replaceAll("$tags", tagsString)
.replaceAll("$aiTags", aiTagsString)
.replaceAll("$userTags", userTagsString),
);
}

async function loadTagsForUser(userId: string) {
const tagsWithCounts = await db.query.bookmarkTags.findMany({
where: eq(bookmarkTags.userId, userId),
columns: {
name: true,
},
with: {
tagsOnBookmarks: {
columns: {
attachedBy: true,
},
},
},
});

return tagsWithCounts.map((tag) => {
const aiCount = tag.tagsOnBookmarks.filter(
(tob) => tob.attachedBy === "ai",
).length;
const humanCount = tag.tagsOnBookmarks.filter(
(tob) => tob.attachedBy === "human",
).length;
return {
name: tag.name,
aiCount,
humanCount,
};
});
}

function containsTagsPlaceholder(prompts: { text: string }[]): boolean {
return (
prompts.filter(
(p) =>
p.text.includes("$tags") ||
p.text.includes("$aiTags") ||
p.text.includes("$userTags"),
).length > 0
);
}

async function inferTagsFromPDF(
jobId: string,
bookmark: NonNullable<Awaited<ReturnType<typeof fetchBookmark>>>,
Expand Down
5 changes: 5 additions & 0 deletions docs/docs/03-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ Either `OPENAI_API_KEY` or `OLLAMA_BASE_URL` need to be set for automatic taggin
| INFERENCE_LANG | No | english | The language in which the tags will be generated. |
| INFERENCE_JOB_TIMEOUT_SEC | No | 30 | How long to wait for the inference job to finish before timing out. If you're running ollama without powerful GPUs, you might want to increase the timeout a bit. |

:::info
- You can append additional instructions to the prompt used for automatic tagging, in the `AI Settings` (in the `User Settings` screen)
- You can use the placeholders `$tags`, `$aiTags`, `$userTags` in the prompt. These placeholders will be replaced with all tags, ai generated tags or human created tags when automatic tagging is performed (e.g. `[hoarder, computer, ai]`)
:::

## Crawler Configs

| Name | Required | Default | Description |
Expand Down

0 comments on commit bce4ddb

Please sign in to comment.