diff --git a/app/api/azure/[...path]/route.ts b/app/api/azure/[...path]/route.ts index 397c695ac..474ee761d 100644 --- a/app/api/azure/[...path]/route.ts +++ b/app/api/azure/[...path]/route.ts @@ -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)); } } diff --git a/app/api/common.ts b/app/api/common.ts index 44b5509cf..0dcdbed94 100644 --- a/app/api/common.ts +++ b/app/api/common.ts @@ -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 { diff --git a/app/api/openai/[...path]/route.ts b/app/api/openai/[...path]/route.ts index 5c7cb925d..fda863781 100644 --- a/app/api/openai/[...path]/route.ts +++ b/app/api/openai/[...path]/route.ts @@ -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(