新增字节跳动ds模型兼容,新增华为ds模型兼容

This commit is contained in:
wangjianhua
2025-02-28 13:54:14 +08:00
parent 2167076652
commit cf9d088789
30 changed files with 896 additions and 0 deletions

View File

@@ -15,6 +15,7 @@ import { handle as siliconflowHandler } from "../../siliconflow";
import { handle as xaiHandler } from "../../xai";
import { handle as chatglmHandler } from "../../glm";
import { handle as proxyHandler } from "../../proxy";
import { handle as huaweiHandler } from "../../huawei";
async function handle(
req: NextRequest,
@@ -52,6 +53,8 @@ async function handle(
return siliconflowHandler(req, { params });
case ApiPath.OpenAI:
return openaiHandler(req, { params });
case ApiPath.Huawei:
return huaweiHandler(req, { params });
default:
return proxyHandler(req, { params });
}

187
app/api/huawei.ts Normal file
View File

@@ -0,0 +1,187 @@
import { getServerSideConfig } from "@/app/config/server";
import {
HUAWEI_BASE_URL,
ApiPath,
ModelProvider,
Huawei,
} from "@/app/constant";
import { prettyObject } from "@/app/utils/format";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/app/api/auth";
const serverConfig = getServerSideConfig();
export async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
console.log("[Huawei Route] params ", params);
if (req.method === "OPTIONS") {
return NextResponse.json({ body: "OK" }, { status: 200 });
}
const authResult = auth(req, ModelProvider.Huawei);
if (authResult.error) {
return NextResponse.json(authResult, {
status: 401,
});
}
try {
const response = await request(req);
return response;
} catch (e) {
console.error("[Huawei] ", e);
return NextResponse.json(prettyObject(e));
}
}
async function request(req: NextRequest) {
const controller = new AbortController();
let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Huawei, "");
const bodyText = await req.text();
const body = JSON.parse(bodyText);
let modelName = body.model as string;
// 先用原始 modelName 获取 charUrl
let baseUrl: string;
let endpoint = "";
if (modelName === "DeepSeek-R1-671B-32K") {
endpoint = "952e4f88-ef93-4398-ae8d-af37f63f0d8e";
}
if (modelName === "DeepSeek-V3-671B-32K") {
endpoint = "fd53915b-8935-48fe-be70-449d76c0fc87";
}
if (modelName === "DeepSeek-R1-671B-8K") {
endpoint = "861b6827-e5ef-4fa6-90d2-5fd1b2975882";
}
if (modelName === "DeepSeek-V3-671B-8K") {
endpoint = "707c01c8-517c-46ca-827a-d0b21c71b074";
}
if (modelName === "DeepSeek-V3-671B-4K") {
endpoint = "f354eacc-a2c5-43b4-a785-e5aadca988b3";
}
if (modelName === "DeepSeek-R1-671B-4K") {
endpoint = "c3cfa9e2-40c9-485f-a747-caae405296ef";
}
let charUrl = HUAWEI_BASE_URL.concat("/")
.concat(endpoint)
.concat("/v1/chat/completions")
.replace(/(?<!:)\/+/g, "/"); // 只替换不在 :// 后面的多个斜杠
console.log(`current charUrl name:${charUrl}`);
baseUrl = charUrl;
// 处理请求体1. 移除 system role 消息 2. 修改 model 名称格式
const modifiedBody = {
messages: body.messages
.map((msg: any) => ({
role: msg.role,
content: msg.content,
}))
.filter((msg: any) => msg.role !== "system"),
model: modelName.replace(/^(DeepSeek-(?:R1|V3)).*$/, "$1"), // 只保留 DeepSeek-R1 或 DeepSeek-V3
stream: body.stream,
temperature: body.temperature,
presence_penalty: body.presence_penalty,
frequency_penalty: body.frequency_penalty,
top_p: body.top_p,
};
const modifiedBodyText = JSON.stringify(modifiedBody);
console.log("Modified request body:", modifiedBodyText);
// if(!baseUrl){
// baseUrl = HUAWEI_BASE_URL
// }
// baseUrl = Huawei.ChatPath(modelName) || serverConfig.huaweiUrl || HUAWEI_BASE_URL;
console.log(
`current model name:${modelName},current api path:${baseUrl}.........`,
);
if (!baseUrl.startsWith("http")) {
baseUrl = `https://${baseUrl}`;
}
if (baseUrl.endsWith("/")) {
baseUrl = baseUrl.slice(0, -1);
}
console.log("[Proxy] ", path);
console.log("[Base Url]", baseUrl);
const timeoutId = setTimeout(
() => {
controller.abort();
},
10 * 60 * 1000,
);
// 如果 baseUrl 来自 Huawei.ChatPath则不需要再拼接 path
let fetchUrl = baseUrl.includes(HUAWEI_BASE_URL)
? baseUrl
: `${baseUrl}${path}`;
const headers: Record<string, string> = {
"Content-Type": "application/json",
Authorization: req.headers.get("Authorization") ?? "",
"X-Forwarded-For": req.headers.get("X-Forwarded-For") ?? "",
"X-Real-IP": req.headers.get("X-Real-IP") ?? "",
"User-Agent": req.headers.get("User-Agent") ?? "",
};
console.debug(`headers.Authorization:${headers.Authorization}`);
console.debug(`serverConfig.huaweiApiKey:${serverConfig.huaweiApiKey}`);
// 如果没有 Authorization header使用系统配置的 API key
headers.Authorization = `Bearer ${serverConfig.huaweiApiKey}`;
// #1815 try to refuse some request to some models
// if (serverConfig.customModels) {
// try {
// const jsonBody = JSON.parse(bodyText); // 直接使用已解析的 body
//
// if (
// isModelNotavailableInServer(
// serverConfig.customModels,
// jsonBody?.model as string,
// ServiceProvider.Huawei as string,
// )
// ) {
// return NextResponse.json(
// {
// error: true,
// message: `you are not allowed to use ${jsonBody?.model} model`,
// },
// {
// status: 403,
// },
// );
// }
// } catch (e) {
// console.error(`[Huawei] filter`, e);
// }
// }
try {
const res = await fetch(fetchUrl, {
headers,
method: req.method,
body: modifiedBodyText,
redirect: "manual",
// @ts-ignore
duplex: "half",
signal: controller.signal,
});
const newHeaders = new Headers(res.headers);
newHeaders.delete("www-authenticate");
// to disable nginx buffering
newHeaders.set("X-Accel-Buffering", "no");
return new Response(res.body, {
status: res.status,
statusText: res.statusText,
headers: newHeaders,
});
} finally {
clearTimeout(timeoutId);
}
}
export { Huawei };