From 5b0c8c303b7caa43bc9348bf1c027531c9e26dae Mon Sep 17 00:00:00 2001 From: kjartan0 <97447517+kjartan0@users.noreply.github.com> Date: Wed, 18 Nov 2026 09:13:19 +0100 Subject: [PATCH 1/4] =?UTF-8?q?feat(llm):=20=E6=94=AF=E6=8C=81=20OpenAI=20?= =?UTF-8?q?=E5=85=BC=E5=AE=B9=E5=90=8E=E7=AB=AF=E9=80=8F=E4=BC=A0=20chat?= =?UTF-8?q?=5Ftemplate=5Fkwargs=20(extra=5Fbody)=20(#3564)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Kjartan --- main/xiaozhi-server/config.yaml | 3 +++ main/xiaozhi-server/core/providers/llm/openai/openai.py | 8 ++++++++ 2 files changed, 11 insertions(+) diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index 11c87a5f6..f00c40098 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -626,6 +626,9 @@ LLM: max_tokens: 500 # 最大生成token数 top_p: 1 frequency_penalty: 0 # 频率惩罚 + # 可选: 透传给后端的 chat_template_kwargs(供 vLLM/SGLang 等 OpenAI 兼容后端控制 chat template, 例如思考开关) + # chat_template_kwargs: + # enable_thinking: true AliAppLLM: # 定义LLM API类型 type: AliBL diff --git a/main/xiaozhi-server/core/providers/llm/openai/openai.py b/main/xiaozhi-server/core/providers/llm/openai/openai.py index 0229b4314..54c80228d 100644 --- a/main/xiaozhi-server/core/providers/llm/openai/openai.py +++ b/main/xiaozhi-server/core/providers/llm/openai/openai.py @@ -31,6 +31,8 @@ class LLMProvider(LLMProviderBase): # 可选实例级思考强度: off/disabled/none/disable | low/medium/high | 空/其他=按域名默认 self.thinking_mode = str(config.get("thinking") or "").strip().lower() self.api_key = config.get("api_key") + # 可选: 透传给后端的 chat_template_kwargs(供 vLLM/SGLang 等 OpenAI 兼容后端使用) + self.chat_template_kwargs = config.get("chat_template_kwargs") or {} if "base_url" in config: self.base_url = config.get("base_url") else: @@ -140,6 +142,9 @@ class LLMProvider(LLMProviderBase): # 禁用思考模式 self._apply_thinking_disabled(request_params) + # 透传 chat_template_kwargs 到 extra_body(OpenAI 兼容后端) + if self.chat_template_kwargs: + request_params.setdefault("extra_body", {})["chat_template_kwargs"] = self.chat_template_kwargs responses = self.client.chat.completions.create(**request_params) @@ -186,6 +191,9 @@ class LLMProvider(LLMProviderBase): # 禁用思考模式 self._apply_thinking_disabled(request_params) + # 透传 chat_template_kwargs 到 extra_body(OpenAI 兼容后端) + if self.chat_template_kwargs: + request_params.setdefault("extra_body", {})["chat_template_kwargs"] = self.chat_template_kwargs stream = self.client.chat.completions.create(**request_params) From a1919f7db3d796bcc481ea2d144db6d3ba2d7b80 Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Wed, 18 Nov 2026 17:11:09 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E8=A1=A5=E5=85=85sql=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E5=AD=97=E6=AE=B5=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../resources/db/changelog/202611181622.sql | 6 +++++ .../db/changelog/db.changelog-master.yaml | 7 ++++++ main/xiaozhi-server/config.yaml | 3 ++- .../core/providers/llm/openai/openai.py | 22 ++++++++++++------- 4 files changed, 29 insertions(+), 9 deletions(-) create mode 100644 main/manager-api/src/main/resources/db/changelog/202611181622.sql diff --git a/main/manager-api/src/main/resources/db/changelog/202611181622.sql b/main/manager-api/src/main/resources/db/changelog/202611181622.sql new file mode 100644 index 000000000..5d58b0379 --- /dev/null +++ b/main/manager-api/src/main/resources/db/changelog/202611181622.sql @@ -0,0 +1,6 @@ +-- 为 LLM 的 OpenAI 接口配置表单新增 chat_template_kwargs 字段(json-textarea) +-- 用于向 vLLM/SGLang 等自托管 OpenAI 兼容后端透传 chat template 控制参数(如 enable_thinking) +-- 注意: DashScope 等云服务不识别此字段, 仅供自托管后端使用 +UPDATE `ai_model_provider` +SET `fields` = '[{"key":"base_url","label":"基础URL","type":"string"},{"key":"model_name","label":"模型名称","type":"string"},{"key":"api_key","label":"API密钥","type":"string"},{"key":"temperature","label":"温度","type":"number"},{"key":"max_tokens","label":"最大令牌数","type":"number"},{"key":"top_p","label":"top_p值","type":"number"},{"key":"top_k","label":"top_k值","type":"number"},{"key":"frequency_penalty","label":"频率惩罚","type":"number"},{"key":"chat_template_kwargs","label":"chat模板参数(JSON,仅自托管vLLM/SGLang等)","type":"json-textarea"}]' +WHERE `id` = 'SYSTEM_LLM_openai'; diff --git a/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml b/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml index 1bdd43de4..648932256 100644 --- a/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/main/manager-api/src/main/resources/db/changelog/db.changelog-master.yaml @@ -774,3 +774,10 @@ databaseChangeLog: - sqlFile: encoding: utf8 path: classpath:db/changelog/202611101200.sql + - changeSet: + id: 202611181622 + author: RanChen + changes: + - sqlFile: + encoding: utf8 + path: classpath:db/changelog/202611181622.sql diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index f00c40098..ad4d9ea3c 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -626,7 +626,8 @@ LLM: max_tokens: 500 # 最大生成token数 top_p: 1 frequency_penalty: 0 # 频率惩罚 - # 可选: 透传给后端的 chat_template_kwargs(供 vLLM/SGLang 等 OpenAI 兼容后端控制 chat template, 例如思考开关) + # 可选: chat_template_kwargs 仅适用于 vLLM/SGLang 等自托管 OpenAI 兼容后端(请求级控制 chat template, 例如思考开关) + # 注意: DashScope 等云服务不认识此字段, 配置后会被忽略; 云服务的思考开关由服务端按域名自动处理 # chat_template_kwargs: # enable_thinking: true AliAppLLM: diff --git a/main/xiaozhi-server/core/providers/llm/openai/openai.py b/main/xiaozhi-server/core/providers/llm/openai/openai.py index 54c80228d..ac6ffe2a1 100644 --- a/main/xiaozhi-server/core/providers/llm/openai/openai.py +++ b/main/xiaozhi-server/core/providers/llm/openai/openai.py @@ -31,8 +31,13 @@ class LLMProvider(LLMProviderBase): # 可选实例级思考强度: off/disabled/none/disable | low/medium/high | 空/其他=按域名默认 self.thinking_mode = str(config.get("thinking") or "").strip().lower() self.api_key = config.get("api_key") - # 可选: 透传给后端的 chat_template_kwargs(供 vLLM/SGLang 等 OpenAI 兼容后端使用) - self.chat_template_kwargs = config.get("chat_template_kwargs") or {} + # 可选: 透传给后端的 chat_template_kwargs(供 vLLM/SGLang 等自托管 OpenAI 兼容后端使用) + chat_template_kwargs = config.get("chat_template_kwargs") + if chat_template_kwargs and not isinstance(chat_template_kwargs, dict): + logger.bind(tag=TAG).warning( + f"chat_template_kwargs 配置格式错误, 应为键值对象, 已忽略: {chat_template_kwargs!r}") + chat_template_kwargs = None + self.chat_template_kwargs = chat_template_kwargs or {} if "base_url" in config: self.base_url = config.get("base_url") else: @@ -119,6 +124,11 @@ class LLMProvider(LLMProviderBase): logger.bind(tag=TAG).info(f"为域名 {domain} 禁用思考模式,参数: {params}") break + def _apply_chat_template_kwargs(self, request_params: dict): + """透传 chat_template_kwargs 到 extra_body(仅 vLLM/SGLang 等自托管 OpenAI 兼容后端识别, 云服务会忽略)""" + if self.chat_template_kwargs: + request_params.setdefault("extra_body", {})["chat_template_kwargs"] = self.chat_template_kwargs + def response(self, session_id, dialogue, **kwargs): dialogue = self.normalize_dialogue(dialogue) @@ -142,9 +152,7 @@ class LLMProvider(LLMProviderBase): # 禁用思考模式 self._apply_thinking_disabled(request_params) - # 透传 chat_template_kwargs 到 extra_body(OpenAI 兼容后端) - if self.chat_template_kwargs: - request_params.setdefault("extra_body", {})["chat_template_kwargs"] = self.chat_template_kwargs + self._apply_chat_template_kwargs(request_params) responses = self.client.chat.completions.create(**request_params) @@ -191,9 +199,7 @@ class LLMProvider(LLMProviderBase): # 禁用思考模式 self._apply_thinking_disabled(request_params) - # 透传 chat_template_kwargs 到 extra_body(OpenAI 兼容后端) - if self.chat_template_kwargs: - request_params.setdefault("extra_body", {})["chat_template_kwargs"] = self.chat_template_kwargs + self._apply_chat_template_kwargs(request_params) stream = self.client.chat.completions.create(**request_params) From ad0e51466682f58c2855cfe95b1c6dfa76f6c38b Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Thu, 19 Nov 2026 15:01:10 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E8=B0=83=E6=95=B4=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/db/changelog/202611181622.sql | 4 ++-- main/xiaozhi-server/config.yaml | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/main/manager-api/src/main/resources/db/changelog/202611181622.sql b/main/manager-api/src/main/resources/db/changelog/202611181622.sql index 5d58b0379..db74442d8 100644 --- a/main/manager-api/src/main/resources/db/changelog/202611181622.sql +++ b/main/manager-api/src/main/resources/db/changelog/202611181622.sql @@ -1,6 +1,6 @@ --- 为 LLM 的 OpenAI 接口配置表单新增 chat_template_kwargs 字段(json-textarea) +-- 为 LLM 的 OpenAI 接口配置表单新增 chat_template_kwargs 字段(dict, 前端映射为 JSON 文本框) -- 用于向 vLLM/SGLang 等自托管 OpenAI 兼容后端透传 chat template 控制参数(如 enable_thinking) -- 注意: DashScope 等云服务不识别此字段, 仅供自托管后端使用 UPDATE `ai_model_provider` -SET `fields` = '[{"key":"base_url","label":"基础URL","type":"string"},{"key":"model_name","label":"模型名称","type":"string"},{"key":"api_key","label":"API密钥","type":"string"},{"key":"temperature","label":"温度","type":"number"},{"key":"max_tokens","label":"最大令牌数","type":"number"},{"key":"top_p","label":"top_p值","type":"number"},{"key":"top_k","label":"top_k值","type":"number"},{"key":"frequency_penalty","label":"频率惩罚","type":"number"},{"key":"chat_template_kwargs","label":"chat模板参数(JSON,仅自托管vLLM/SGLang等)","type":"json-textarea"}]' +SET `fields` = '[{"key":"base_url","label":"基础URL","type":"string"},{"key":"model_name","label":"模型名称","type":"string"},{"key":"api_key","label":"API密钥","type":"string"},{"key":"temperature","label":"温度","type":"number"},{"key":"max_tokens","label":"最大令牌数","type":"number"},{"key":"top_p","label":"top_p值","type":"number"},{"key":"top_k","label":"top_k值","type":"number"},{"key":"frequency_penalty","label":"频率惩罚","type":"number"},{"key":"chat_template_kwargs","label":"chat模板参数(JSON,仅自托管vLLM/SGLang等)","type":"dict"}]' WHERE `id` = 'SYSTEM_LLM_openai'; diff --git a/main/xiaozhi-server/config.yaml b/main/xiaozhi-server/config.yaml index ad4d9ea3c..35aaa54af 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -626,8 +626,8 @@ LLM: max_tokens: 500 # 最大生成token数 top_p: 1 frequency_penalty: 0 # 频率惩罚 - # 可选: chat_template_kwargs 仅适用于 vLLM/SGLang 等自托管 OpenAI 兼容后端(请求级控制 chat template, 例如思考开关) - # 注意: DashScope 等云服务不认识此字段, 配置后会被忽略; 云服务的思考开关由服务端按域名自动处理 + # 可选: chat_template_kwargs 仅供 vLLM/SGLang 等自托管 OpenAI 兼容后端使用(请求级控制 chat template, 例如思考开关) + # 注意: DashScope 等云服务不支持此字段, 请勿在 AliLLM 等云服务配置段启用; 此处示例仅作参数格式参考 # chat_template_kwargs: # enable_thinking: true AliAppLLM: From a2f949c3474185be1c651d3de10fbb2d90b09feb Mon Sep 17 00:00:00 2001 From: Sakura-RanChen <1908198662@qq.com> Date: Thu, 19 Nov 2026 15:41:41 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=E5=90=8D=E8=BF=87=E9=95=BF=E6=98=BE=E7=A4=BA=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/resources/db/changelog/202611181622.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main/manager-api/src/main/resources/db/changelog/202611181622.sql b/main/manager-api/src/main/resources/db/changelog/202611181622.sql index db74442d8..ddf0c713d 100644 --- a/main/manager-api/src/main/resources/db/changelog/202611181622.sql +++ b/main/manager-api/src/main/resources/db/changelog/202611181622.sql @@ -2,5 +2,5 @@ -- 用于向 vLLM/SGLang 等自托管 OpenAI 兼容后端透传 chat template 控制参数(如 enable_thinking) -- 注意: DashScope 等云服务不识别此字段, 仅供自托管后端使用 UPDATE `ai_model_provider` -SET `fields` = '[{"key":"base_url","label":"基础URL","type":"string"},{"key":"model_name","label":"模型名称","type":"string"},{"key":"api_key","label":"API密钥","type":"string"},{"key":"temperature","label":"温度","type":"number"},{"key":"max_tokens","label":"最大令牌数","type":"number"},{"key":"top_p","label":"top_p值","type":"number"},{"key":"top_k","label":"top_k值","type":"number"},{"key":"frequency_penalty","label":"频率惩罚","type":"number"},{"key":"chat_template_kwargs","label":"chat模板参数(JSON,仅自托管vLLM/SGLang等)","type":"dict"}]' +SET `fields` = '[{"key":"base_url","label":"基础URL","type":"string"},{"key":"model_name","label":"模型名称","type":"string"},{"key":"api_key","label":"API密钥","type":"string"},{"key":"temperature","label":"温度","type":"number"},{"key":"max_tokens","label":"最大令牌数","type":"number"},{"key":"top_p","label":"top_p值","type":"number"},{"key":"top_k","label":"top_k值","type":"number"},{"key":"frequency_penalty","label":"频率惩罚","type":"number"},{"key":"chat_template_kwargs","label":"模板变量(JSON)","type":"dict"}]' WHERE `id` = 'SYSTEM_LLM_openai';