Skip to content

Commit

Permalink
✨ Modify Prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
Swaggeroo committed Jul 17, 2024
1 parent 6898bed commit 546f5e6
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 20 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
3 changes: 1 addition & 2 deletions src/ollamaManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> {
Expand All @@ -31,7 +30,7 @@ export async function analyzeImage(file: TFile): Promise<string> {

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);
Expand Down
51 changes: 35 additions & 16 deletions src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,26 @@ interface AIImageAnalyzerPluginSettings {
ollamaHost: string;
ollamaPort: number;
ollamaModel: Model;
prompt: string;
}

const DEFAULT_SETTINGS: AIImageAnalyzerPluginSettings = {
debug: false,
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 {
Expand All @@ -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)
Expand All @@ -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();
Expand All @@ -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)
Expand All @@ -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);
});
});
}
}

0 comments on commit 546f5e6

Please sign in to comment.