diff --git a/app/api/chat-stream/route.ts b/app/api/chat-stream/route.ts index ad40c6be1..ddd6f4efa 100644 --- a/app/api/chat-stream/route.ts +++ b/app/api/chat-stream/route.ts @@ -1,5 +1,6 @@ import { createParser } from "eventsource-parser"; import { NextRequest } from "next/server"; +import { BASE_PATH } from "openai/base"; async function createStream(req: NextRequest) { const encoder = new TextEncoder(); @@ -13,7 +14,12 @@ async function createStream(req: NextRequest) { console.log("[Stream] using user api key"); } - const res = await fetch("https://api.openai.com/v1/chat/completions", { + let basePath = BASE_PATH; + if (process.env.OPENAI_API_BASE_PATH) { + basePath = process.env.OPENAI_API_BASE_PATH.replace(/\/+$/, ""); + } + + const res = await fetch(basePath + "/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..d6bdfc41b 100644 --- a/app/api/chat/route.ts +++ b/app/api/chat/route.ts @@ -10,10 +10,16 @@ export async function POST(req: Request) { apiKey = userApiKey; } + let basePath = undefined; + if (process.env.OPENAI_API_BASE_PATH) { + basePath = process.env.OPENAI_API_BASE_PATH.replace(/\/+$/, ""); + } + const openai = new OpenAIApi( new Configuration({ apiKey, - }) + }), + basePath ); const requestBody = (await req.json()) as ChatRequest;