milvus入库脚本.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python3
  2. """
  3. 测试修复后的 Milvus 向量实现
  4. """
  5. import sys
  6. import os
  7. print("测试修复后的 Milvus 向量实现")
  8. print("=" * 50)
  9. def test_basic_functionality():
  10. """测试基本功能"""
  11. try:
  12. # 导入并初始化 MilvusVectorManager
  13. from foundation.database.base.vector.milvus_vector import MilvusVectorManager
  14. print("成功导入 MilvusVectorManager")
  15. # 初始化管理器
  16. manager = MilvusVectorManager()
  17. print("MilvusVectorManager 初始化成功")
  18. # 测试 text_to_vector 方法
  19. test_text = "桥梁建设技术"
  20. vector = manager.text_to_vector(test_text)
  21. print(f"text_to_vector 测试成功,向量维度: {len(vector)}")
  22. # 简单测试文档
  23. test_documents = [
  24. {
  25. 'content': '四川路桥建设集团专注于桥梁和隧道工程建设',
  26. 'metadata': {'category': 'company', 'type': 'construction'}
  27. },
  28. {
  29. 'content': '高速公路桥梁建设技术包括预应力混凝土和钢结构',
  30. 'metadata': {'category': 'technology', 'type': 'highway'}
  31. }
  32. ]
  33. collection_name = "test_fix_validation"
  34. print(f"\n测试 create_hybrid_collection 方法...")
  35. vectorstore = manager.create_hybrid_collection(
  36. collection_name=collection_name,
  37. documents=test_documents,
  38. drop_old=True
  39. )
  40. print("create_hybrid_collection 执行成功!")
  41. print(f"返回的 vectorstore 类型: {type(vectorstore)}")
  42. # 等待索引创建完成
  43. import time
  44. time.sleep(5)
  45. print(f"\n测试 hybrid_search 方法...")
  46. param = {'collection_name': collection_name}
  47. # 测试加权搜索
  48. results = manager.hybrid_search(
  49. param=param,
  50. query_text="桥梁建设",
  51. top_k=2,
  52. ranker_type="weighted",
  53. dense_weight=0.7,
  54. sparse_weight=0.3
  55. )
  56. print(f"Hybrid search 执行成功,返回 {len(results)} 个结果")
  57. for i, result in enumerate(results):
  58. content = result.get('text_content', '')[:50]
  59. print(f" {i+1}. {content}...")
  60. # 清理测试集合
  61. print(f"\n清理测试集合...")
  62. try:
  63. from pymilvus import utility
  64. if utility.has_collection(collection_name):
  65. utility.drop_collection(collection_name)
  66. print(f"成功清理集合: {collection_name}")
  67. except Exception as e:
  68. print(f"清理集合失败: {e}")
  69. return True
  70. except Exception as e:
  71. print(f"测试失败: {e}")
  72. import traceback
  73. traceback.print_exc()
  74. return False
  75. if __name__ == "__main__":
  76. success = test_basic_functionality()
  77. print("\n" + "=" * 50)
  78. print(f"测试结果: {'成功' if success else '失败'}")
  79. if success:
  80. print("修复验证成功!")
  81. print("- text_to_vector 方法正常工作")
  82. print("- create_hybrid_collection 方法正常工作")
  83. print("- hybrid_search 方法正常工作")