Merge pull request #2 from stereoplegic/feat/custom-urls

 feat: add custom URL
This commit is contained in:
Aidan Tilgner 2024-02-19 15:01:08 -08:00 committed by GitHub
commit 37a8bfa9ab
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 22 additions and 2 deletions

21
main.ts
View file

@ -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,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);
}
}
}
@ -330,6 +336,19 @@ class AutogenSettingTab extends PluginSettingTab {
})
);
new Setting(containerEl)
.setName("Custom URL")
.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)")
.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")

View file

@ -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,
});
};