This commit is contained in:
sijinhui
2024-07-11 13:09:18 +08:00
parent a70f984b0f
commit af741e4dbf
12 changed files with 5233 additions and 2873 deletions

View File

@@ -2,40 +2,40 @@ import { getServerSideConfig } from "@/app/config/server";
import { ModelProvider } from "@/app/constant";
import { prettyObject } from "@/app/utils/format";
import { NextRequest, NextResponse } from "next/server";
import { NextApiResponse, NextApiRequest } from "next";
import { auth } from "../../auth";
import { requestOpenai } from "../../common";
async function handle(
req: NextRequest,
res: NextApiResponse,
{ params }: { params: { path: string[] } },
) {
console.log("[Azure Route] params ", params);
if (req.method === "OPTIONS") {
return NextResponse.json({ body: "OK" }, { status: 200 });
return res.status(200).json({ body: "OK" });
}
const subpath = params.path.join("/");
const authResult = auth(req, ModelProvider.GPT);
if (authResult.error) {
return NextResponse.json(authResult, {
status: 401,
});
return res.status(401).json(authResult);
}
try {
return await requestOpenai(req);
return await requestOpenai(req, (await req.text()) as any);
} catch (e) {
console.error("[Azure] ", e);
return NextResponse.json(prettyObject(e));
return res.json(prettyObject(e));
}
}
export const GET = handle;
export const POST = handle;
export const runtime = "edge";
// export const runtime = "edge";
export const preferredRegion = [
"arn1",
"bom1",

View File

@@ -20,8 +20,8 @@ const serverConfig = getServerSideConfig();
export async function requestOpenai(
req: NextRequest,
cloneBody: any,
isAzure: boolean,
current_model: string,
// isAzure: boolean,
current_model?: string,
) {
const controller = new AbortController();

View File

@@ -3,15 +3,16 @@ import { getServerSideConfig } from "@/app/config/server";
import {
ModelProvider,
OpenaiPath,
AZURE_PATH,
AZURE_MODELS,
// AZURE_PATH,
// AZURE_MODELS,
} from "@/app/constant";
import { prettyObject } from "@/app/utils/format";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "../../auth";
import { requestLog, requestOpenai } from "../../common";
const ALLOWD_PATH = new Set(Object.values({ ...OpenaiPath, ...AZURE_PATH }));
// const ALLOWED_PATH = new Set(Object.values({ ...OpenaiPath, ...AZURE_PATH }));
const ALLOWD_PATH = new Set(Object.values(OpenaiPath));
function getModels(remoteModelRes: OpenAIListModelResponse) {
const config = getServerSideConfig();
@@ -75,9 +76,8 @@ async function handle(
await requestLog(req, jsonBody, subpath);
const isAzure = AZURE_MODELS.includes(jsonBody?.model as string);
// console.log("[Models]", jsonBody?.model);
const authResult = auth(req, ModelProvider.GPT, isAzure);
// const isAzure = AZURE_MODELS.includes(jsonBody?.model as string);
const authResult = auth(req, ModelProvider.GPT);
// if (authResult.error) {
// return NextResponse.json(authResult, {
// status: 401,
@@ -88,7 +88,7 @@ async function handle(
const response = await requestOpenai(
req,
cloneBody,
isAzure,
// isAzure,
jsonBody?.model ?? "",
);