Skip to content

Commit

Permalink
Added ability to change models using dropdown menu
Browse files Browse the repository at this point in the history
  • Loading branch information
pfrankov committed Dec 12, 2023
1 parent d5c25d7 commit f115a24
Show file tree
Hide file tree
Showing 5 changed files with 132 additions and 38 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,15 @@ You can install this plugin via [BRAT](https://obsidian.md/plugins?id=obsidian42
4. Click on `+` icon and press hotkey (e.g. `⌘ + M`)

## Roadmap
- [ ] Ability to select models from the list instead of typing their names
- [x] Ability to select models from the list instead of typing their names
- [ ] Ability to share and apply presets (system prompt + prompt + model)
- [ ] Additional AI providers (OpenAI, etc...)
- [ ] Changing order of the prompts
- [ ] Accounting your local documents in results as described here https://ollama.ai/blog/llms-in-obsidian

## Other AI providers
If you would like to use other providers, please let me know [in the discussions](https://github.com/pfrankov/obsidian-local-gpt/discussions/1).

## My other Obsidian plugins
- [Colored Tags](https://github.com/pfrankov/obsidian-colored-tags) that colorizes tags in distinguishable colors.

Expand Down
115 changes: 85 additions & 30 deletions src/LocalGPTSettingTab.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {App, Notice, PluginSettingTab, Setting} from "obsidian";
import {App, Notice, PluginSettingTab, requestUrl, Setting} from "obsidian";
import {DEFAULT_SETTINGS} from "defaultSettings";
import LocalGPT from "./main";
import {LocalGPTAction} from "./interfaces";
Expand All @@ -7,6 +7,7 @@ export class LocalGPTSettingTab extends PluginSettingTab {
plugin: LocalGPT;
editEnabled: boolean = false;
isNew: boolean = false;
modelsOptions: any = {};

constructor(app: App, plugin: LocalGPT) {
super(app, plugin);
Expand All @@ -18,31 +19,79 @@ export class LocalGPTSettingTab extends PluginSettingTab {

containerEl.empty();

new Setting(containerEl)
.setName("Ollama URL")
.setDesc("Default is http://localhost:11434")
.addText((text) =>
text
.setPlaceholder("http://localhost:11434")
.setValue(this.plugin.settings.ollamaUrl)
const aiProvider = new Setting(containerEl)
.setName('AI provider')
.setDesc('')
.addDropdown(dropdown =>
dropdown.addOptions({
'ollama': 'Ollama',
})
.setValue(String(this.plugin.settings.selectedProvider))
.onChange(async (value) => {
this.plugin.settings.ollamaUrl = value;
this.plugin.settings.selectedProvider = value;
await this.plugin.saveSettings();
this.display()
})
);
)

new Setting(containerEl)
.setName("Default model")
.setDesc("Name of the default Ollama model to use for prompts")
.addText((text) =>
text
.setPlaceholder("llama2")
.setValue(this.plugin.settings.defaultModel)
.onChange(async (value) => {
this.plugin.settings.defaultModel = value;
await this.plugin.saveSettings();
aiProvider.descEl.innerHTML = `If you would like to use other providers, please let me know <a href="https://github.com/pfrankov/obsidian-local-gpt/discussions/1">in the discussions</a>`

if (this.plugin.settings.selectedProvider === 'ollama') {
new Setting(containerEl)
.setName("Ollama URL")
.setDesc("Default is http://localhost:11434")
.addText((text) =>
text
.setPlaceholder("http://localhost:11434")
.setValue(this.plugin.settings.providers.ollama.ollamaUrl)
.onChange(async (value) => {
this.plugin.settings.providers.ollama.ollamaUrl = value;
await this.plugin.saveSettings();
})
);

const ollamaDefaultModel = new Setting(containerEl)
.setName("Default model")
.setDesc("Name of the default Ollama model to use for prompts")
if (this.plugin.settings.providers.ollama.ollamaUrl) {
requestUrl(`${this.plugin.settings.providers.ollama.ollamaUrl}/api/tags`)
.then(({json}) => {
if (!json.models || json.models.length === 0) {
return Promise.reject();
}
this.modelsOptions = json.models.reduce((acc: any, el:any) => {
const name = el.name.replace(":latest", "");
acc[name] = name;
return acc;
}, {})

ollamaDefaultModel
.addDropdown(dropdown =>
dropdown.addOptions(this.modelsOptions)
.setValue(String(this.plugin.settings.providers.ollama.defaultModel))
.onChange(async (value) => {
this.plugin.settings.providers.ollama.defaultModel = value;
await this.plugin.saveSettings();
})

)
.addButton((button) =>
button.setIcon('refresh-cw').onClick(async () => {
this.display()
})
)
})
);
.catch(() => {
ollamaDefaultModel.descEl.innerHTML = `Get the models from <a href="https://ollama.ai/library">Ollama library</a> or check that Ollama URL is correct.`
ollamaDefaultModel.addButton((button) =>
button.setIcon('refresh-cw').onClick(async () => {
this.display()
})
)
})
}
}


const editingAction: LocalGPTAction = {
name: "",
Expand Down Expand Up @@ -97,15 +146,21 @@ export class LocalGPTSettingTab extends PluginSettingTab {
});
});

new Setting(containerEl)
.setName("Model")
.setDesc('Optional')
.addText((text) => {
text.setPlaceholder(this.plugin.settings.defaultModel);
text.onChange(async (value) => {
editingAction.model = value;
});
});
if (this.plugin.settings.selectedProvider === 'ollama') {
new Setting(containerEl)
.setName("Model")
.setDesc('Optional')
.addDropdown(dropdown =>
dropdown
.addOption('', 'Default model')
.addOptions(this.modelsOptions)
.onChange(async (value) => {
this.plugin.settings.providers.ollama.defaultModel = value;
await this.plugin.saveSettings();
})

)
}

new Setting(containerEl)
.setName("Replace selected text")
Expand Down
10 changes: 8 additions & 2 deletions src/defaultSettings.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import {LocalGPTSettings} from "./interfaces";

export const DEFAULT_SETTINGS: LocalGPTSettings = {
ollamaUrl: "http://localhost:11434",
defaultModel: "orca-mini",
providers: {
ollama: {
ollamaUrl: "http://localhost:11434",
defaultModel: "orca-mini",
}
},
selectedProvider: 'ollama',
actions: [
{
name: "🪄 General help",
Expand Down Expand Up @@ -31,4 +36,5 @@ export const DEFAULT_SETTINGS: LocalGPTSettings = {
system: "You are an AI assistant that follows instruction extremely well. Help as much as you can."
}
],
_version: 1
};
12 changes: 10 additions & 2 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
export interface LocalGPTSettings {
ollamaUrl: string;
defaultModel: string;
selectedProvider: string;
providers: OllamaProvider;
actions: LocalGPTAction[];
_version: number;
}

export interface OllamaProvider {
ollama: {
ollamaUrl: string;
defaultModel: string;
}
}

export interface LocalGPTAction {
Expand Down
28 changes: 25 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export default class LocalGPT extends Plugin {
this.settings.actions.forEach((action) => {
const requestBody: OllamaRequestBody = {
prompt: action.prompt + "\n\n" + text,
model: action.model || this.settings.defaultModel,
model: action.model || this.settings.providers.ollama.defaultModel,
options: {
temperature: action.temperature || 0.2,
},
Expand All @@ -54,7 +54,7 @@ export default class LocalGPT extends Plugin {

requestUrl({
method: "POST",
url: `${this.settings.ollamaUrl}/api/generate`,
url: `${this.settings.providers.ollama.ollamaUrl}/api/generate`,
body: JSON.stringify(requestBody)
})
.then(({json}) => {
Expand Down Expand Up @@ -98,7 +98,29 @@ export default class LocalGPT extends Plugin {
}

async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
const loadedData:LocalGPTSettings = await this.loadData();
let needToSave = false;

if (loadedData && !loadedData._version || loadedData._version < 1) {
needToSave = true;

loadedData.providers = DEFAULT_SETTINGS.providers;
// @ts-ignore
loadedData.providers.ollama.ollamaUrl = loadedData.ollamaUrl;
// @ts-ignore
delete loadedData.ollamaUrl;
// @ts-ignore
loadedData.providers.ollama.defaultModel = loadedData.defaultModel;
// @ts-ignore
delete loadedData.defaultModel;
loadedData.selectedProvider = DEFAULT_SETTINGS.selectedProvider;
loadedData._version = 2;
}
this.settings = Object.assign({}, DEFAULT_SETTINGS, loadedData);

if (needToSave) {
await this.saveData(this.settings);
}
}

reload() {
Expand Down

0 comments on commit f115a24

Please sign in to comment.