mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
feat:默认禁用部分平台LLM思考模式
This commit is contained in:
@@ -4,10 +4,19 @@ from openai.types import CompletionUsage
|
||||
from config.logger import setup_logging
|
||||
from core.utils.util import check_model_key
|
||||
from core.providers.llm.base import LLMProviderBase
|
||||
from urllib.parse import urlparse
|
||||
|
||||
TAG = __name__
|
||||
logger = setup_logging()
|
||||
|
||||
# 需要禁用思考模式的平台域名及其对应参数(默认关闭思考模式)
|
||||
THINKING_DISABLED_DOMAINS = {
|
||||
"aliyuncs.com": {"enable_thinking": False},
|
||||
"bigmodel.cn": {"thinking": {"type": "disabled"}},
|
||||
"moonshot.cn": {"thinking": {"type": "disabled"}},
|
||||
"volces.com": {"thinking": {"type": "disabled"}},
|
||||
}
|
||||
|
||||
|
||||
class LLMProvider(LLMProviderBase):
|
||||
def __init__(self, config):
|
||||
@@ -69,6 +78,16 @@ class LLMProvider(LLMProviderBase):
|
||||
msg["content"] = ""
|
||||
return dialogue
|
||||
|
||||
def _apply_thinking_disabled(self, request_params: dict):
|
||||
"""根据域名自动禁用思考模式"""
|
||||
parsed_url = urlparse(self.base_url)
|
||||
domain = parsed_url.netloc
|
||||
for disabled_domain, params in THINKING_DISABLED_DOMAINS.items():
|
||||
if disabled_domain in domain:
|
||||
request_params.setdefault("extra_body", {}).update(params)
|
||||
logger.bind(tag=TAG).info(f"为域名 {domain} 禁用思考模式,参数: {params}")
|
||||
break
|
||||
|
||||
def response(self, session_id, dialogue, **kwargs):
|
||||
dialogue = self.normalize_dialogue(dialogue)
|
||||
|
||||
@@ -90,6 +109,9 @@ class LLMProvider(LLMProviderBase):
|
||||
if value is not None:
|
||||
request_params[key] = value
|
||||
|
||||
# 禁用思考模式
|
||||
self._apply_thinking_disabled(request_params)
|
||||
|
||||
responses = self.client.chat.completions.create(**request_params)
|
||||
|
||||
is_active = True
|
||||
@@ -130,6 +152,9 @@ class LLMProvider(LLMProviderBase):
|
||||
if value is not None:
|
||||
request_params[key] = value
|
||||
|
||||
# 禁用思考模式
|
||||
self._apply_thinking_disabled(request_params)
|
||||
|
||||
stream = self.client.chat.completions.create(**request_params)
|
||||
|
||||
for chunk in stream:
|
||||
|
||||
Reference in New Issue
Block a user