From ad5741879acb03600e4c159d7de415407e84026a Mon Sep 17 00:00:00 2001 From: LIlGG <1103069291@qq.com> Date: Mon, 29 Sep 2025 17:19:07 +0800 Subject: [PATCH] feat: Make MAX_TOKENS and MAX_RESPONSE_SEGMENTS configurable Added MAX_TOKENS and MAX_RESPONSE_SEGMENTS to .env.example and documentation. Updated constants to read from environment variables, allowing configuration of token and response segment limits. --- .env.example | 4 ++++ app/lib/.server/llm/constants.ts | 6 ++++-- app/routes/api.chat/chat.server.ts | 2 +- docs/content/configuration.md | 2 ++ 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/.env.example b/.env.example index 966983a..5772e56 100644 --- a/.env.example +++ b/.env.example @@ -10,6 +10,10 @@ OPERATING_ENV=production STORAGE_DIR=/public/uploads # Maximum upload size for attachments MAX_UPLOAD_SIZE_MB=5 +# The number of segments when reaching the maximum Tokens count +MAX_RESPONSE_SEGMENTS=5 +# Default token usage for a single chat +MAX_TOKENS=8000 # Example Context Values for qwen2.5-coder:32b # diff --git a/app/lib/.server/llm/constants.ts b/app/lib/.server/llm/constants.ts index 5ffcc33..12acb51 100644 --- a/app/lib/.server/llm/constants.ts +++ b/app/lib/.server/llm/constants.ts @@ -1,5 +1,7 @@ // see https://docs.anthropic.com/en/docs/about-claude/models -export const MAX_TOKENS = 8000; +export const MAX_TOKENS = process.env.MAX_TOKENS ? parseInt(process.env.MAX_TOKENS) : 8000; // limits the number of model responses that can be returned in a single request -export const MAX_RESPONSE_SEGMENTS = 3; +export const MAX_RESPONSE_SEGMENTS = process.env.MAX_RESPONSE_SEGMENTS + ? parseInt(process.env.MAX_RESPONSE_SEGMENTS) + : 5; diff --git a/app/routes/api.chat/chat.server.ts b/app/routes/api.chat/chat.server.ts index c2f1100..4894d49 100644 --- a/app/routes/api.chat/chat.server.ts +++ b/app/routes/api.chat/chat.server.ts @@ -331,7 +331,7 @@ export async function chatAction({ request, userId }: ChatActionArgs) { const continueMessage = async (text: string) => { logger.info( - `达到最大 token 限制 (${MAX_TOKENS}): 继续消息, 还可以响应 (${MAX_RESPONSE_SEGMENTS - streamSwitches} 个分段)`, + `达到最大 token 限制 (${DEFAULT_MODEL_DETAILS?.maxTokenAllowed || MAX_TOKENS}): 继续消息, 还可以响应 (${MAX_RESPONSE_SEGMENTS - streamSwitches} 个分段)`, ); messages.push({ id: generateId(), diff --git a/docs/content/configuration.md b/docs/content/configuration.md index beef18d..1d23f7f 100644 --- a/docs/content/configuration.md +++ b/docs/content/configuration.md @@ -24,6 +24,8 @@ UPage 使用环境变量进行配置。您可以通过以下方式设置环境 | `USAGE_LOG_FILE` | 是否开启文件日志 | `true` | 否 | | `MAX_UPLOAD_SIZE_MB` | 附件上传的最大大小 (MB) | `5` | 否 | | `STORAGE_DIR` | 资源文件存储位置 | `/app/storage` | 否 | +| `MAX_RESPONSE_SEGMENTS` | 最大响应分段数 | `5` | 否 | +| `MAX_TOKENS` | 最大 token 数 | `8000` | 否 | ## AI 提供商配置