fix: 优化思考内容提取,支持多种思考标记格式

This commit is contained in:
2026-04-12 18:51:30 +08:00
parent 23935a1a28
commit 066d2fe44d

View File

@@ -128,6 +128,8 @@ class LLMService:
# 处理思考功能 # 处理思考功能
if enable_thinking and agent_config.get('enable_thinking', True): if enable_thinking and agent_config.get('enable_thinking', True):
thinking_prompt = agent_config.get('thinking_prompt') thinking_prompt = agent_config.get('thinking_prompt')
thinking_prefix = agent_config.get('thinking_prefix', '')
thinking_suffix = agent_config.get('thinking_suffix', '')
if supports_thinking and thinking_model: if supports_thinking and thinking_model:
# 使用专门的思考模型 # 使用专门的思考模型
@@ -139,22 +141,15 @@ class LLMService:
thinking_result = await self._call_api( thinking_result = await self._call_api(
api_base, api_key, thinking_model, thinking_messages, api_base, api_key, thinking_model, thinking_messages,
max_tokens=min(max_tokens, 1000), max_tokens=min(max_tokens, 1000),
temperature=0.3 # 思考时降低温度 temperature=0.3
) )
thinking_content = thinking_result thinking_content = thinking_result
except Exception as e: except Exception as e:
logger.warning(f"思考模型调用失败: {e}") logger.warning(f"思考模型调用失败: {e}")
elif supports_thinking:
# Provider支持思考但无单独模型尝试在回复中获取思考部分
pass # 在回复解析时处理
elif thinking_prompt: elif thinking_prompt:
# Provider不支持思考Agent配置了思考提示词 # Agent配置了思考提示词,添加到系统提示中
# 将思考提示词添加到系统提示 enhanced_system = f"{system_prompt}\n\n{thinking_prompt}"
enhanced_system = f"{system_prompt}\n\n在回答之前,请先思考问题。思考过程请用{agent_config.get('thinking_prefix', '')}{agent_config.get('thinking_suffix', '')}包裹,然后再给出正式回答。"
if thinking_prompt:
enhanced_system += f"\n思考指导:{thinking_prompt}"
final_messages[0] = {"role": "system", "content": enhanced_system} final_messages[0] = {"role": "system", "content": enhanced_system}
# 调用主模型 # 调用主模型
@@ -165,19 +160,37 @@ class LLMService:
temperature=temperature temperature=temperature
) )
# 尝试从回复中提取思考内容 # 尝试从回复中提取思考内容支持DeepSeek R1、GLM等模型的思考模式
if enable_thinking and not supports_thinking: if enable_thinking and agent_config.get('enable_thinking', True):
thinking_prefix = agent_config.get('thinking_prefix', '') thinking_prefix = agent_config.get('thinking_prefix', '')
thinking_suffix = agent_config.get('thinking_suffix', '') thinking_suffix = agent_config.get('thinking_suffix', '')
if thinking_prefix and thinking_suffix: # 如果没有配置前缀后缀,使用常见的思考标记
# 提取思考部分 if not thinking_prefix:
pattern = f"{re.escape(thinking_prefix)}(.*?)?{re.escape(thinking_suffix)}" # 尝试常见的思考标记
match = re.search(pattern, response, re.DOTALL) common_thinking_markers = [
if match: ('<think>', '</think>'),
thinking_content = match.group(1).strip() ('【思考】', '【回答】'),
# 移除思考部分,只保留回复 ('Thought:', 'Answer:'),
response = re.sub(pattern, '', response, flags=re.DOTALL).strip() ('思考:', '回答:'),
]
for prefix, suffix in common_thinking_markers:
if prefix in response and suffix in response:
thinking_prefix = prefix
thinking_suffix = suffix
break
# 提取思考部分
if thinking_prefix and thinking_suffix and thinking_prefix in response:
try:
start_idx = response.find(thinking_prefix)
end_idx = response.find(thinking_suffix, start_idx)
if end_idx > start_idx:
thinking_content = response[start_idx + len(thinking_prefix):end_idx].strip()
# 移除思考部分,只保留回复
response = response[end_idx + len(thinking_suffix):].strip()
except Exception as e:
logger.warning(f"提取思考内容失败: {e}")
return response, thinking_content return response, thinking_content