From de9be8c7ce3950221c150a572baaa25377305c66 Mon Sep 17 00:00:00 2001 From: arcticfly <41524992+arcticfly@users.noreply.github.com> Date: Fri, 11 Aug 2023 23:07:04 -0700 Subject: [PATCH] Allow custom config file (#143) * Allow custom config file * Temporarily remove dependency on local openpipe --- app/.gitignore | 3 +++ app/src/server/utils/openai.ts | 27 ++++++++++++++++++++------- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/app/.gitignore b/app/.gitignore index 354334f..4f31439 100644 --- a/app/.gitignore +++ b/app/.gitignore @@ -44,3 +44,6 @@ yarn-error.log* # Sentry Auth Token .sentryclirc + +# custom openai intialization +src/server/utils/openaiCustomConfig.json diff --git a/app/src/server/utils/openai.ts b/app/src/server/utils/openai.ts index 64ac3d8..95da03a 100644 --- a/app/src/server/utils/openai.ts +++ b/app/src/server/utils/openai.ts @@ -1,13 +1,26 @@ +import { type ClientOptions, default as OriginalOpenAI } from "openai"; +import fs from "fs"; +import path from "path"; + import { env } from "~/env.mjs"; -import { default as OriginalOpenAI } from "openai"; +// TODO: use local dependency // import { OpenAI } from "openpipe"; -const openAIConfig = { apiKey: env.OPENAI_API_KEY ?? "dummy-key" }; +let config: ClientOptions; -// Set a dummy key so it doesn't fail at build time -// export const openai = env.OPENPIPE_API_KEY -// ? new OpenAI.OpenAI(openAIConfig) -// : new OriginalOpenAI(openAIConfig); +try { + // Allow developers to override the config with a local file + const jsonData = fs.readFileSync( + path.join(path.dirname(import.meta.url).replace("file://", ""), "./openaiCustomConfig.json"), + "utf8", + ); + config = JSON.parse(jsonData.toString()); +} catch (error) { + // Set a dummy key so it doesn't fail at build time + config = { apiKey: env.OPENAI_API_KEY ?? "dummy-key" }; +} -export const openai = new OriginalOpenAI(openAIConfig); +// export const openai = env.OPENPIPE_API_KEY ? new OpenAI.OpenAI(config) : new OriginalOpenAI(config); + +export const openai = new OriginalOpenAI(config);