兼容思考模型

This commit is contained in:
2025-07-10 18:32:34 +08:00
parent af03df99e3
commit fbe11486cb
+27 -12
View File
@@ -155,7 +155,9 @@ class AsyncIntentRecognizer:
logging.info(f"异步意图分类耗时统计 - 总耗时: {classification_time:.2f}") logging.info(f"异步意图分类耗时统计 - 总耗时: {classification_time:.2f}")
# 尝试直接解析JSON响应 # 尝试直接解析JSON响应
parsed_output = classification_parser.parse(response.content.strip()) response.content = response.content.strip()
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
parsed_output = classification_parser.parse(clean_output)
return parsed_output return parsed_output
except Exception as e: except Exception as e:
raise RuntimeError(f"解析分类结果时出错: {e}") from e raise RuntimeError(f"解析分类结果时出错: {e}") from e
@@ -216,7 +218,9 @@ class AsyncIntentRecognizer:
response = await self._llm.invoke_async(formatted_prompt, False) response = await self._llm.invoke_async(formatted_prompt, False)
# 尝试使用Pydantic解析器解析TermList # 尝试使用Pydantic解析器解析TermList
parsed_output = terms_list_parser.parse(response.content) response.content = response.content.strip()
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
parsed_output = terms_list_parser.parse(clean_output)
return parsed_output.terms return parsed_output.terms
@@ -344,9 +348,9 @@ class AsyncIntentRecognizer:
try: try:
# 异步调用LLM # 异步调用LLM
response = await self._llm.invoke_async(formatted_prompt, False) response = await self._llm.invoke_async(formatted_prompt, False)
response.content = response.content.strip()
# 尝试直接解析JSON响应 clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
parsed_output = query_rewrite_parser.parse(response.content) parsed_output = query_rewrite_parser.parse(clean_output)
rewrite_end_time = time.time() rewrite_end_time = time.time()
rewrite_time = rewrite_end_time - rewrite_start_time rewrite_time = rewrite_end_time - rewrite_start_time
logging.info(f"异步问题改写耗时统计 - 总耗时: {rewrite_time:.2f}") logging.info(f"异步问题改写耗时统计 - 总耗时: {rewrite_time:.2f}")
@@ -597,9 +601,10 @@ class AsyncIntentRecognizer:
try: try:
# 异步调用LLM # 异步调用LLM
response = await self._llm.invoke_async(formatted_prompt, False) response = await self._llm.invoke_async(formatted_prompt, False)
response.content = response.content.strip()
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
# 尝试解析LLM响应 # 尝试解析LLM响应
parsed_output = slot_parser.parse(response.content) parsed_output = slot_parser.parse(clean_output)
return parsed_output return parsed_output
except Exception as e: except Exception as e:
# 如果解析失败,创建一个空的模型实例 # 如果解析失败,创建一个空的模型实例
@@ -633,7 +638,9 @@ class AsyncIntentRecognizer:
response = await self._llm.invoke_async(formatted_prompt, False) response = await self._llm.invoke_async(formatted_prompt, False)
# 解析输出 # 解析输出
parsed_output = step_back_parser.parse(response.content) response.content = response.content.strip()
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
parsed_output = step_back_parser.parse(clean_output)
step_back_end_time = time.time() step_back_end_time = time.time()
step_back_time = step_back_end_time - step_back_start_time step_back_time = step_back_end_time - step_back_start_time
logging.debug(f"异步后退提示生成耗时统计 - 总耗时: {step_back_time:.2f}") logging.debug(f"异步后退提示生成耗时统计 - 总耗时: {step_back_time:.2f}")
@@ -670,7 +677,9 @@ class AsyncIntentRecognizer:
response = await self._llm.invoke_async(formatted_prompt, False) response = await self._llm.invoke_async(formatted_prompt, False)
# 解析输出 # 解析输出
parsed_output = follow_up_parser.parse(response.content) response.content = response.content.strip()
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
parsed_output = follow_up_parser.parse(clean_output)
follow_up_end_time = time.time() follow_up_end_time = time.time()
follow_up_time = follow_up_end_time - follow_up_start_time follow_up_time = follow_up_end_time - follow_up_start_time
logging.debug(f"异步后续问题生成耗时统计 - 总耗时: {follow_up_time:.2f}") logging.debug(f"异步后续问题生成耗时统计 - 总耗时: {follow_up_time:.2f}")
@@ -707,7 +716,9 @@ class AsyncIntentRecognizer:
response = await self._llm.invoke_async(formatted_prompt, False) response = await self._llm.invoke_async(formatted_prompt, False)
# 解析输出 # 解析输出
parsed_output = hyde_parser.parse(response.content) response.content = response.content.strip()
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
parsed_output = hyde_parser.parse(clean_output)
hyde_end_time = time.time() hyde_end_time = time.time()
hyde_time = hyde_end_time - hyde_start_time hyde_time = hyde_end_time - hyde_start_time
logging.debug(f"异步假设性文档生成耗时统计 - 总耗时: {hyde_time:.2f}") logging.debug(f"异步假设性文档生成耗时统计 - 总耗时: {hyde_time:.2f}")
@@ -744,7 +755,9 @@ class AsyncIntentRecognizer:
response = await self._llm.invoke_async(formatted_prompt, False) response = await self._llm.invoke_async(formatted_prompt, False)
# 解析输出 # 解析输出
parsed_output = multi_questions_parser.parse(response.content) response.content = response.content.strip()
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
parsed_output = multi_questions_parser.parse(clean_output)
multi_questions_end_time = time.time() multi_questions_end_time = time.time()
multi_questions_time = multi_questions_end_time - multi_questions_start_time multi_questions_time = multi_questions_end_time - multi_questions_start_time
logging.debug(f"异步多角度问题生成耗时统计 - 总耗时: {multi_questions_time:.2f}") logging.debug(f"异步多角度问题生成耗时统计 - 总耗时: {multi_questions_time:.2f}")
@@ -799,7 +812,9 @@ class AsyncIntentRecognizer:
try: try:
# 解析LLM响应为JSON # 解析LLM响应为JSON
result_json = parser.parse(response.content) response.content = response.content.strip()
clean_output = re.sub(r'<think>.*?</think>', '', response.content, flags=re.DOTALL)
result_json = parser.parse(clean_output)
classification = result_json.classification classification = result_json.classification
slot_filling = result_json.slots slot_filling = result_json.slots
is_complete, missing_slots = slot_filling.check_required_slots() is_complete, missing_slots = slot_filling.check_required_slots()