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] =?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)