修复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( async function handle(
req: NextRequest, req: NextRequest,
res: NextApiResponse, // res: NextApiResponse,
{ params }: { params: { path: string[] } }, { params }: { params: { path: string[] } },
) { ) {
console.log("");
console.log("[Azure Route] params ", params); console.log("[Azure Route] params ", params);
if (req.method === "OPTIONS") { if (req.method === "OPTIONS") {
return res.status(200).json({ body: "OK" }); return NextResponse.json({ body: "OK" });
} }
const subpath = params.path.join("/"); const subpath = params.path.join("/");
const authResult = auth(req, ModelProvider.GPT); const authResult = auth(req, ModelProvider.GPT);
if (authResult.error) { if (authResult.error) {
return res.status(401).json(authResult); return NextResponse.json(authResult, { status: 401 });
} }
try { try {
return await requestOpenai(req); return await requestOpenai(req);
} catch (e) { } catch (e) {
console.error("[Azure] ", 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; baseUrl = baseUrl.split("/deployments").shift() as string;
path = `${req.nextUrl.pathname.replaceAll( path = `${req.nextUrl.pathname.replaceAll(
"/api/azure/", "/api/azure/",
"", "openai/",
)}?api-version=${azureApiVersion}`; )}?api-version=${azureApiVersion}`;
// Forward compatibility: // Forward compatibility:
@ -110,6 +110,7 @@ export async function requestOpenai(
} }
const fetchUrl = `${baseUrl}/${path}`; const fetchUrl = `${baseUrl}/${path}`;
const jsonBody = await req.json();
const fetchOptions: RequestInit = { const fetchOptions: RequestInit = {
headers: { headers: {
"Content-Type": "application/json", "Content-Type": "application/json",
@ -120,7 +121,7 @@ export async function requestOpenai(
}), }),
}, },
method: req.method, 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 // to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
redirect: "manual", redirect: "manual",
// @ts-ignore // @ts-ignore
@ -128,6 +129,8 @@ export async function requestOpenai(
signal: controller.signal, signal: controller.signal,
}; };
// console.log('4444444444444444', fetchUrl, req.body)
requestLog(req, jsonBody, path);
// #1815 try to refuse gpt4 request // #1815 try to refuse gpt4 request
if (serverConfig.customModels && req.body) { if (serverConfig.customModels && req.body) {
try { try {

View File

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