clean_tool_research_data.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 清理之前导入的 tool_research 数据
  5. """
  6. import sys
  7. from pathlib import Path
  8. # 设置 Windows 控制台编码
  9. if sys.platform == 'win32':
  10. import codecs
  11. sys.stdout = codecs.getwriter('utf-8')(sys.stdout.buffer, 'strict')
  12. sys.stderr = codecs.getwriter('utf-8')(sys.stderr.buffer, 'strict')
  13. # 添加父目录到路径
  14. sys.path.insert(0, str(Path(__file__).parent.parent.parent))
  15. # 加载环境变量
  16. from dotenv import load_dotenv
  17. project_root = Path(__file__).parent.parent.parent
  18. env_path = project_root / '.env'
  19. load_dotenv(env_path)
  20. from knowhub.knowhub_db.pg_store import PostgreSQLStore
  21. from knowhub.knowhub_db.pg_requirement_store import PostgreSQLRequirementStore
  22. def clean_requirements(req_store: PostgreSQLRequirementStore):
  23. """清理需求表中的 tool_research 数据"""
  24. print("\n=== 清理需求表 ===")
  25. cursor = req_store._get_cursor()
  26. try:
  27. # 删除 midjourney 和 seedream 的需求
  28. cursor.execute("""
  29. DELETE FROM requirement_table
  30. WHERE id LIKE 'req_midjourney_%' OR id LIKE 'req_seedream_%'
  31. """)
  32. deleted_count = cursor.rowcount
  33. req_store.conn.commit()
  34. print(f"删除了 {deleted_count} 条需求记录")
  35. except Exception as e:
  36. print(f"清理需求失败: {e}")
  37. req_store.conn.rollback()
  38. finally:
  39. cursor.close()
  40. def clean_cases(knowledge_store: PostgreSQLStore):
  41. """清理知识表中的 case 数据"""
  42. print("\n=== 清理知识表中的 cases ===")
  43. cursor = knowledge_store._get_cursor()
  44. try:
  45. # 删除 tool_research_agent 创建的 case 类型知识
  46. cursor.execute("""
  47. DELETE FROM knowledge
  48. WHERE owner = 'tool_research_agent'
  49. AND 'case' = ANY(types)
  50. """)
  51. deleted_count = cursor.rowcount
  52. knowledge_store.conn.commit()
  53. print(f"删除了 {deleted_count} 条 case 知识记录")
  54. except Exception as e:
  55. print(f"清理 cases 失败: {e}")
  56. knowledge_store.conn.rollback()
  57. finally:
  58. cursor.close()
  59. def main():
  60. print("开始清理之前导入的 tool_research 数据...")
  61. knowledge_store = PostgreSQLStore()
  62. req_store = PostgreSQLRequirementStore()
  63. clean_requirements(req_store)
  64. clean_cases(knowledge_store)
  65. print("\n清理完成!")
  66. if __name__ == '__main__':
  67. main()