auto_test.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import time
  2. from tqdm import tqdm
  3. from module.qa_chain import QAChain
  4. from utils.logger_config import setup_logger
  5. logger = setup_logger(__name__)
  6. # def auto_runtest(test_file, save_file):
  7. # custom_config = "config/qa_config.json"
  8. # custom_chain = QAChain(config_path=custom_config)
  9. # test_case = []
  10. # with open(test_file, 'r', encoding='utf-8') as file:
  11. # for line in file:
  12. # if "客户" in line:
  13. # test_case.append(line.strip())
  14. # with open(save_file, 'w', encoding='utf-8') as file:
  15. # for case in tqdm(test_case):
  16. # file.write(f"用户:{case}\n")
  17. # result = custom_chain.invoke({"question":case})
  18. # file.write(f"回答:{result['answer']}\n")
  19. # file.write("\n")
  20. def auto_runtest(test_file, save_file):
  21. try:
  22. custom_config = "config/qa_config.json"
  23. custom_chain = QAChain(config_path=custom_config)
  24. test_case = []
  25. with open(test_file, 'r', encoding='utf-8') as file:
  26. for line in file:
  27. if "客户" in line:
  28. test_case.append(line.strip())
  29. with open(save_file, 'w', encoding='utf-8') as file:
  30. for case in tqdm(test_case):
  31. try:
  32. file.write(f"{case}\n")
  33. result = custom_chain.invoke({"question": case})
  34. answer = result.get('answer', '系统无响应')
  35. file.write(f"回答:{answer}\n")
  36. except Exception as e:
  37. logger.error(f"处理案例失败: {case}, 错误: {str(e)}")
  38. file.write(f"回答:系统处理失败,请稍后重试\n")
  39. finally:
  40. file.write("\n")
  41. except Exception as e:
  42. logger.error(f"测试执行失败: {str(e)}")
  43. with open(save_file, 'w', encoding='utf-8') as file:
  44. file.write(f"测试执行错误: {str(e)}")
  45. if __name__ == "__main__":
  46. # 使用自定义配置文件
  47. label = input("请输入标签: ")
  48. test_file = f"./test_case/{label}_测试用例.txt"
  49. save_file = f"./test_result/{label}_deepseek0324.txt"
  50. auto_runtest(test_file, save_file)