From d4b67f1783e00cebb0cd5bc5b9b9e4a84ff0f78b Mon Sep 17 00:00:00 2001 From: Linxiushen <1787979356@qq.com> Date: Sun, 20 Sep 2026 11:54:35 +0800 Subject: [PATCH] =?UTF-8?q?[fix]=20serve=5Fopenai=5Fapi=20=E9=9D=9E?= =?UTF-8?q?=E6=B5=81=E5=BC=8F=E7=AB=AF=E7=82=B9=E7=9A=84=20max=5Ftokens=20?= =?UTF-8?q?=E4=B8=8D=E7=94=9F=E6=95=88=EF=BC=8C=E4=B8=8E=E6=B5=81=E5=BC=8F?= =?UTF-8?q?=E7=AB=AF=E7=82=B9=E8=A1=8C=E4=B8=BA=E4=B8=8D=E4=B8=80=E8=87=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 非流式分支传的是 max_length,但 MiniMindForCausalLM.generate() 的签名里 没有这个形参(只有 max_new_tokens=8192),max_length 被 **kwargs 吞掉后 无人处理,于是 request.max_tokens 对生成长度零约束。 同一文件的流式分支(第117行)传的是 max_new_tokens=max_tokens,精确生效。 同一个请求参数在两条路径上行为完全不同。 判决性实验:同时传 max_length=15 与 max_new_tokens=20, prompt 长度 10 -> 实际输出总长 30(新增 20),证明 max_length 被无视。 改为与流式分支一致的 max_new_tokens=request.max_tokens(一行)。 --- scripts/serve_openai_api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/serve_openai_api.py b/scripts/serve_openai_api.py index adf2212..e9cf871 100644 --- a/scripts/serve_openai_api.py +++ b/scripts/serve_openai_api.py @@ -202,7 +202,7 @@ async def chat_completions(request: ChatRequest): with torch.no_grad(): generated_ids = model.generate( inputs["input_ids"], - max_length=inputs["input_ids"].shape[1] + request.max_tokens, + max_new_tokens=request.max_tokens, do_sample=True, attention_mask=inputs["attention_mask"], pad_token_id=tokenizer.pad_token_id,