Skip to content
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

Auto get models #1

Closed
wants to merge 109 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
f3b972e
Fix web url
josephrocca May 27, 2024
1d8fd48
Add new Teracloud domain
junxian-li-hpc Jun 6, 2024
b6735bf
Merge pull request #4826 from junxian-li-hpc/fix-webdav
fred-bf Jun 7, 2024
163fc9e
fix someone forgot to update license year to 2024
ImBIOS Jun 14, 2024
9b0a705
Update LICENSE
ImBIOS Jun 14, 2024
6efe4fb
chore(app/layout.tsx): fix deprecated viewport nextjs 14
ImBIOS Jun 16, 2024
4640060
feat: support model: claude-3-5-sonnet-20240620
hengstchon Jun 21, 2024
78e7ea7
Merge pull request #4869 from hengstchon/feat/models
fred-bf Jun 24, 2024
9fb8fbc
fix: validate the url to avoid SSRF
fred-bf Jun 24, 2024
dad1221
Merge pull request from GHSA-gph5-rx77-3pjg
fred-bf Jun 24, 2024
2074935
Merge pull request #4841 from ImBIOS/patch-1
fred-bf Jun 24, 2024
b972a0d
feat: bump version
fred-bf Jun 24, 2024
95e3b15
Update Dockerfile
fred-bf Jun 26, 2024
c359b30
Merge pull request #4891 from ChatGPTNextWeb/fred-bf-patch-4
fred-bf Jun 26, 2024
d65ddea
fix: anthropic client using common getHeaders
lloydzhou Jul 1, 2024
37e2517
fix: 1. anthropic client using common getHeaders; 2. always using `Au…
lloydzhou Jul 1, 2024
ce3b6a0
Merge pull request #4782 from josephrocca/patch-1
lloydzhou Jul 1, 2024
69974d5
gemini using real sse format response #3677 #3688
lloydzhou Jul 1, 2024
c4ad66f
remove console.log
lloydzhou Jul 1, 2024
501f8b0
Merge pull request #4903 from ConnectAI-E/fix-claude-get-headers
lloydzhou Jul 3, 2024
78e2b41
Merge pull request #4906 from ConnectAI-E/feature-gemini-streaming
lloydzhou Jul 3, 2024
88c74ae
feat: using fetch to get buildin masks
lloydzhou Jul 3, 2024
1609abd
fix ts
lloydzhou Jul 3, 2024
cf2fce7
Merge pull request #4917 from ConnectAI-E/feature-make-mask-outsite
lloydzhou Jul 3, 2024
2803a91
readme 添加 DEFAULT_MODEL 参数 (#4915)
ccoc-cc Jul 3, 2024
e7b16bf
add function to check model is available
lloydzhou Jul 4, 2024
14f2a8f
merge model with modelName and providerName
lloydzhou Jul 4, 2024
b9ffd50
using <modelName>@<providerName> as fullName in modelTable
lloydzhou Jul 4, 2024
7a5596b
hotfix
lloydzhou Jul 4, 2024
aa08183
hotfix
lloydzhou Jul 4, 2024
a68341e
include providerId in fullName
lloydzhou Jul 4, 2024
97aa72e
hotfix ts
lloydzhou Jul 4, 2024
8cb204e
refactor: get language (#4922)
lloydzhou Jul 4, 2024
31d9444
hotfix
lloydzhou Jul 4, 2024
c4a6c93
Merge pull request #4923 from ConnectAI-E/refactor-model-table
Dogtiti Jul 4, 2024
1c20137
support azure deployment name
lloydzhou Jul 5, 2024
6b1b530
remove makeAzurePath
lloydzhou Jul 5, 2024
d9758be
fix ts
lloydzhou Jul 5, 2024
183ad2a
add missing file
lloydzhou Jul 5, 2024
14bc1b6
chore: optimize the code
Dogtiti Jul 5, 2024
6dc4844
using default azure api-version value
lloydzhou Jul 5, 2024
70907ea
Merge pull request #4850 from ImBIOS/patch-2
Dogtiti Jul 5, 2024
0b2863d
Improve tw Traditional Chinese locale
PeterDaveHello May 12, 2024
2d1f522
Merge pull request #4930 from ConnectAI-E/feature-azure
Dogtiti Jul 6, 2024
b58bbf8
feat: optimize getHeaders
Dogtiti Jul 6, 2024
700b06f
feat: optimize getHeaders
Dogtiti Jul 6, 2024
5e0657c
feat: add getClientApi method
Dogtiti Jul 6, 2024
785d374
feat: support baidu model
Dogtiti Jul 6, 2024
9b3b449
wip: doubao
Dogtiti Jul 6, 2024
fa31e78
Merge pull request #4935 from ConnectAI-E/feature/get-client-api
lloydzhou Jul 6, 2024
7218f13
Merge pull request #4934 from ConnectAI-E/feature/client-headers
lloydzhou Jul 6, 2024
9f7d137
Merge branch 'main' of https://github.com/ConnectAI-E/ChatGPT-Next-We…
Dogtiti Jul 6, 2024
2ec8b7a
Merge branch 'main' of https://github.com/ConnectAI-E/ChatGPT-Next-We…
Dogtiti Jul 6, 2024
f3e3f08
fix: apiClient
Dogtiti Jul 6, 2024
1caa61f
feat: swap name and displayName for bytedance in custom models
Dogtiti Jul 6, 2024
9bdd37b
feat: qwen
Dogtiti Jul 7, 2024
15f028a
update macOS icon to be consistent with design in public/macos.png
billxc Jul 8, 2024
71af262
hotfix: old AZURE_URL config error: "DeploymentNotFound". #4945 #4930
lloydzhou Jul 8, 2024
34ab37f
update CUSTOM_MODELS config for Azure mode.
lloydzhou Jul 8, 2024
6ac9789
hotfix
lloydzhou Jul 9, 2024
f68cd2c
review code
lloydzhou Jul 9, 2024
011b76e
review code
lloydzhou Jul 9, 2024
fadd7f6
try getAccessToken in app, fixbug to fetch in none stream mode
lloydzhou Jul 9, 2024
5295802
Merge pull request #4953 from ConnectAI-E/hotfix/azure-deployment-not…
lloydzhou Jul 9, 2024
b14a0f2
update locales
lloydzhou Jul 9, 2024
230e382
update readme
lloydzhou Jul 9, 2024
93a03f8
Merge remote-tracking branch 'origin/main' into feat-baidu
lloydzhou Jul 9, 2024
147fc9a
fix ts type error
lloydzhou Jul 9, 2024
f2a35f1
add missing file
lloydzhou Jul 9, 2024
47ea383
Merge pull request #4936 from ConnectAI-E/feat-baidu
lloydzhou Jul 9, 2024
c229d2c
merge main
lloydzhou Jul 9, 2024
b302354
update
lloydzhou Jul 9, 2024
9d7e19c
display doubao model name when select model
lloydzhou Jul 9, 2024
1149d45
remove check vision model
lloydzhou Jul 9, 2024
9d2a633
更新文档支持配置豆包的模型
lloydzhou Jul 9, 2024
82be426
fix eslint error
lloydzhou Jul 9, 2024
bb349a0
fix get headers for bytedance
lloydzhou Jul 9, 2024
2387208
merge code
lloydzhou Jul 9, 2024
3628d68
update
lloydzhou Jul 9, 2024
7573a19
add custom settings
lloydzhou Jul 9, 2024
3fcbb30
Merge branch 'feature/ByteDance' into feature/alibaba
lloydzhou Jul 9, 2024
e3b3a4f
add custom settings
lloydzhou Jul 9, 2024
814aaa4
update config for alibaba(qwen)
lloydzhou Jul 9, 2024
cd4784c
update
lloydzhou Jul 9, 2024
044c16d
update
lloydzhou Jul 9, 2024
94bc91c
Merge pull request #4939 from ConnectAI-E/feature/ByteDance
lloydzhou Jul 9, 2024
844025e
Merge pull request #4942 from ConnectAI-E/feature/alibaba
lloydzhou Jul 9, 2024
6885812
hotfix Gemini finish twice. #4955 #4966
lloydzhou Jul 10, 2024
ba6039f
Merge pull request #4968 from ConnectAI-E/hotfix/google
lloydzhou Jul 10, 2024
32b82b9
change build messages for qwen in client
lloydzhou Jul 10, 2024
2299a41
change build messages for qwen in client
lloydzhou Jul 10, 2024
fec36eb
hotfix
lloydzhou Jul 11, 2024
f8c2732
Merge pull request #4971 from ConnectAI-E/hotfix/alibaba
Dogtiti Jul 11, 2024
5e7254e
hotfix: doubao display name
lloydzhou Jul 11, 2024
89049e1
Merge pull request #4974 from ConnectAI-E/hotfix/bytedance
Dogtiti Jul 11, 2024
728c383
support cloudflare ai gateway
lloydzhou Jul 12, 2024
9d1b13b
Merge pull request #4933 from PeterDaveHello/locale-tw
LiuElric Jul 12, 2024
59d9bcd
Merge pull request #4946 from billxc/fix_mac_large_icon
LiuElric Jul 12, 2024
e8088d6
Merge pull request #4979 from ConnectAI-E/hotfix/cf-ai-gateway
Dogtiti Jul 12, 2024
9203870
hotfix: using custom models, create custom provider
lloydzhou Jul 12, 2024
4ea8c08
Merge pull request #4986 from ConnectAI-E/hotfix/cf-ai-gateway
LiuElric Jul 12, 2024
9b97dca
hotfix: custom comfig for Gemini api. #4944
lloydzhou Jul 12, 2024
8316272
update
lloydzhou Jul 12, 2024
476bdac
update
lloydzhou Jul 12, 2024
b4ae706
Merge pull request #4988 from ConnectAI-E/hotfix/cf-ai-gateway
lloydzhou Jul 12, 2024
46d3e78
hotfix: bytedance custom models
lloydzhou Jul 12, 2024
53c1176
update labels in setting page
lloydzhou Jul 12, 2024
8e4d261
Merge pull request #4989 from ConnectAI-E/hotfix/cf-ai-gateway
LiuElric Jul 12, 2024
b9d1dca
update version v2.13.0
lloydzhou Jul 13, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,4 @@ dev
.env

*.key
*.key.pub
*.key.pub
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ COPY --from=builder /app/.next/server ./.next/server
EXPOSE 3000

CMD if [ -n "$PROXY_URL" ]; then \
export HOSTNAME="127.0.0.1"; \
export HOSTNAME="0.0.0.0"; \
protocol=$(echo $PROXY_URL | cut -d: -f1); \
host=$(echo $PROXY_URL | cut -d/ -f3 | cut -d: -f1); \
port=$(echo $PROXY_URL | cut -d: -f3); \
Expand All @@ -58,7 +58,7 @@ CMD if [ -n "$PROXY_URL" ]; then \
echo "[ProxyList]" >> $conf; \
echo "$protocol $host $port" >> $conf; \
cat /etc/proxychains.conf; \
proxychains -f $conf node server.js --host 0.0.0.0; \
proxychains -f $conf node server.js; \
else \
node server.js; \
fi
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Zhang Yifei
Copyright (c) 2023-2024 Zhang Yifei

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
41 changes: 40 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ One-Click to get a well-designed cross-platform ChatGPT web UI, with GPT3, GPT4

[网页版](https://app.nextchat.dev/) / [客户端](https://github.com/Yidadaa/ChatGPT-Next-Web/releases) / [反馈](https://github.com/Yidadaa/ChatGPT-Next-Web/issues)

[web-url]: https://chatgpt.nextweb.fun
[web-url]: https://app.nextchat.dev/
[download-url]: https://github.com/Yidadaa/ChatGPT-Next-Web/releases
[Web-image]: https://img.shields.io/badge/Web-PWA-orange?logo=microsoftedge
[Windows-image]: https://img.shields.io/badge/-Windows-blue?logo=windows
Expand Down Expand Up @@ -181,6 +181,7 @@ Specify OpenAI organization ID.
### `AZURE_URL` (optional)

> Example: https://{azure-resource-url}/openai/deployments/{deploy-name}
> if you config deployment name in `CUSTOM_MODELS`, you can remove `{deploy-name}` in `AZURE_URL`

Azure deploy url.

Expand Down Expand Up @@ -212,6 +213,34 @@ anthropic claude Api version.

anthropic claude Api Url.

### `BAIDU_API_KEY` (optional)

Baidu Api Key.

### `BAIDU_SECRET_KEY` (optional)

Baidu Secret Key.

### `BAIDU_URL` (optional)

Baidu Api Url.

### `BYTEDANCE_API_KEY` (optional)

ByteDance Api Key.

### `BYTEDANCE_URL` (optional)

ByteDance Api Url.

### `ALIBABA_API_KEY` (optional)

Alibaba Cloud Api Key.

### `ALIBABA_URL` (optional)

Alibaba Cloud Api Url.

### `HIDE_USER_API_KEY` (optional)

> Default: Empty
Expand Down Expand Up @@ -245,6 +274,16 @@ To control custom models, use `+` to add a custom model, use `-` to hide a model

User `-all` to disable all default models, `+all` to enable all default models.

For Azure: use `modelName@azure=deploymentName` to customize model name and deployment name.
> Example: `+gpt-3.5-turbo@azure=gpt35` will show option `gpt35(Azure)` in model list.

For ByteDance: use `modelName@bytedance=deploymentName` to customize model name and deployment name.
> Example: `+Doubao-lite-4k@bytedance=ep-xxxxx-xxx` will show option `Doubao-lite-4k(ByteDance)` in model list.

### `DEFAULT_MODEL` (optional)

Change default model

### `WHITE_WEBDEV_ENDPOINTS` (optional)

You can use this option if you want to increase the number of webdav service addresses you are allowed to access, as required by the format:
Expand Down
51 changes: 46 additions & 5 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ OpenAI 接口代理 URL,如果你手动配置了 openai 接口代理,请填
### `AZURE_URL` (可选)

> 形如:https://{azure-resource-url}/openai/deployments/{deploy-name}
> 如果你已经在`CUSTOM_MODELS`中参考`displayName`的方式配置了{deploy-name},那么可以从`AZURE_URL`中移除`{deploy-name}`

Azure 部署地址。

Expand All @@ -106,26 +107,54 @@ Azure 密钥。

Azure Api 版本,你可以在这里找到:[Azure 文档](https://learn.microsoft.com/en-us/azure/ai-services/openai/reference#chat-completions)。

### `GOOGLE_API_KEY` (optional)
### `GOOGLE_API_KEY` (可选)

Google Gemini Pro 密钥.

### `GOOGLE_URL` (optional)
### `GOOGLE_URL` (可选)

Google Gemini Pro Api Url.

### `ANTHROPIC_API_KEY` (optional)
### `ANTHROPIC_API_KEY` (可选)

anthropic claude Api Key.

### `ANTHROPIC_API_VERSION` (optional)
### `ANTHROPIC_API_VERSION` (可选)

anthropic claude Api version.

### `ANTHROPIC_URL` (optional)
### `ANTHROPIC_URL` (可选)

anthropic claude Api Url.

### `BAIDU_API_KEY` (可选)

Baidu Api Key.

### `BAIDU_SECRET_KEY` (可选)

Baidu Secret Key.

### `BAIDU_URL` (可选)

Baidu Api Url.

### `BYTEDANCE_API_KEY` (可选)

ByteDance Api Key.

### `BYTEDANCE_URL` (可选)

ByteDance Api Url.

### `ALIBABA_API_KEY` (可选)

阿里云(千问)Api Key.

### `ALIBABA_URL` (可选)

阿里云(千问)Api Url.

### `HIDE_USER_API_KEY` (可选)

如果你不想让用户自行填入 API Key,将此环境变量设置为 1 即可。
Expand Down Expand Up @@ -156,7 +185,19 @@ anthropic claude Api Url.

用来控制模型列表,使用 `+` 增加一个模型,使用 `-` 来隐藏一个模型,使用 `模型名=展示名` 来自定义模型的展示名,用英文逗号隔开。

在Azure的模式下,支持使用`modelName@azure=deploymentName`的方式配置模型名称和部署名称(deploy-name)
> 示例:`+gpt-3.5-turbo@azure=gpt35`这个配置会在模型列表显示一个`gpt35(Azure)`的选项

在ByteDance的模式下,支持使用`modelName@bytedance=deploymentName`的方式配置模型名称和部署名称(deploy-name)
> 示例: `+Doubao-lite-4k@bytedance=ep-xxxxx-xxx`这个配置会在模型列表显示一个`Doubao-lite-4k(ByteDance)`的选项


### `DEFAULT_MODEL` (可选)

更改默认模型

### `DEFAULT_INPUT_TEMPLATE` (可选)

自定义默认的 template,用于初始化『设置』中的『用户输入预处理』配置项

## 开发
Expand Down
155 changes: 155 additions & 0 deletions app/api/alibaba/[...path]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { getServerSideConfig } from "@/app/config/server";
import {
Alibaba,
ALIBABA_BASE_URL,
ApiPath,
ModelProvider,
ServiceProvider,
} from "@/app/constant";
import { prettyObject } from "@/app/utils/format";
import { NextRequest, NextResponse } from "next/server";
import { auth } from "@/app/api/auth";
import { isModelAvailableInServer } from "@/app/utils/model";
import type { RequestPayload } from "@/app/client/platforms/openai";

const serverConfig = getServerSideConfig();

async function handle(
req: NextRequest,
{ params }: { params: { path: string[] } },
) {
console.log("[Alibaba Route] params ", params);

if (req.method === "OPTIONS") {
return NextResponse.json({ body: "OK" }, { status: 200 });
}

const authResult = auth(req, ModelProvider.Qwen);
if (authResult.error) {
return NextResponse.json(authResult, {
status: 401,
});
}

try {
const response = await request(req);
return response;
} catch (e) {
console.error("[Alibaba] ", e);
return NextResponse.json(prettyObject(e));
}
}

export const GET = handle;
export const POST = handle;

export const runtime = "edge";
export const preferredRegion = [
"arn1",
"bom1",
"cdg1",
"cle1",
"cpt1",
"dub1",
"fra1",
"gru1",
"hnd1",
"iad1",
"icn1",
"kix1",
"lhr1",
"pdx1",
"sfo1",
"sin1",
"syd1",
];

async function request(req: NextRequest) {
const controller = new AbortController();

// alibaba use base url or just remove the path
let path = `${req.nextUrl.pathname}`.replaceAll(ApiPath.Alibaba, "");

let baseUrl = serverConfig.alibabaUrl || ALIBABA_BASE_URL;

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,
);

const fetchUrl = `${baseUrl}${path}`;
const fetchOptions: RequestInit = {
headers: {
"Content-Type": "application/json",
Authorization: req.headers.get("Authorization") ?? "",
"X-DashScope-SSE": req.headers.get("X-DashScope-SSE") ?? "disable",
},
method: req.method,
body: req.body,
redirect: "manual",
// @ts-ignore
duplex: "half",
signal: controller.signal,
};

// #1815 try to refuse some request to some models
if (serverConfig.customModels && req.body) {
try {
const clonedBody = await req.text();
fetchOptions.body = clonedBody;

const jsonBody = JSON.parse(clonedBody) as { model?: string };

// not undefined and is false
if (
isModelAvailableInServer(
serverConfig.customModels,
jsonBody?.model as string,
ServiceProvider.Alibaba as string,
)
) {
return NextResponse.json(
{
error: true,
message: `you are not allowed to use ${jsonBody?.model} model`,
},
{
status: 403,
},
);
}
} catch (e) {
console.error(`[Alibaba] filter`, e);
}
}
try {
const res = await fetch(fetchUrl, fetchOptions);

// to prevent browser prompt for credentials
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);
}
}
Loading
Loading