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..ddf0c713d --- /dev/null +++ b/main/manager-api/src/main/resources/db/changelog/202611181622.sql @@ -0,0 +1,6 @@ +-- 为 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":"模板变量(JSON)","type":"dict"}]' +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 b61f90083..5139ffef5 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 @@ -788,3 +788,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 a20ddce79..97145af68 100644 --- a/main/xiaozhi-server/config.yaml +++ b/main/xiaozhi-server/config.yaml @@ -630,6 +630,10 @@ LLM: max_tokens: 500 # 最大生成token数 top_p: 1 frequency_penalty: 0 # 频率惩罚 + # 可选: chat_template_kwargs 仅供 vLLM/SGLang 等自托管 OpenAI 兼容后端使用(请求级控制 chat template, 例如思考开关) + # 注意: DashScope 等云服务不支持此字段, 请勿在 AliLLM 等云服务配置段启用; 此处示例仅作参数格式参考 + # 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..ac6ffe2a1 100644 --- a/main/xiaozhi-server/core/providers/llm/openai/openai.py +++ b/main/xiaozhi-server/core/providers/llm/openai/openai.py @@ -31,6 +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 兼容后端使用) + 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: @@ -117,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) @@ -140,6 +152,7 @@ class LLMProvider(LLMProviderBase): # 禁用思考模式 self._apply_thinking_disabled(request_params) + self._apply_chat_template_kwargs(request_params) responses = self.client.chat.completions.create(**request_params) @@ -186,6 +199,7 @@ class LLMProvider(LLMProviderBase): # 禁用思考模式 self._apply_thinking_disabled(request_params) + self._apply_chat_template_kwargs(request_params) stream = self.client.chat.completions.create(**request_params)