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

Add feature: Support for multiple models on Azure and Azure model mapper #3344

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,15 @@ Specify OpenAI organization ID.

### `AZURE_URL` (optional)

> Example: https://{azure-resource-url}/openai/deployments/{deploy-name}
> Example: https://{azure-resource-url}/openai/deployments/{deploy-name}
>
> Example: https://xxx.openai.azure.com/openai/deployments/{deploy-name}

Azure deploy url.

If `{deploy-name}` is using the template mode, then it will automatically replace the path based on the model selected by the client.
If your model name is different from the deployment name, then you need to set the `AZURE_OPENAI_MODEL_MAPPER` parameter.

### `AZURE_API_KEY` (optional)

Azure Api Key.
Expand Down Expand Up @@ -216,12 +221,22 @@ If you want to disable parse settings from url, set this to 1.
### `CUSTOM_MODELS` (optional)

> Default: Empty
>
> Example: `+llama,+claude-2,-gpt-3.5-turbo,gpt-4-1106-preview=gpt-4-turbo` means add `llama, claude-2` to model list, and remove `gpt-3.5-turbo` from list, and display `gpt-4-1106-preview` as `gpt-4-turbo`.
>
> Example: `-all,gpt-35-turbo,gpt-4,gpt-4-32k`. The meaning is to only display `gpt-3.5-turbo`, `gpt-4`, and `gpt-4-32k` in the model list.

To control custom models, use `+` to add a custom model, use `-` to hide a model, use `name=displayName` to customize model name, separated by comma.

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

### `AZURE_OPENAI_MODEL_MAPPER` (optional)
> Default: Empty
> Example: `gpt-3.5-turbo=gpt-35-turbo` means map `gpt-3.5-turbo` to `gpt-35-turbo`

If you are deploying ChatGPT using Azure OpenAI, it is recommended to set the `AZURE_OPENAI_MODEL_MAPPER`.
The session summarization feature relies on the `gpt-3.5-turbo` model, unless the name of your Azure deployment is the same as it.

## Requirements

NodeJS >= 18, Docker >= 20
Expand Down
9 changes: 8 additions & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,16 @@ Azure Api 版本,你可以在这里找到:[Azure 文档](https://learn.micro

> 示例:`+qwen-7b-chat,+glm-6b,-gpt-3.5-turbo,gpt-4-1106-preview=gpt-4-turbo` 表示增加 `qwen-7b-chat` 和 `glm-6b` 到模型列表,而从列表中删除 `gpt-3.5-turbo`,并将 `gpt-4-1106-preview` 模型名字展示为 `gpt-4-turbo`。
> 如果你想先禁用所有模型,再启用指定模型,可以使用 `-all,+gpt-3.5-turbo`,则表示仅启用 `gpt-3.5-turbo`

> Example: `-all,gpt-35-turbo,gpt-4,gpt-4-32k`. 意思是在model 列表中只显示gpt-35-turbo,gpt-4,gpt-4-32k
用来控制模型列表,使用 `+` 增加一个模型,使用 `-` 来隐藏一个模型,使用 `模型名=展示名` 来自定义模型的展示名,用英文逗号隔开。

### `AZURE_OPENAI_MODEL_MAPPER` (optional)
> Default: Empty
> Example: `gpt-3.5-turbo=gpt-35-turbo` 意思是 `gpt-3.5-turbo` 映射到 `gpt-35-turbo`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看起来似乎是一个容易在配置文件中实现的方案 @Dogtiti


如果你使用azure openai 来部署的chatgpt,建议设定AZURE_OPENAI_MODEL_MAPPER .
会话摘要功能依赖gpt-3.5-turbo 这个model,除非你的azure deployment的名字和它相同
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以直接使用CUSTOM_MODELS中配置gpt-4-1106-preview=gpt-4-turbo的方式,等于后面既作为展示名称,也作为deploy_name使用

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你好,这个最新 readme 文档有了吗?使用 azure 部署的遇到一些麻烦。


## 开发

点击下方按钮,开始二次开发:
Expand Down
18 changes: 16 additions & 2 deletions app/api/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ import { makeAzurePath } from "../azure";

const serverConfig = getServerSideConfig();

export function azureModelMaper(model: string) {
if (serverConfig.isAzure) {
return serverConfig.azureModelMaper[model] ?? model;
}
return model;
}

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

Expand All @@ -28,6 +35,13 @@ export async function requestOpenai(req: NextRequest) {
baseUrl = baseUrl.slice(0, -1);
}

let body = await req.json();
console.log("[model name]", body["model"]);
baseUrl = baseUrl.replace(
"{deploy-name}",
azureModelMaper(body["model"]) as string,
);

console.log("[Proxy] ", path);
console.log("[Base Url]", baseUrl);
// this fix [Org ID] undefined in server side if not using custom point
Expand Down Expand Up @@ -63,7 +77,7 @@ export async function requestOpenai(req: NextRequest) {
}),
},
method: req.method,
body: req.body,
body: JSON.stringify(body),
// to fix #2485: https://stackoverflow.com/questions/55920957/cloudflare-worker-typeerror-one-time-use-body
redirect: "manual",
// @ts-ignore
Expand All @@ -72,7 +86,7 @@ export async function requestOpenai(req: NextRequest) {
};

// #1815 try to refuse gpt4 request
if (serverConfig.customModels && req.body) {
if (serverConfig.customModels && body) {
try {
const modelTable = collectModelTable(
DEFAULT_MODELS,
Expand Down
11 changes: 11 additions & 0 deletions app/config/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ export const getServerSideConfig = () => {
`[Server Config] using ${randomIndex + 1} of ${apiKeys.length} api key`,
);

let AzureModelMaper: Record<string, string> = {};
(process.env.AZURE_OPENAI_MODEL_MAPPER || "")
.trim()
.split(",")
.map((v) => v.trim().split("="))
.forEach(([k, v]) => {
AzureModelMaper[k] = v;
});
console.log("[AZURE_OPENAI_MODEL_MAPPER]", AzureModelMaper);

return {
baseUrl: process.env.BASE_URL,
apiKey,
Expand All @@ -79,6 +89,7 @@ export const getServerSideConfig = () => {
azureUrl: process.env.AZURE_URL,
azureApiKey: process.env.AZURE_API_KEY,
azureApiVersion: process.env.AZURE_API_VERSION,
azureModelMaper: AzureModelMaper,

needCode: ACCESS_CODES.size > 0,
code: process.env.CODE,
Expand Down
2 changes: 2 additions & 0 deletions next.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const nextConfig = {
child_process: false,
};

// open source map
config.devtool = 'source-map';
return config;
},
output: mode,
Expand Down
Loading