Support custom commands

This commit is contained in:
Simon Yang 2023-03-14 18:07:23 +11:00
parent 8fda69740f
commit 1116c8d185
4 changed files with 78 additions and 19 deletions

View file

@ -2,27 +2,27 @@
<a href="https://www.buymeacoffee.com/yzh503" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/v2/default-yellow.png" alt="Buy Me A Coffee" style="height: 40px !important;width: 150px !important;" ></a>
**AI Commander** is an open-source AI connector that helps you write notes with the power of OpenAI APIs in conjunction with Bing search engine. It performs similar to the new bing chat on precise mode.
**AI Commander** is an open-source AI connector that helps you write notes with the power of OpenAI APIs in conjunction with Bing search engine.
[OpenAI API key](https://platform.openai.com/account/api-keys) is required to use this plugin.
# Current Features
## Current Features
- Generate text from prompt, selected lines or current line.
- Generate image from prompt, selected lines or current line.
- Generate audio transcript from the last audio file above the current line.
- Generate text in conjunction with search engine results. This allows the text model to work with Bing search engine. A [Bing Web Search API](https://www.microsoft.com/en-us/bing/apis/bing-web-search-api) key is required to use this feature.
- Generate text in context of the PDF attachment embedded in the note. In this mode, it will not incorporate the search result.
- Generate text in context of the **PDF** attachment embedded in the note. In this mode, it will not incorporate the search result.
- Use [Prompt Perfect](https://promptperfect.jina.ai/) to automatically improve prompts. Your prompts will be replaced by the improved one.
- Custom prompt commands for selected text and PDF. For example, add a command "Summarise the text" in the plugin settings, and it will appear as a command. Note that you need to **reload** the plugin by disabling and re-enabling it after changing the Custom Commands setting.
# Supported models:
## Supported models
- OpenAI gpt-3.5-turbo
- OpenAI Whisper
- OpenAI image generation model
# How to use
## How to use
Call out the command pallette and try the following commands:
@ -37,8 +37,8 @@ Call out the command pallette and try the following commands:
- AI Commander: Generate text from the current line in context of the above PDF
- AI Commander: Generate text from the selected text in context of the above PDF
# Install from Github
## Install from Github
1. Move manifest.json and main.js to <vault>/.obsidian/plugins/obsidian-aicommander-plugin
1. Move `manifest.json` and `main.js` to `<vault>/.obsidian/plugins/obsidian-aicommander-plugin`
2. Refresh installed plugins
2. Enable AI Commander
3. Enable AI Commander

67
main.ts
View file

@ -12,6 +12,8 @@ interface AICommanderPluginSettings {
bingSearchKey: string;
usePromptPerfect: boolean;
promptPerfectKey: string;
promptsForSelected: string;
promptsForPdf: string
}
const DEFAULT_SETTINGS: AICommanderPluginSettings = {
@ -23,6 +25,8 @@ const DEFAULT_SETTINGS: AICommanderPluginSettings = {
bingSearchKey: '',
promptPerfectKey: '',
usePromptPerfect: false,
promptsForSelected: '',
promptsForPdf: ''
}
export default class AICommanderPlugin extends Plugin {
@ -467,6 +471,37 @@ export default class AICommanderPlugin extends Plugin {
}
});
const extraCommandsForSelected = this.settings.promptsForSelected.split('\n');
for (let command of extraCommandsForSelected) {
command = command.trim();
if (command == null || command == undefined || command.length < 1) continue;
const cid = command.toLowerCase().replace(/ /g, '-');
this.addCommand({
id: cid,
name: command,
editorCallback: (editor: Editor, view: MarkdownView) => {
const selectedText = editor.getSelection();
const prompt = 'You are an assistant who can learn from the text I give to you. Here is the text:\n\n' + selectedText + '\n\n' + command;
this.commandGenerateText(editor, prompt, false, true);
}
});
}
const extraCommandsForPdf = this.settings.promptsForPdf.split('\n');
for (let command of extraCommandsForPdf) {
command = command.trim();
if (command == null || command == undefined || command.length < 1) continue;
const cid = command.toLowerCase().replace(/ /g, '-');
this.addCommand({
id: cid,
name: command,
editorCallback: (editor: Editor, view: MarkdownView) => {
this.commandGenerateTextWithPdf(editor, command, false, false);
}
});
}
// This adds a settings tab so the user can configure various aspects of the plugin
this.addSettingTab(new ApiSettingTab(this.app, this));
@ -504,8 +539,7 @@ class ApiSettingTab extends PluginSettingTab {
display(): void {
const {containerEl} = this;
containerEl.empty();
containerEl.createEl('h2', {text: 'Settings'});
containerEl.createEl('h3', {text: 'OpenAI API'});
containerEl.createEl('h2', {text: 'OpenAI API'});
new Setting(containerEl)
.setName('OpenAI API key')
@ -542,7 +576,7 @@ class ApiSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
}));
containerEl.createEl('h3', {text: 'Search Engine'});
containerEl.createEl('h2', {text: 'Search Engine'});
new Setting(containerEl)
.setName('Use search engine')
@ -576,7 +610,7 @@ class ApiSettingTab extends PluginSettingTab {
await this.plugin.saveSettings();
}));
containerEl.createEl('h3', {text: 'Prompt Perfect'});
containerEl.createEl('h2', {text: 'Prompt Perfect'});
new Setting(containerEl)
.setName('Use Prompt Perfect')
@ -599,5 +633,30 @@ class ApiSettingTab extends PluginSettingTab {
this.plugin.settings.promptPerfectKey = value;
await this.plugin.saveSettings();
}));
containerEl.createEl('h2', {text: 'Custom Commands'});
containerEl.createEl('p', {text: 'Reload the plugin after changing below settings'});
new Setting(containerEl)
.setName('Custom command for selected text')
.setDesc('Fill in your prompts line by line. They will appear as commands.')
.addTextArea(text => text
.setPlaceholder('Summarise the selected text\nTranslate into English')
.setValue(this.plugin.settings.promptsForSelected)
.onChange(async (value) => {
this.plugin.settings.promptsForSelected = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Custom command for PDF')
.setDesc('Fill in your prompts line by line. They will appear as commands.')
.addTextArea(text => text
.setPlaceholder('Summarise the PDF')
.setValue(this.plugin.settings.promptsForPdf)
.onChange(async (value) => {
this.plugin.settings.promptsForPdf = value;
await this.plugin.saveSettings();
}));
}
}

View file

@ -1,7 +1,7 @@
{
"id": "ai-commander",
"name": "AI Commander",
"version": "1.1.2",
"version": "1.2.0",
"minAppVersion": "1.0.0",
"description": "Generate audio transcripts, images, and text in context of PDF attachments or web search results using OpenAI and Bing API.",
"author": "Simon Yang",

View file

@ -1,3 +1,3 @@
{
"1.1.2": "1.0.0"
"1.2.0": "1.0.0"
}