add basePath for api env inject

This commit is contained in:
Rentoo
2023-03-28 03:43:26 +08:00
parent c93a46a02f
commit e6241bf405
2 changed files with 15 additions and 8 deletions

View File

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