milvus入库脚本.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. )
  39. print("create_hybrid_collection 执行成功!")
  40. print(f"返回的 vectorstore 类型: {type(vectorstore)}")
  41. # 等待索引创建完成
  42. import time
  43. time.sleep(5)
  44. print(f"\n测试 hybrid_search 方法...")
  45. param = {'collection_name': collection_name}
  46. # 测试加权搜索
  47. results = manager.hybrid_search(
  48. param=param,
  49. query_text="桥梁建设",
  50. top_k=2,
  51. ranker_type="weighted",
  52. dense_weight=0.7,
  53. sparse_weight=0.3
  54. )
  55. print(f"Hybrid search 执行成功,返回 {len(results)} 个结果")
  56. for i, result in enumerate(results):
  57. content = result.get('text_content', '')[:50]
  58. print(f" {i+1}. {content}...")
  59. # 清理测试集合
  60. print(f"\n清理测试集合...")
  61. try:
  62. from pymilvus import utility
  63. if utility.has_collection(collection_name):
  64. utility.drop_collection(collection_name)
  65. print(f"成功清理集合: {collection_name}")
  66. except Exception as e:
  67. print(f"清理集合失败: {e}")
  68. return True
  69. except Exception as e:
  70. print(f"测试失败: {e}")
  71. import traceback
  72. traceback.print_exc()
  73. return False
  74. if __name__ == "__main__":
  75. success = test_basic_functionality()
  76. print("\n" + "=" * 50)
  77. print(f"测试结果: {'成功' if success else '失败'}")
  78. if success:
  79. print("修复验证成功!")
  80. print("- text_to_vector 方法正常工作")
  81. print("- create_hybrid_collection 方法正常工作")
  82. print("- hybrid_search 方法正常工作")