From db5312229678a9693a14a59f535e618fb80fe2da Mon Sep 17 00:00:00 2001 From: H0llyW00dzZ Date: Wed, 8 Nov 2023 15:30:57 +0700 Subject: [PATCH] docs: up to date (#64) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 安忆 --- app/api/common.ts | 58 +++++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 37 deletions(-) diff --git a/app/api/common.ts b/app/api/common.ts index fc877b02db2..0af7761d88c 100644 --- a/app/api/common.ts +++ b/app/api/common.ts @@ -1,36 +1,35 @@ import { NextRequest, NextResponse } from "next/server"; -import { getServerSideConfig } from "../config/server"; -import { DEFAULT_MODELS, OPENAI_BASE_URL } from "../constant"; -import { collectModelTable } from "../utils/model"; -import { makeAzurePath } from "../azure"; -const serverConfig = getServerSideConfig(); +export const OPENAI_URL = "api.openai.com"; +const DEFAULT_PROTOCOL = "https"; +const PROTOCOL = process.env.PROTOCOL || DEFAULT_PROTOCOL; +const BASE_URL = process.env.BASE_URL || OPENAI_URL; +const DISABLE_GPT4 = !!process.env.DISABLE_GPT4; export async function requestOpenai(req: NextRequest) { const controller = new AbortController(); - const authValue = req.headers.get("Authorization") ?? ""; - const authHeaderName = serverConfig.isAzure ? "api-key" : "Authorization"; - - let path = `${req.nextUrl.pathname}${req.nextUrl.search}`.replaceAll( + const openaiPath = `${req.nextUrl.pathname}${req.nextUrl.search}`.replaceAll( "/api/openai/", "", ); - let baseUrl = - serverConfig.azureUrl ?? serverConfig.baseUrl ?? OPENAI_BASE_URL; + let baseUrl = BASE_URL; if (!baseUrl.startsWith("http")) { - baseUrl = `https://${baseUrl}`; + baseUrl = `${PROTOCOL}://${baseUrl}`; } if (baseUrl.endsWith("/")) { baseUrl = baseUrl.slice(0, -1); } - console.log("[Proxy] ", path); + console.log("[Proxy] ", openaiPath); console.log("[Base Url]", baseUrl); - console.log("[Org ID]", serverConfig.openaiOrgId); + + if (process.env.OPENAI_ORG_ID) { + console.log("[Org ID]", process.env.OPENAI_ORG_ID); + } const timeoutId = setTimeout( () => { @@ -39,24 +38,14 @@ export async function requestOpenai(req: NextRequest) { 10 * 60 * 1000, ); - if (serverConfig.isAzure) { - if (!serverConfig.azureApiVersion) { - return NextResponse.json({ - error: true, - message: `missing AZURE_API_VERSION in server env vars`, - }); - } - path = makeAzurePath(path, serverConfig.azureApiVersion); - } - - const fetchUrl = `${baseUrl}/${path}`; + const fetchUrl = `${baseUrl}/${openaiPath}`; const fetchOptions: RequestInit = { headers: { "Content-Type": "application/json", "Cache-Control": "no-store", - [authHeaderName]: authValue, - ...(serverConfig.openaiOrgId && { - "OpenAI-Organization": serverConfig.openaiOrgId, + Authorization: authValue, + ...(process.env.OPENAI_ORG_ID && { + "OpenAI-Organization": process.env.OPENAI_ORG_ID, }), }, method: req.method, @@ -69,23 +58,18 @@ export async function requestOpenai(req: NextRequest) { }; // #1815 try to refuse gpt4 request - if (serverConfig.customModels && req.body) { + if (DISABLE_GPT4 && req.body) { try { - const modelTable = collectModelTable( - DEFAULT_MODELS, - serverConfig.customModels, - ); const clonedBody = await req.text(); fetchOptions.body = clonedBody; - const jsonBody = JSON.parse(clonedBody) as { model?: string }; + const jsonBody = JSON.parse(clonedBody); - // not undefined and is false - if (modelTable[jsonBody?.model ?? ""] === false) { + if ((jsonBody?.model ?? "").includes("gpt-4")) { return NextResponse.json( { error: true, - message: `you are not allowed to use ${jsonBody?.model} model`, + message: "you are not allowed to use gpt-4 model", }, { status: 403,