修复了思考过程未能妥当处理(至少在火山上是这样)的bug

This commit is contained in:
Charlie Wang
2025-03-21 16:17:48 +08:00
parent 571d15489d
commit dcf2b7c1ff

View File

@@ -274,6 +274,7 @@ class LLM_request:
raise RuntimeError(f"请求被拒绝: {error_code_mapping.get(response.status)}") raise RuntimeError(f"请求被拒绝: {error_code_mapping.get(response.status)}")
response.raise_for_status() response.raise_for_status()
reasoning_content = ""
# 将流式输出转化为非流式输出 # 将流式输出转化为非流式输出
if stream_mode: if stream_mode:
@@ -303,6 +304,8 @@ class LLM_request:
accumulated_content += delta_content accumulated_content += delta_content
# 检测流式输出文本是否结束 # 检测流式输出文本是否结束
finish_reason = chunk["choices"][0].get("finish_reason") finish_reason = chunk["choices"][0].get("finish_reason")
if delta.get("reasoning_content", None):
reasoning_content += delta["reasoning_content"]
if finish_reason == "stop": if finish_reason == "stop":
chunk_usage = chunk.get("usage", None) chunk_usage = chunk.get("usage", None)
if chunk_usage: if chunk_usage:
@@ -314,7 +317,6 @@ class LLM_request:
except Exception as e: except Exception as e:
logger.exception(f"解析流式输出错误: {str(e)}") logger.exception(f"解析流式输出错误: {str(e)}")
content = accumulated_content content = accumulated_content
reasoning_content = ""
think_match = re.search(r"<think>(.*?)</think>", content, re.DOTALL) think_match = re.search(r"<think>(.*?)</think>", content, re.DOTALL)
if think_match: if think_match:
reasoning_content = think_match.group(1).strip() reasoning_content = think_match.group(1).strip()