-
Notifications
You must be signed in to change notification settings - Fork 61.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature dsmodels 20250228 #6317
Open
wangjianhuaa
wants to merge
10
commits into
ChatGPTNextWeb:main
Choose a base branch
from
wangjianhuaa:feature-dsmodels-20250228
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
cf9d088
新增字节跳动ds模型兼容,新增华为ds模型兼容
bd1a39b
国际化
b49910a
fix some bugs for suggestion
wangjianhuaa bc34792
fix some bugs for suggestion
wangjianhuaa 334fff6
Merge pull request #1 from wangjianhuaa/feature-dsmodels-20250228
wangjianhuaa 60fe83a
Merge branch 'ChatGPTNextWeb:main' into main
wangjianhuaa 18e2ab0
华为兼容
bb84a81
Merge branch 'ChatGPTNextWeb:main' into main
wangjianhuaa 62d50e9
Merge branch 'main' of https://github.com/wangjianhuaa/NextChat into …
c3648a3
字节v3新模型兼容
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -81,3 +81,7 @@ SILICONFLOW_API_KEY= | |
|
||
### siliconflow Api url (optional) | ||
SILICONFLOW_URL= | ||
|
||
HUAWEI_URL= | ||
|
||
HUAWEI_API_KEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
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-V3") { | ||
endpoint = "271c9332-4aa6-4ff5-95b3-0cf8bd94c394"; | ||
} | ||
if (modelName === "DeepSeek-R1") { | ||
endpoint = "8a062fd4-7367-4ab4-a936-5eeb8fb821c4"; | ||
} | ||
|
||
|
||
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: *****"); | ||
// 如果没有 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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix type annotation syntax
There's a syntax issue with the variable declaration. The "boolean" after the variable name needs to be corrected.
Apply this fix:
📝 Committable suggestion