-
Notifications
You must be signed in to change notification settings - Fork 4
/
autoTagger.ts
63 lines (53 loc) · 2.25 KB
/
autoTagger.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { App, Editor, EditorPosition, MarkdownView, Notice, requestUrl } from "obsidian";
import { OLocalLLMSettings } from "./main";
export async function generateAndAppendTags(app: App, settings: OLocalLLMSettings) {
const view = app.workspace.getActiveViewOfType(MarkdownView);
if (!view) {
new Notice("No active Markdown view");
return;
}
const editor = view.editor;
const selectedText = editor.getSelection();
const fullText = editor.getValue();
const cursorPosition = editor.getCursor();
const textToProcess = selectedText || fullText;
try {
const tags = await generateTags(textToProcess, settings);
appendTags(editor, tags, cursorPosition);
new Notice("Tags generated and appended");
} catch (error) {
console.error("Error generating tags:", error);
new Notice("Error generating tags. Check the console for details.");
}
}
async function generateTags(text: string, settings: OLocalLLMSettings): Promise<string[]> {
const prompt = "Generate 1-5 hashtags for the following text. Return only the hashtags, separated by spaces:";
const body = {
model: settings.llmModel,
messages: [
{ role: "system", content: "You are a helpful assistant that generates relevant hashtags." },
{ role: "user", content: `${prompt}\n\n${text}` }
],
temperature: 0.7,
max_tokens: 100
};
const response = await requestUrl({
url: `${settings.serverAddress}/v1/chat/completions`,
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
});
if (response.status !== 200) {
throw new Error(`Error from LLM server: ${response.status} ${response.text}`);
}
const data = await response.json;
const generatedTags = data.choices[0].message.content.trim().split(/\s+/);
return generatedTags
.filter((tag: string) => /^#?[a-zA-Z0-9]+$/.test(tag))
.map((tag: string) => tag.startsWith('#') ? tag : `#${tag}`)
.slice(0, 5);
}
function appendTags(editor: Editor, tags: string[], cursorPosition: EditorPosition) {
const tagsString = '\n\n' + tags.join(' ');
editor.replaceRange(tagsString, cursorPosition);
}