generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
53 lines (43 loc) · 1.2 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { Editor, Plugin } from "obsidian";
import { Configuration, OpenAIApi } from "openai";
import { SettingTab } from "./settings";
interface OpenAIPluginSettings {
apiKey: string;
}
const DEFAULT_SETTINGS: Partial<OpenAIPluginSettings> = {
apiKey: "",
};
export default class OpenAIPlugin extends Plugin {
settings: OpenAIPluginSettings;
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
async loadOpenAi() {
const configuration = new Configuration({
apiKey: this.settings.apiKey,
});
return new OpenAIApi(configuration);
}
async onload() {
await this.loadSettings();
this.addSettingTab(new SettingTab(this.app, this));
this.addCommand({
id: "openai",
name: "Run Prompt",
editorCallback: async (editor: Editor) => {
const openai = await this.loadOpenAi();
const selection = editor.getSelection();
const completion = await openai.createCompletion("text-davinci-001", {
prompt: selection,
temperature: 0.8,
top_p: 1,
max_tokens: 500,
});
editor.replaceSelection(selection + completion.data.choices[0].text);
},
});
}
}