Skip to content

Commit

Permalink
Merge pull request #2 from stevennguyen/ollama
Browse files Browse the repository at this point in the history
Add ollama support
nicucalcea authored May 10, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
2 parents 53441f5 + cd11721 commit cb12c7f
Showing 2 changed files with 70 additions and 12 deletions.
39 changes: 28 additions & 11 deletions main.ts
Original file line number Diff line number Diff line change
@@ -13,10 +13,16 @@ function selectLine(editor: Editor) {

interface MagicTasksPluginSettings {
openaiKey: string;
ollama_url: string;
ollama_model: string;
api_select: string;
}

const DEFAULT_SETTINGS: Partial<MagicTasksPluginSettings> = {
openaiKey: "sk-XXXXXX",
ollama_url: "http://127.0.0.1:11434/",
ollama_model: "llama3",
api_select: "openai"
};

export default class MagicTasksPlugin extends Plugin {
@@ -35,7 +41,7 @@ export default class MagicTasksPlugin extends Plugin {
selectLine(editor);
const selectedText = editor.getSelection();
// console.log('Selected Text:', selectedText);
const subtasks = await this.sendToOpenAI(selectedText);
const subtasks = await this.sendToModel(selectedText);
this.insertIndentedSubtasks(editor, subtasks);
},
});
@@ -61,17 +67,28 @@ export default class MagicTasksPlugin extends Plugin {

onunload() { }

async sendToOpenAI(text: string): Promise<string> {
async sendToModel(text: string): Promise<string> {
try {
// console.log('Sending to OpenAI:', text);
const response = await fetch(OPENAI_API_ENDPOINT, {
let endpoint = '';
let headers: {[key: string]: string} = {
'Content-Type': 'application/json'
};
let model = '';

if (this.settings.api_select === 'openai') {
endpoint = OPENAI_API_ENDPOINT;
headers['Authorization'] = `Bearer ${this.settings.openaiKey}`;
model = "gpt-3.5-turbo";
} else if (this.settings.api_select === 'ollama') {
endpoint = this.settings.ollama_url + 'v1/chat/completions';
model = this.settings.ollama_model;
}
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${this.settings.openaiKey}`,
},
headers: headers,
body: JSON.stringify({
"model": "gpt-3.5-turbo",
"model": model,
"messages": [
{ "role": "system", "content": "You are a helpful assistant that responds with markdown-formatted tasks in the format `- [ ] This is a subtask`. Please only respond with tasks, no other text." },
{ "role": "user", "content": `Please break down the following task into smaller subtasks: ${text}` } // Append selectedText to the user message
@@ -80,17 +97,17 @@ export default class MagicTasksPlugin extends Plugin {
});

if (!response.ok) {
console.error('Error sending request to OpenAI API');
console.error('Error sending request to model');
return '';
}

const data = await response.json();
const subtasks = data.choices[0].message.content;
// console.log('OpenAI data:', subtasks);
return subtasks;
// console.log('OpenAI data:', subtasks);
} catch (error) {
console.error('Error while calling OpenAI API:', error);
return '';
}
}
}
}
43 changes: 42 additions & 1 deletion settings.ts
Original file line number Diff line number Diff line change
@@ -26,5 +26,46 @@ export class MagicTasksSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Ollama URL")
.setDesc("Provide the URL for the Ollama API (include trailing slash)")
.addText((text) =>
text
.setPlaceholder("http://127.0.0.1:11434/")
.setValue(this.plugin.settings.ollama_url)
.onChange(async (value) => {
this.plugin.settings.ollama_url = value;
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("Ollama Model")
.setDesc("Provide the name of the Ollama model")
.addText((text) =>
text
.setPlaceholder("llama3")
.setValue(this.plugin.settings.ollama_model)
.onChange(async (value) => {
this.plugin.settings.ollama_model = value;
await this.plugin.saveSettings();
})
);

new Setting(containerEl)
.setName("API Provider")
.setDesc("Select the API provider")
.addDropdown((dropdown) =>
dropdown
.addOption("openai", "OpenAI")
.addOption("ollama", "Ollama")
.setValue(this.plugin.settings.api_select || "openai")
.onChange(async (value) => {
this.plugin.settings.api_select = value;
await this.plugin.saveSettings();
this.display();
})
);
}
}
}

0 comments on commit cb12c7f

Please sign in to comment.