From c652beb446cf270ef59c1f1153254bb368dae217 Mon Sep 17 00:00:00 2001 From: Milan Gruner Date: Mon, 4 Mar 2024 10:39:39 +0100 Subject: [PATCH] Fix missing API keys at build time error in chat route Signed-off-by: Milan Gruner --- app/src/app/api/v0/chat/route.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/app/src/app/api/v0/chat/route.ts b/app/src/app/api/v0/chat/route.ts index fea6eb991..92d7d6178 100644 --- a/app/src/app/api/v0/chat/route.ts +++ b/app/src/app/api/v0/chat/route.ts @@ -6,10 +6,18 @@ import OpenAI from "openai"; import { ChatCompletionMessageParam } from "openai/resources/chat/completions"; import { apiHandler } from "@/util/api"; -const Hf = new HfInference(process.env.HUGGINGFACE_API_KEY); -const openai = new OpenAI({ - apiKey: process.env.OPENAI_API_KEY, -}); +let Hf, openai; + +function setupModels() { + if (!Hf) { + Hf = new HfInference(process.env.HUGGINGFACE_API_KEY); + } + if (!openai) { + openai = new OpenAI({ + apiKey: process.env.OPENAI_API_KEY, + }); + } +} // export const runtime = "edge"; @@ -90,6 +98,7 @@ export const POST = apiHandler(async (req: Request) => { }); } + setupModels(); if (process.env.CHAT_PROVIDER === "openai") { return handleOpenAIChat(messages); } else {