From 3a610fe447b12d23d0686680fcd3f419302577a7 Mon Sep 17 00:00:00 2001 From: Mike Bybee Date: Mon, 19 Feb 2024 16:18:24 -0600 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20feat:=20add=20custom=20URL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.ts | 17 ++++++++++++++++- utils/openai.ts | 3 ++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/main.ts b/main.ts index 3e4df61..b5194c5 100644 --- a/main.ts +++ b/main.ts @@ -17,6 +17,7 @@ import { getClient, getGeneration } from "utils/openai"; interface AutogenSettings { openaiApiKey: string; + customURL?: string; model: ChatCompletionCreateParamsBase["model"]; triggerRegex: string; windowSize: number; @@ -25,6 +26,7 @@ interface AutogenSettings { const DEFAULT_SETTINGS: AutogenSettings = { openaiApiKey: "", + customURL, model: "gpt-3.5-turbo", triggerRegex: "@\\[(.*?)\\]", windowSize: 8000, @@ -59,7 +61,7 @@ export default class Autogen extends Plugin { initOpenAIClient() { if (this.settings.openaiApiKey) { - this.openaiClient = getClient(this.settings.openaiApiKey); + this.openaiClient = getClient(this.settings.openaiApiKey,); } } @@ -330,6 +332,19 @@ class AutogenSettingTab extends PluginSettingTab { }) ); + new Setting(containerEl) + .setName("Custom URL") + .setDesc("Custom URL (e.g. for proxy or local models with OpenAI-compatible API)") + .addText((text) => + text + .setPlaceholder("Custom URL (leave blank for OpenAI default)") + .setValue(this.plugin.settings.customURL) + .onChange(async (value) => { + this.plugin.settings.customURL = value; + await this.plugin.saveSettings(); + }) + ); + new Setting(containerEl) .setName("Model") .setDesc("The model to use for generating text") diff --git a/utils/openai.ts b/utils/openai.ts index bebb193..3323d1e 100644 --- a/utils/openai.ts +++ b/utils/openai.ts @@ -1,9 +1,10 @@ import OpenAI from "openai"; import { ChatCompletionCreateParamsBase } from "openai/resources/chat/completions"; -export const getClient = (apiKey: string) => { +export const getClient = (apiKey: string, customURL?: string) => { return new OpenAI({ apiKey, + customURL, dangerouslyAllowBrowser: true, }); }; From f0715a4fbe51a259c434d902899e5a056b25f4a3 Mon Sep 17 00:00:00 2001 From: Mike Bybee Date: Mon, 19 Feb 2024 16:29:58 -0600 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A9=B9=20fix:=20apply=20settings.cust?= =?UTF-8?q?omURL=20to=20initOpenAIClient?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- main.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/main.ts b/main.ts index b5194c5..59b8aa0 100644 --- a/main.ts +++ b/main.ts @@ -61,7 +61,11 @@ export default class Autogen extends Plugin { initOpenAIClient() { if (this.settings.openaiApiKey) { - this.openaiClient = getClient(this.settings.openaiApiKey,); + if (this.settings.customURL) { + this.openaiClient = getClient(this.settings.openaiApiKey, this.settings.customURL); + } else { + this.openaiClient = getClient(this.settings.openaiApiKey); + } } } @@ -334,7 +338,7 @@ class AutogenSettingTab extends PluginSettingTab { new Setting(containerEl) .setName("Custom URL") - .setDesc("Custom URL (e.g. for proxy or local models with OpenAI-compatible API)") + .setDesc("Set a custom URL (e.g. for proxy or local models with OpenAI-compatible API)") .addText((text) => text .setPlaceholder("Custom URL (leave blank for OpenAI default)")