From 546f5e6a3de4415e2fea6c2692715176a2a824c2 Mon Sep 17 00:00:00 2001 From: swaggeroo Date: Thu, 18 Jul 2024 01:09:43 +0200 Subject: [PATCH] :sparkles: Modify Prompt --- README.md | 3 ++- src/main.ts | 2 +- src/ollamaManager.ts | 3 +-- src/settings.ts | 51 ++++++++++++++++++++++++++++++-------------- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index b6455c1..669f18f 100644 --- a/README.md +++ b/README.md @@ -40,7 +40,8 @@ Those files can be synced between devices. ### Limitations The prompt to analyze the image will sometimes deliver varying results. -In the future, I will improve on the prompt or even give the user the option to choose the prompt. +In the future, I will improve on the prompt to make it more consistent. +You can also modify the prompt in the settings. ## Installation You can download the latest release from the GitHub [releases page](https://github.com/swaggeroo/obsidian-ai-image-analyser/releases) and install it manually in Obsidian. diff --git a/src/main.ts b/src/main.ts index 8ee0eb0..9cecfc4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -21,7 +21,7 @@ export default class AIImageAnalyzerPlugin extends Plugin { async onload() { debugLog('loading ai image analyzer plugin'); - await loadSettings(); + await loadSettings(this); setOllama(new Ollama({host: `${settings.ollamaHost}:${settings.ollamaPort}`})); await checkOllama(); diff --git a/src/ollamaManager.ts b/src/ollamaManager.ts index 496f6a7..937bf6e 100644 --- a/src/ollamaManager.ts +++ b/src/ollamaManager.ts @@ -4,7 +4,6 @@ import {ChatResponse, Ollama} from "ollama"; import {debugLog, isImageFile} from "./util"; import {settings} from "./settings"; -const promt = 'Describe the image. Just use Keywords. For example: cat, dog, tree. This must be Computer readable. The provided pictures are used in an notebook. Please provide at least 5 Keywords. It will be used to search for the image later.'; let ollama: Ollama; export async function analyzeImage(file: TFile): Promise { @@ -31,7 +30,7 @@ export async function analyzeImage(file: TFile): Promise { const response: ChatResponse = await ollama.chat({ model: settings.ollamaModel.model, //llava:13b or llava or llava-llama3 - messages: [{role: 'user', content: promt, images: [data]}], + messages: [{role: 'user', content: settings.prompt, images: [data]}], }); debugLog(response); diff --git a/src/settings.ts b/src/settings.ts index e66acc8..7123498 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -11,6 +11,7 @@ interface AIImageAnalyzerPluginSettings { ollamaHost: string; ollamaPort: number; ollamaModel: Model; + prompt: string; } const DEFAULT_SETTINGS: AIImageAnalyzerPluginSettings = { @@ -18,17 +19,18 @@ const DEFAULT_SETTINGS: AIImageAnalyzerPluginSettings = { ollamaHost: '127.0.0.1', ollamaPort: 11434, ollamaModel: possibleModels[0], + prompt: 'Describe the image. Just use Keywords. For example: cat, dog, tree. This must be Computer readable. The provided pictures are used in an notebook. Please provide at least 5 Keywords. It will be used to search for the image later.', } export let settings: AIImageAnalyzerPluginSettings = Object.assign({}, DEFAULT_SETTINGS); -export async function loadSettings() { - settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); +export async function loadSettings(plugin: AIImageAnalyzerPlugin) { + settings = Object.assign({}, DEFAULT_SETTINGS, await plugin.loadData()); } -export async function saveSettings() { +export async function saveSettings(plugin: AIImageAnalyzerPlugin) { setOllama(new Ollama({host: `${settings.ollamaHost}:${settings.ollamaPort}`})); - await this.saveData(settings); + await plugin.saveData(settings); } export class AIImageAnalyzerSettingsTab extends PluginSettingTab { @@ -55,7 +57,7 @@ export class AIImageAnalyzerSettingsTab extends PluginSettingTab { .setValue(possibleModels.find(model => model.model === settings.ollamaModel.model)!.name) .onChange(async (value) => { settings.ollamaModel = possibleModels.find(model => model.name === value)!; - await saveSettings(); + await saveSettings(this.plugin); })); new Setting(containerEl) @@ -82,7 +84,7 @@ export class AIImageAnalyzerSettingsTab extends PluginSettingTab { .setValue(settings.debug) .onChange(async (value) => { settings.debug = value; - await saveSettings(); + await saveSettings(this.plugin); })); new Setting(containerEl).setName('Ollama server').setHeading(); @@ -95,12 +97,10 @@ export class AIImageAnalyzerSettingsTab extends PluginSettingTab { .setValue(settings.ollamaHost) .onChange(async (value) => { if (value.length === 0) { - settings.ollamaHost = '127.0.0.1'; - await saveSettings(); - return; + value = DEFAULT_SETTINGS.ollamaHost; } settings.ollamaHost = value; - await saveSettings(); + await saveSettings(this.plugin); })); new Setting(containerEl) @@ -110,13 +110,32 @@ export class AIImageAnalyzerSettingsTab extends PluginSettingTab { .setPlaceholder('Enter the port (11434)') .setValue(settings.ollamaPort.toString()) .onChange(async (value) => { - if (isNaN(parseInt(value))) { - settings.ollamaPort = 11434; - await saveSettings(); - return; + let port = parseInt(value); + if (isNaN(port)) { + port = DEFAULT_SETTINGS.ollamaPort; } - settings.ollamaPort = parseInt(value); - await saveSettings(); + settings.ollamaPort = port; + await saveSettings(this.plugin); })); + + new Setting(containerEl).setName('Advanced').setHeading(); + + new Setting(containerEl) + .setName('Prompt') + .setDesc('Set the prompt for the Ollama model') + .addTextArea(text => { + text.inputEl.rows = 5; + text.inputEl.cols = 50; + return text + .setPlaceholder('Enter the prompt') + .setValue(settings.prompt) + .onChange(async (value) => { + if (value.length === 0) { + value = DEFAULT_SETTINGS.prompt; + } + settings.prompt = value; + await saveSettings(this.plugin); + }); + }); } }