fix: fix using different model

This commit is contained in:
Fred Liang
2023-12-25 05:12:21 +08:00
parent a9d7253143
commit 5c638251f8
3 changed files with 38 additions and 13 deletions

View File

@@ -1,7 +1,7 @@
import { NextRequest } from "next/server";
import { getServerSideConfig } from "../config/server";
import md5 from "spark-md5";
import { ACCESS_CODE_PREFIX } from "../constant";
import { ACCESS_CODE_PREFIX, ModelProvider } from "../constant";
function getIP(req: NextRequest) {
let ip = req.ip ?? req.headers.get("x-real-ip");
@@ -24,7 +24,7 @@ function parseApiKey(bearToken: string) {
};
}
export function auth(req: NextRequest) {
export function auth(req: NextRequest, modelProvider: ModelProvider) {
const authToken = req.headers.get("Authorization") ?? "";
// check if it is openai api key or user token
@@ -56,12 +56,19 @@ export function auth(req: NextRequest) {
// if user does not provide an api key, inject system api key
if (!apiKey) {
const serverConfig = getServerSideConfig();
const systemApiKey = serverConfig.isAzure
? serverConfig.azureApiKey
: serverConfig.isGoogle
? serverConfig.googleApiKey
: serverConfig.apiKey;
// const systemApiKey = serverConfig.isAzure
// ? serverConfig.azureApiKey
// : serverConfig.isGoogle
// ? serverConfig.googleApiKey
// : serverConfig.apiKey;
const systemApiKey =
modelProvider === ModelProvider.GeminiPro
? serverConfig.googleApiKey
: serverConfig.isAzure
? serverConfig.azureApiKey
: serverConfig.apiKey;
if (systemApiKey) {
console.log("[Auth] use system api key");
req.headers.set("Authorization", `Bearer ${systemApiKey}`);