From ea566f30d531d0db677842c73b9554d89cd490b0 Mon Sep 17 00:00:00 2001 From: David Manouchehri Date: Thu, 29 Aug 2024 21:36:54 +0000 Subject: [PATCH] Allow setting non-default OpenAI base URLs. Uses env `OPENAI_BASE_URL` since it's also used by the OpenAI client: https://github.com/openai/openai-node/blob/6ae19ce08eea4f6b3c4865861a6cce09d403cac8/src/index.ts#L106 --- src/commands/openai.ts | 8 +++++++- src/plugins/openai/openai-rename.ts | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/commands/openai.ts b/src/commands/openai.ts index aa00928..2c62cbe 100644 --- a/src/commands/openai.ts +++ b/src/commands/openai.ts @@ -15,6 +15,11 @@ export const openai = cli() "-k, --apiKey ", "The OpenAI API key. Alternatively use OPENAI_API_KEY environment variable" ) + .option( + "--baseURL ", + "The OpenAI base server URL.", + env("OPENAI_BASE_URL") ?? "https://api.openai.com/v1" + ) .option("--verbose", "Show verbose output") .argument("input", "The input minified Javascript file") .action(async (filename, opts) => { @@ -23,9 +28,10 @@ export const openai = cli() } const apiKey = opts.apiKey ?? env("OPENAI_API_KEY"); + const baseURL = opts.baseURL; await unminify(filename, opts.outputDir, [ babel, - openaiRename({ apiKey, model: opts.model }), + openaiRename({ apiKey, baseURL, model: opts.model }), prettier ]); }); diff --git a/src/plugins/openai/openai-rename.ts b/src/plugins/openai/openai-rename.ts index 87b3e9b..f22791a 100644 --- a/src/plugins/openai/openai-rename.ts +++ b/src/plugins/openai/openai-rename.ts @@ -5,12 +5,14 @@ import { verbose } from "../../verbose.js"; export function openaiRename({ apiKey, + baseURL, model }: { apiKey: string; + baseURL: string; model: string; }) { - const client = new OpenAI({ apiKey }); + const client = new OpenAI({ apiKey, baseURL }); return async (code: string): Promise => { return await visitAllIdentifiers(