移除DifyComparisonTester类中的析构函数,优化问题处理逻辑,增强错误处理和日志记录,调整返回结果结构以包含更多信息。

This commit is contained in:
2025-07-02 11:02:01 +08:00
parent f76f44640a
commit d811ae411f
+19 -7
View File
@@ -81,13 +81,6 @@ class DifyComparisonTester:
self.dify_tool = DifyTool()
def __del__(self):
"""
析构函数,在对象被销毁时自动关闭数据库连接。
确保在对象生命周期结束时释放数据库资源。
"""
self.dify_tool.close_connection()
def get_llm(self, **kwargs):
api_key = os.getenv("OPENAI_API_KEY")
base_url = os.getenv("OPENAI_API_BASE")
@@ -538,6 +531,7 @@ content: "{content}"
Returns:
dict: 包含问题、回答和评判结果的字典
"""
try:
# 获取基本的问题和回答
future_old, future_new = self.process_question(q)
if future_new is None:
@@ -568,14 +562,29 @@ content: "{content}"
if new_result is not None:
judge_result = "正确" if new_result else "错误"
# 判断检索词条是否正确
retrieve_right = answer_title in future_new["新检索词条"]
retrieve_right_str = ("正确" if retrieve_right else "错误") if answer_title else ""
# 判断槽点是否缺失
slot_info = future_new["槽点信息"]
slot_info_data=None
if isinstance(slot_info, str):
slot_info_data = json.loads(slot_info)
else:
slot_info_data = slot_info
slot_missing = slot_info_data.get("slot_missing", None)
slot_missing_str = "完整" if not slot_missing else "缺失"
# 返回结果
return {
"问题": query,
"问题改写": future_new["新问题改写"],
"问题分类": future_new["新问题分类"],
"槽点信息": future_new["槽点信息"],
"槽点是否缺失": slot_missing_str,
"新流程答案": new_answer,
"回答是否正确": judge_result,
"检索是否正确": retrieve_right_str,
"答案词条": answer_title if answer_title else "",
"检索词条": future_new["新检索词条"],
}
@@ -623,6 +632,9 @@ content: "{content}"
"新检索词条": future_new["新检索词条"],
"旧检索词条": future_old["旧检索词条"],
}
except Exception as e:
logging.error(f"处理问题 '{q}' 时发生错误: {str(e)}", exc_info=True)
return None
def run_comparison(self, with_judge=False):
"""