修复merge bug

This commit is contained in:
sijinhui 2024-07-12 00:01:27 +08:00
parent cf8f587a56
commit 7c63912e85
3 changed files with 19 additions and 27 deletions

View File

@ -8,27 +8,27 @@ import { requestOpenai } from "../../common";
async function handle(
req: NextRequest,
res: NextApiResponse,
// res: NextApiResponse,
{ params }: { params: { path: string[] } },
) {
console.log("");
console.log("[Azure Route] params ", params);
if (req.method === "OPTIONS") {
return res.status(200).json({ body: "OK" });
return NextResponse.json({ body: "OK" });
}
const subpath = params.path.join("/");
const authResult = auth(req, ModelProvider.GPT);
if (authResult.error) {
return res.status(401).json(authResult);
return NextResponse.json(authResult, { status: 401 });
}
try {
return await requestOpenai(req);
} catch (e) {
console.error("[Azure] ", e);
return res.json(prettyObject(e));
return NextResponse.json(prettyObject(e));
}
}

View File

@ -78,7 +78,7 @@ export async function requestOpenai(
baseUrl = baseUrl.split("/deployments").shift() as string;
path = `${req.nextUrl.pathname.replaceAll(
"/api/azure/",
"",
"openai/",
)}?api-version=${azureApiVersion}`;
// Forward compatibility:
@ -110,6 +110,7 @@ export async function requestOpenai(
}
const fetchUrl = `${baseUrl}/${path}`;
const jsonBody = await req.json();
const fetchOptions: RequestInit = {
headers: {
"Content-Type": "application/json",
@ -120,7 +121,7 @@ export async function requestOpenai(
}),
},
method: req.method,
body: req.body,
body: JSON.stringify(jsonBody),
// to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
redirect: "manual",
// @ts-ignore
@ -128,6 +129,8 @@ export async function requestOpenai(
signal: controller.signal,
};
// console.log('4444444444444444', fetchUrl, req.body)
requestLog(req, jsonBody, path);
// #1815 try to refuse gpt4 request
if (serverConfig.customModels && req.body) {
try {

View File

@ -59,30 +59,19 @@ async function handle(
);
}
// const authResult = auth(req, ModelProvider.GPT);
// if (authResult.error) {
// return NextResponse.json(authResult, {
// status: 401,
// });
//
// let cloneBody, jsonBody;
//
// try {
// cloneBody = (await req.text()) as any;
// jsonBody = JSON.parse(cloneBody) as { model?: string };
// } catch (e) {
// jsonBody = {};
// }
let cloneBody, jsonBody;
try {
cloneBody = (await req.text()) as any;
jsonBody = JSON.parse(cloneBody) as { model?: string };
} catch (e) {
jsonBody = {};
}
// await requestLog(req, jsonBody, subpath);
await requestLog(req, jsonBody, subpath);
// const isAzure = AZURE_MODELS.includes(jsonBody?.model as string);
const authResult = auth(req, ModelProvider.GPT);
// if (authResult.error) {
// return NextResponse.json(authResult, {
// status: 401,
// });
// }
try {
const response = await requestOpenai(