连通性测试.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python3
  2. """
  3. 简化调试 - 检查集合字段结构
  4. """
  5. import sys
  6. import os
  7. print("调试 LangChain Milvus 集合字段结构")
  8. print("=" * 50)
  9. def debug_collection():
  10. """调试集合结构"""
  11. try:
  12. from langchain_milvus import Milvus, BM25BuiltInFunction
  13. from langchain_core.documents import Document
  14. from pymilvus import Collection, utility
  15. from foundation.ai.models.model_handler import model_handler
  16. # 连接参数
  17. connection_args = {
  18. "uri": "http://192.168.92.61:19530",
  19. "user": None,
  20. "db_name": "lq_db"
  21. }
  22. collection_name = "debug_simple_fields"
  23. # 获取嵌入模型
  24. emdmodel = model_handler.get_embedding_model()
  25. # 创建测试文档
  26. test_docs = [
  27. Document(page_content="测试文档内容", metadata={"category": "test"})
  28. ]
  29. print("1. 创建 LangChain Milvus 混合搜索集合...")
  30. vectorstore = Milvus.from_documents(
  31. documents=test_docs,
  32. embedding=emdmodel,
  33. builtin_function=BM25BuiltInFunction(),
  34. vector_field=["dense", "sparse"],
  35. connection_args=connection_args,
  36. collection_name=collection_name,
  37. consistency_level="Strong",
  38. drop_old=True,
  39. )
  40. print("集合创建成功")
  41. # 等待索引创建
  42. import time
  43. time.sleep(3)
  44. print("\n2. 检查集合结构...")
  45. from pymilvus import connections
  46. connections.connect(uri="http://192.168.92.61:19530", db_name="lq_db")
  47. if utility.has_collection(collection_name):
  48. collection = Collection(collection_name)
  49. # 获取集合信息
  50. print(f"集合名称: {collection.name}")
  51. print(f"集合数量: {collection.num_entities}")
  52. # 获取字段信息
  53. schema = collection.schema
  54. print(f"\n字段结构:")
  55. for field in schema.fields:
  56. print(f" - 字段名: {field.name}")
  57. print(f" 类型: {field.dtype}")
  58. print(f" 是否主键: {field.is_primary}")
  59. if hasattr(field, 'dim'):
  60. print(f" 维度: {field.dim}")
  61. if hasattr(field, 'max_length'):
  62. print(f" 最大长度: {field.max_length}")
  63. print()
  64. # 获取索引信息
  65. print("索引信息:")
  66. try:
  67. indexes = collection.indexes
  68. for index in indexes:
  69. print(f" - 索引字段: {index.field_name}")
  70. print(f" 索引类型: {index.params.get('index_type', 'N/A')}")
  71. print(f" 索引参数: {index.params}")
  72. print()
  73. except Exception as e:
  74. print(f"获取索引失败: {e}")
  75. # 清理
  76. if utility.has_collection(collection_name):
  77. utility.drop_collection(collection_name)
  78. print(f"清理测试集合: {collection_name}")
  79. return True
  80. except Exception as e:
  81. print(f"调试失败: {e}")
  82. import traceback
  83. traceback.print_exc()
  84. return False
  85. if __name__ == "__main__":
  86. debug_collection()