From fbe11486cb5eacbd6f4321b4d16bb8de57d75c8e Mon Sep 17 00:00:00 2001 From: ouyangyouzhang Date: Thu, 10 Jul 2025 18:32:34 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=BC=E5=AE=B9=E6=80=9D=E8=80=83=E6=A8=A1?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../intent_recognition/IntentRecognition.py | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/rag2_0/intent_recognition/IntentRecognition.py b/rag2_0/intent_recognition/IntentRecognition.py index 181912a..f809fc1 100755 --- a/rag2_0/intent_recognition/IntentRecognition.py +++ b/rag2_0/intent_recognition/IntentRecognition.py @@ -155,7 +155,9 @@ class AsyncIntentRecognizer: logging.info(f"异步意图分类耗时统计 - 总耗时: {classification_time:.2f}秒") # 尝试直接解析JSON响应 - parsed_output = classification_parser.parse(response.content.strip()) + response.content = response.content.strip() + clean_output = re.sub(r'.*?', '', response.content, flags=re.DOTALL) + parsed_output = classification_parser.parse(clean_output) return parsed_output except Exception as e: raise RuntimeError(f"解析分类结果时出错: {e}") from e @@ -216,7 +218,9 @@ class AsyncIntentRecognizer: response = await self._llm.invoke_async(formatted_prompt, False) # 尝试使用Pydantic解析器解析TermList - parsed_output = terms_list_parser.parse(response.content) + response.content = response.content.strip() + clean_output = re.sub(r'.*?', '', response.content, flags=re.DOTALL) + parsed_output = terms_list_parser.parse(clean_output) return parsed_output.terms @@ -344,9 +348,9 @@ class AsyncIntentRecognizer: try: # 异步调用LLM response = await self._llm.invoke_async(formatted_prompt, False) - - # 尝试直接解析JSON响应 - parsed_output = query_rewrite_parser.parse(response.content) + response.content = response.content.strip() + clean_output = re.sub(r'.*?', '', response.content, flags=re.DOTALL) + parsed_output = query_rewrite_parser.parse(clean_output) rewrite_end_time = time.time() rewrite_time = rewrite_end_time - rewrite_start_time logging.info(f"异步问题改写耗时统计 - 总耗时: {rewrite_time:.2f}秒") @@ -597,9 +601,10 @@ class AsyncIntentRecognizer: try: # 异步调用LLM response = await self._llm.invoke_async(formatted_prompt, False) - + response.content = response.content.strip() + clean_output = re.sub(r'.*?', '', response.content, flags=re.DOTALL) # 尝试解析LLM响应 - parsed_output = slot_parser.parse(response.content) + parsed_output = slot_parser.parse(clean_output) return parsed_output except Exception as e: # 如果解析失败,创建一个空的模型实例 @@ -633,7 +638,9 @@ class AsyncIntentRecognizer: 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'.*?', '', response.content, flags=re.DOTALL) + parsed_output = step_back_parser.parse(clean_output) step_back_end_time = time.time() step_back_time = step_back_end_time - step_back_start_time logging.debug(f"异步后退提示生成耗时统计 - 总耗时: {step_back_time:.2f}秒") @@ -670,7 +677,9 @@ class AsyncIntentRecognizer: 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'.*?', '', response.content, flags=re.DOTALL) + parsed_output = follow_up_parser.parse(clean_output) follow_up_end_time = time.time() follow_up_time = follow_up_end_time - follow_up_start_time logging.debug(f"异步后续问题生成耗时统计 - 总耗时: {follow_up_time:.2f}秒") @@ -707,7 +716,9 @@ class AsyncIntentRecognizer: 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'.*?', '', response.content, flags=re.DOTALL) + parsed_output = hyde_parser.parse(clean_output) hyde_end_time = time.time() hyde_time = hyde_end_time - hyde_start_time logging.debug(f"异步假设性文档生成耗时统计 - 总耗时: {hyde_time:.2f}秒") @@ -744,7 +755,9 @@ class AsyncIntentRecognizer: 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'.*?', '', response.content, flags=re.DOTALL) + parsed_output = multi_questions_parser.parse(clean_output) multi_questions_end_time = time.time() multi_questions_time = multi_questions_end_time - multi_questions_start_time logging.debug(f"异步多角度问题生成耗时统计 - 总耗时: {multi_questions_time:.2f}秒") @@ -799,7 +812,9 @@ class AsyncIntentRecognizer: try: # 解析LLM响应为JSON - result_json = parser.parse(response.content) + response.content = response.content.strip() + clean_output = re.sub(r'.*?', '', response.content, flags=re.DOTALL) + result_json = parser.parse(clean_output) classification = result_json.classification slot_filling = result_json.slots is_complete, missing_slots = slot_filling.check_required_slots()