Last active
September 3, 2025 02:03
-
-
Save TutorialDoctor/3022091867ba8ec23634f6d9a20231ea to your computer and use it in GitHub Desktop.
A simple ollama plugin for obsidian
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import { Notice, Plugin, Editor, MarkdownView, PluginSettingTab, Setting } from 'obsidian'; | |
| import ollama from 'ollama/browser' | |
| interface ModelSettings { | |
| model: string; | |
| systemPrompt: string; | |
| } | |
| const DEFAULT_SETTINGS: ModelSettings = { | |
| model: 'llama3.2' | |
| } | |
| export default class OllamaPlugin extends Plugin { | |
| settings: ModelSettings; | |
| async onload() { | |
| await this.loadSettings(); | |
| this.addRibbonIcon('message-circle', 'Greet', async () => { | |
| const view = this.app.workspace.getActiveViewOfType(MarkdownView); | |
| if (view && view.editor) { | |
| const prompt = view.editor.getSelection() | |
| view.editor.setValue('Please wait for your response....') | |
| let currentText = "" | |
| const message = { role: 'user', content: this.settings.systemPrompt + "\n\n" + prompt } | |
| console.log(this.settings.systemPrompt + "\n\n" + prompt) | |
| const response = await ollama.chat({ | |
| model: this.settings.model, | |
| messages: [message], stream: true | |
| }) | |
| for await (const part of response) { | |
| currentText += part.message.content | |
| view.editor.setValue(currentText) | |
| } | |
| } | |
| }); | |
| // This adds a settings tab so the user can configure various aspects of the plugin | |
| this.addSettingTab(new SampleSettingTab(this.app, this)); | |
| } | |
| onunload() { } | |
| async loadSettings() { | |
| this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); | |
| } | |
| async saveSettings() { | |
| await this.saveData(this.settings); | |
| } | |
| } | |
| class SampleSettingTab extends PluginSettingTab { | |
| plugin: OllamaPlugin; | |
| constructor(app: App, plugin: OllamaPlugin) { | |
| super(app, plugin); | |
| this.plugin = plugin; | |
| } | |
| display(): void { | |
| const { containerEl } = this; | |
| containerEl.empty(); | |
| new Setting(containerEl) | |
| .setName('AI Model') | |
| .setDesc('Model you want to use for prompting') | |
| .addText(text => text | |
| .setPlaceholder('Enter your model name') | |
| .setValue(this.plugin.settings.model) | |
| .onChange(async (value) => { | |
| this.plugin.settings.model = value; | |
| await this.plugin.saveSettings(); | |
| })); | |
| new Setting(containerEl) | |
| .setName('System Prompt') | |
| .addTextArea(text => { text | |
| .setPlaceholder("This is the personality of the AI") | |
| .setValue(this.plugin.settings.systemPrompt) | |
| .onChange(async (value)=>{ | |
| this.plugin.settings.systemPrompt = value; | |
| await this.plugin.saveSettings | |
| }) | |
| }); | |
| } | |
| } | |
| // This code was created by [The Tutorial Doctor](https://upskil.dev/) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment