diff --git a/src/renderer/plugins/editor-openai.ts b/src/renderer/plugins/editor-openai.ts index 4089a1d8..87b87ae1 100644 --- a/src/renderer/plugins/editor-openai.ts +++ b/src/renderer/plugins/editor-openai.ts @@ -4,7 +4,9 @@ import { getLogger } from '@fe/utils' const actionName = 'plugin.editor-openai.trigger' const settingKeyToken = 'plugin.editor-openai.api-token' +const settingKeyEngine = 'plugin.editor-openai.engine-id' const settingKeyArgs = 'plugin.editor-openai.args-json' +const defaultEngine = 'text-davinci-002' class CompletionProvider implements Monaco.languages.InlineCompletionsProvider { private readonly monaco: typeof Monaco @@ -56,7 +58,6 @@ class CompletionProvider implements Monaco.languages.InlineCompletionsProvider { position.column, ) - const url = 'https://api.openai.com/v1/engines/text-davinci-002/completions' const token = this.ctx.setting.getSetting(settingKeyToken, '') if (token.length < 40) { @@ -77,24 +78,29 @@ class CompletionProvider implements Monaco.languages.InlineCompletionsProvider { } const body = { - prompt: text, - temperature: 0, + temperature: 0.4, max_tokens: 100, n: 1, top_p: 1, frequency_penalty: 0.0, presence_penalty: 0.0, - ...args + // stop: '\n', + ...args, + prompt: text, } + const engineId = this.ctx.setting.getSetting('plugin.editor-openai.engine-id', defaultEngine) + + this.logger.debug('provideSuggestions', 'request', engineId, body) + try { const res = await this.ctx.api.proxyRequest( - url, + `https://api.openai.com/v1/engines/${engineId}/completions`, { headers, body: JSON.stringify(body), method: 'post' }, true ).then(x => x.json()) - this.logger.debug('provideSuggestions', res) + this.logger.debug('provideSuggestions', 'result', res) if (!res.choices) { this.ctx.ui.useToast().show('warning', JSON.stringify(res), 5000) @@ -107,7 +113,7 @@ class CompletionProvider implements Monaco.languages.InlineCompletionsProvider { })) } catch (error: any) { this.ctx.ui.useToast().show('warning', error.message || `${error}`, 5000) - this.logger.error('provideSuggestions', error) + this.logger.error('provideSuggestions', 'error', error) throw error } } @@ -124,23 +130,40 @@ export default { }) ctx.setting.changeSchema((schema) => { + schema.groups.push({ label: 'OpenAI' as any, value: 'openai' }) + schema.properties[settingKeyToken] = { title: 'T_openai.api-token', description: 'T_openai.api-token-desc', type: 'string', defaultValue: '', - group: 'editor', + group: 'openai', options: { inputAttributes: { placeholder: 'sk-' + 'x'.repeat(10) } }, } + schema.properties[settingKeyEngine] = { + title: 'T_openai.engine-id', + description: 'T_openai.engine-id-desc', + type: 'string', + defaultValue: defaultEngine, + group: 'openai', + enum: [ + 'text-davinci-002', + 'text-curie-001', + 'text-babbage-001', + 'text-ada-001', + ], + required: true, + } + schema.properties[settingKeyArgs] = { title: 'T_openai.args-json', description: 'T_openai.args-json-desc', type: 'string', defaultValue: '{"max_tokens": 256}', - group: 'editor', + group: 'openai', options: { inputAttributes: { placeholder: '{"max_tokens": 256}' } }, @@ -153,10 +176,15 @@ export default { handler: () => { ctx.editor.getEditor().getAction('editor.action.inlineSuggest.trigger').run() ctx.editor.getEditor().focus() - } + }, + when: () => ctx.store.state.showEditor && !ctx.store.state.presentation, }) ctx.statusBar.tapMenus((menus) => { + if (!ctx.store.state.showEditor || ctx.store.state.presentation) { + return + } + menus['status-bar-tool']?.list?.push( { id: actionName, diff --git a/src/renderer/types.ts b/src/renderer/types.ts index c40465b1..efcc1b18 100644 --- a/src/renderer/types.ts +++ b/src/renderer/types.ts @@ -118,6 +118,7 @@ export type BuildInSettings = { 'plugin.image-hosting-picgo.server-url': string, 'plugin.image-hosting-picgo.enable-paste-image': boolean, 'plugin.editor-openai.api-token': string, + 'plugin.editor-openai.engine-id': string, 'plugin.editor-openai.args-json': string, 'license': string, 'mark': FileItem[], diff --git a/src/share/i18n/languages/en.ts b/src/share/i18n/languages/en.ts index 3bb4dd66..00f9f5f8 100644 --- a/src/share/i18n/languages/en.ts +++ b/src/share/i18n/languages/en.ts @@ -506,9 +506,11 @@ const data = { }, 'openai': { 'openai-complete': 'OpenAI Complete', - 'api-token': 'OpenAI Api Token', - 'api-token-desc': 'You can get your api token from openai.com', - 'args-json': 'OpenAI Args JSON', + 'api-token': 'Api Token', + 'api-token-desc': 'You can get your api token from openai.com', + 'engine-id': 'Engine Id', + 'engine-id-desc': 'Please refer to Engine Overview', + 'args-json': 'Custom Arguments', 'args-json-desc': 'JSON string like {"max_tokens": 256}', }, } diff --git a/src/share/i18n/languages/zh-CN.ts b/src/share/i18n/languages/zh-CN.ts index 1dd829ff..92cf02e8 100644 --- a/src/share/i18n/languages/zh-CN.ts +++ b/src/share/i18n/languages/zh-CN.ts @@ -507,9 +507,11 @@ const data: BaseLanguage = { }, 'openai': { 'openai-complete': 'OpenAI 自动补全', - 'api-token': 'OpenAI Api Token', - 'api-token-desc': '从 https://openai.com/ 获取', - 'args-json': 'OpenAI 参数', + 'api-token': 'Api Token', + 'api-token-desc': '你可以从 openai.com 获取', + 'engine-id': 'Engine Id', + 'engine-id-desc': '请参考 Engine Overview', + 'args-json': '自定义参数', 'args-json-desc': 'JSON 字符串如 {"max_tokens": 256}', }, }