supporting for customize BASE_PATH

This commit is contained in:
chu 2023-03-28 23:43:55 +08:00
parent 7f3cbaa064
commit 06d279ac56
2 changed files with 14 additions and 2 deletions

View File

@ -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}`,

View File

@ -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;