From 7584e3ade81512b4531416a2ef6312afe7b8aaf0 Mon Sep 17 00:00:00 2001 From: nanaya Date: Thu, 16 Nov 2023 15:01:16 +0800 Subject: [PATCH 1/2] feat: add env `SUMMARIZATION_MODEL` to specify summarization model on server side --- README.md | 6 ++++++ README_CN.md | 4 ++++ app/api/config/route.ts | 1 + app/config/server.ts | 21 ++++++++++++++++++++- app/store/access.ts | 1 + app/store/chat.ts | 6 +++--- 6 files changed, 35 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index d4e304f9d04..2efa88251dd 100644 --- a/README.md +++ b/README.md @@ -220,6 +220,12 @@ If you want to disable parse settings from url, set this to 1. 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. +### `SUMMARIZATION_MODEL` (optional) + +Specify the model for summarization. +If `CUSTOM_MODELS` is not set, by default `gpt-3.5-turbo` will be used for summarization. +If `CUSTOM_MODELS` is set, the model used for current session will be used. + ## Requirements NodeJS >= 18, Docker >= 20 diff --git a/README_CN.md b/README_CN.md index dde8c19b352..12ae1365a8d 100644 --- a/README_CN.md +++ b/README_CN.md @@ -126,6 +126,10 @@ Azure Api 版本,你可以在这里找到:[Azure 文档](https://learn.micro 用来控制模型列表,使用 `+` 增加一个模型,使用 `-` 来隐藏一个模型,使用 `模型名:展示名` 来自定义模型的展示名,用英文逗号隔开。 +### `SUMMARIZATION_MODEL` (可选) + +指定会话摘要的模型,例如`gpt-4`。如果`CUSTOM_MODELS`未设置,默认使用`gpt-3.5-turbo`。如果`CUSTOM_MODELS`设置了,默认使用当前会话模型。 + ## 开发 点击下方按钮,开始二次开发: diff --git a/app/api/config/route.ts b/app/api/config/route.ts index db84fba175a..54bf6866706 100644 --- a/app/api/config/route.ts +++ b/app/api/config/route.ts @@ -13,6 +13,7 @@ const DANGER_CONFIG = { hideBalanceQuery: serverConfig.hideBalanceQuery, disableFastLink: serverConfig.disableFastLink, customModels: serverConfig.customModels, + summarizationModel: serverConfig.summarizationModel, }; declare global { diff --git a/app/config/server.ts b/app/config/server.ts index 2f2e7d7fd8a..f10aa6b9dd1 100644 --- a/app/config/server.ts +++ b/app/config/server.ts @@ -1,5 +1,5 @@ import md5 from "spark-md5"; -import { DEFAULT_MODELS } from "../constant"; +import { DEFAULT_MODELS, SUMMARIZE_MODEL } from "../constant"; declare global { namespace NodeJS { @@ -21,6 +21,7 @@ declare global { ENABLE_BALANCE_QUERY?: string; // allow user to query balance or not DISABLE_FAST_LINK?: string; // disallow parse settings from url or not CUSTOM_MODELS?: string; // to control custom models + SUMMARIZATION_MODEL?: string; // allow user to use custom model for summarization // azure only AZURE_URL?: string; // https://{azure-url}/openai/deployments/{deploy-name} @@ -43,6 +44,23 @@ const ACCESS_CODES = (function getAccessCodes(): Set { } })(); +const SUMMARIZATION_MODEL = (function getSummarizeModel(): string { + // 1. return env{SUMMARIZATION_MODEL} if it is set + if (process.env.SUMMARIZATION_MODEL) { + return process.env.SUMMARIZATION_MODEL; + } + + // 2. return "gpt-3.5-turbo" + // if both env{SUMMARIZATION_MODEL} and env{CUSTOM_MODELS} are not set + if (!process.env.CUSTOM_MODELS) { + return SUMMARIZE_MODEL; + } + + // 3. return "" to let client use session model + // if env{SUMMARIZATION_MODEL} is not set but env{CUSTOM_MODELS} is set + return ""; +})(); + export const getServerSideConfig = () => { if (typeof process === "undefined") { throw Error( @@ -84,5 +102,6 @@ export const getServerSideConfig = () => { hideBalanceQuery: !process.env.ENABLE_BALANCE_QUERY, disableFastLink: !!process.env.DISABLE_FAST_LINK, customModels, + summarizationModel: SUMMARIZATION_MODEL, }; }; diff --git a/app/store/access.ts b/app/store/access.ts index 3b9008ba84b..0cbe3498b42 100644 --- a/app/store/access.ts +++ b/app/store/access.ts @@ -36,6 +36,7 @@ const DEFAULT_ACCESS_STATE = { disableGPT4: false, disableFastLink: false, customModels: "", + summarizationModel: "", }; export const useAccessStore = createPersistStore( diff --git a/app/store/chat.ts b/app/store/chat.ts index ff7eb51b5c1..d527b2d3cb6 100644 --- a/app/store/chat.ts +++ b/app/store/chat.ts @@ -9,7 +9,6 @@ import { DEFAULT_SYSTEM_TEMPLATE, KnowledgeCutOffDate, StoreKey, - SUMMARIZE_MODEL, } from "../constant"; import { api, RequestMessage } from "../client/api"; import { ChatControllerPool } from "../client/controller"; @@ -17,6 +16,7 @@ import { prettyObject } from "../utils/format"; import { estimateTokenLength } from "../utils/token"; import { nanoid } from "nanoid"; import { createPersistStore } from "../utils/store"; +import { useAccessStore } from "../store"; export type ChatMessage = RequestMessage & { date: string; @@ -81,8 +81,8 @@ function createEmptySession(): ChatSession { } function getSummarizeModel(currentModel: string) { - // if it is using gpt-* models, force to use 3.5 to summarize - return currentModel.startsWith("gpt") ? SUMMARIZE_MODEL : currentModel; + // use current model if summarizationModel is not set on the server side + return useAccessStore.getState().summarizationModel || currentModel; } function countMessages(msgs: ChatMessage[]) { From 4e3aac5a8bf41f228ed12f6ccc598380eada44af Mon Sep 17 00:00:00 2001 From: nanaya Date: Thu, 16 Nov 2023 15:47:13 +0800 Subject: [PATCH 2/2] doc: update readme --- README.md | 5 +++-- README_CN.md | 5 ++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2efa88251dd..d6c134f7ca5 100644 --- a/README.md +++ b/README.md @@ -223,8 +223,9 @@ To control custom models, use `+` to add a custom model, use `-` to hide a model ### `SUMMARIZATION_MODEL` (optional) Specify the model for summarization. -If `CUSTOM_MODELS` is not set, by default `gpt-3.5-turbo` will be used for summarization. -If `CUSTOM_MODELS` is set, the model used for current session will be used. +If it is not set, the summarization model will be decided on whether `CUSTOM_MODELS` is set or not +- If `CUSTOM_MODELS` is not set, `gpt-3.5-turbo` will be used for summarization. +- If `CUSTOM_MODELS` is set, the model used for current session will be used. ## Requirements diff --git a/README_CN.md b/README_CN.md index 12ae1365a8d..5a69a5d2f4d 100644 --- a/README_CN.md +++ b/README_CN.md @@ -128,7 +128,10 @@ Azure Api 版本,你可以在这里找到:[Azure 文档](https://learn.micro ### `SUMMARIZATION_MODEL` (可选) -指定会话摘要的模型,例如`gpt-4`。如果`CUSTOM_MODELS`未设置,默认使用`gpt-3.5-turbo`。如果`CUSTOM_MODELS`设置了,默认使用当前会话模型。 +指定会话摘要的模型,例如`gpt-4`。 +如果不设置,会话摘要模型将根据如下条件决定 +- 如果`CUSTOM_MODELS`未设置,默认使用`gpt-3.5-turbo`。 +- 如果`CUSTOM_MODELS`设置了,默认使用当前会话模型。 ## 开发