From 07c48ef7fd2094ef7392f1b14690f6936532edf0 Mon Sep 17 00:00:00 2001 From: sijinhui Date: Fri, 22 Dec 2023 21:20:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=A0=E5=BC=BA=E4=B8=8D=E5=8F=AF=E7=94=A8?= =?UTF-8?q?=E6=A8=A1=E5=9E=8B=E8=BF=87=E6=BB=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/common.ts | 17 ++++++----------- app/api/openai/[...path]/route.ts | 8 ++++++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/app/api/common.ts b/app/api/common.ts index 445c7af0c..a17af4127 100644 --- a/app/api/common.ts +++ b/app/api/common.ts @@ -10,6 +10,7 @@ export async function requestOpenai( req: NextRequest, cloneBody: any, isAzure: boolean, + current_model: string, ) { const controller = new AbortController(); @@ -65,26 +66,20 @@ export async function requestOpenai( signal: controller.signal, }; - // #1815 try to refuse gpt4 request - if (serverConfig.customModels && cloneBody) { + // #1815 try to refuse some model request + if (current_model) { try { const modelTable = collectModelTable( DEFAULT_MODELS, serverConfig.customModels, ); - // const clonedBody = await req.text(); - fetchOptions.body = cloneBody; - - const jsonBody = JSON.parse(cloneBody) as { - model?: string; - }; // not undefined and is false - if (!modelTable[jsonBody?.model ?? ""].available) { + if (!modelTable[current_model ?? ""].available) { return NextResponse.json( { error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, + message: `you are not allowed to use ${current_model} model`, }, { status: 403, @@ -92,7 +87,7 @@ export async function requestOpenai( ); } } catch (e) { - console.error("[OpenAI] gpt4 filter", e); + console.error("[OpenAI] gpt model filter", e); } } diff --git a/app/api/openai/[...path]/route.ts b/app/api/openai/[...path]/route.ts index 171d5364b..3807d3a4e 100644 --- a/app/api/openai/[...path]/route.ts +++ b/app/api/openai/[...path]/route.ts @@ -27,7 +27,6 @@ function getModels(remoteModelRes: OpenAIListModelResponse) { m.id === "gpt-3.5-turbo-16k" || m.id === "gpt-4-32k", ); - return remoteModelRes; } @@ -105,7 +104,12 @@ async function handle( // } try { - const response = await requestOpenai(req, cloneBody, isAzure); + const response = await requestOpenai( + req, + cloneBody, + isAzure, + jsonBody?.model ?? "", + ); // list models if (subpath === OpenaiPath.ListModelPath && response.status === 200) {