diff --git a/app/api/chat-stream/route.ts b/app/api/chat-stream/route.ts index ad40c6be1..2eb23d6b7 100644 --- a/app/api/chat-stream/route.ts +++ b/app/api/chat-stream/route.ts @@ -6,14 +6,18 @@ async function createStream(req: NextRequest) { const decoder = new TextDecoder(); let apiKey = process.env.OPENAI_API_KEY; - + let apiBasePath = process.env.OPENAI_API_BASE_PATH; + if (apiBasePath) { + apiBasePath = apiBasePath.replace(/\/+$/, ""); + } + const userApiKey = req.headers.get("token"); if (userApiKey) { apiKey = userApiKey; console.log("[Stream] using user api key"); } - const res = await fetch("https://api.openai.com/v1/chat/completions", { + const res = await fetch(`${apiBasePath ? apiBasePath : 'https://api.openai.com/v1'}/chat/completions`, { headers: { "Content-Type": "application/json", Authorization: `Bearer ${apiKey}`, diff --git a/app/api/chat/route.ts b/app/api/chat/route.ts index 18c7db148..9414ca2b1 100644 --- a/app/api/chat/route.ts +++ b/app/api/chat/route.ts @@ -1,20 +1,23 @@ -import { OpenAIApi, Configuration } from "openai"; +import { OpenAIApi, Configuration, ConfigurationParameters } from "openai"; import { ChatRequest } from "./typing"; export async function POST(req: Request) { try { let apiKey = process.env.OPENAI_API_KEY; + let apiBasePath = process.env.OPENAI_API_BASE_PATH; const userApiKey = req.headers.get("token"); if (userApiKey) { apiKey = userApiKey; } + let configuration: ConfigurationParameters = { + apiKey, + }; + if (apiBasePath) { + configuration.basePath = apiBasePath.replace(/\/+$/, ""); + } - const openai = new OpenAIApi( - new Configuration({ - apiKey, - }) - ); + const openai = new OpenAIApi(new Configuration(configuration)); const requestBody = (await req.json()) as ChatRequest; const completion = await openai!.createChatCompletion({