feat: 增加Azure opanai 能力支持

This commit is contained in:
Duang Cheng
2023-04-19 19:45:37 +08:00
parent beb04d8181
commit 19949906c6
6 changed files with 89 additions and 9 deletions

View File

@@ -1,15 +1,21 @@
import { NextRequest } from "next/server";
const OPENAI_URL = "api.openai.com";
const AZURE_OPENAI_URL = "azure-openai-gpt.openai.azure.com";
const DEFAULT_PROTOCOL = "https";
const PROTOCOL = process.env.PROTOCOL ?? DEFAULT_PROTOCOL;
const BASE_URL = process.env.BASE_URL ?? OPENAI_URL;
export async function requestOpenai(req: NextRequest) {
const apiKey = req.headers.get("token");
const openaiPath = req.headers.get("path");
let baseUrl = BASE_URL;
let baseUrl = OPENAI_URL;
if (openaiPath?.includes("/deployments/")) {
baseUrl = AZURE_OPENAI_URL;
}
if (process.env.BASE_URL) {
baseUrl = process.env.BASE_URL;
}
if (!baseUrl.startsWith("http")) {
baseUrl = `${PROTOCOL}://${baseUrl}`;
@@ -22,6 +28,7 @@ export async function requestOpenai(req: NextRequest) {
headers: {
"Content-Type": "application/json",
Authorization: `Bearer ${apiKey}`,
"api-key": apiKey || "",
},
method: req.method,
body: req.body,