debug_simple.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python3
  2. """
  3. 简化调试 - 检查集合字段结构
  4. """
  5. import sys
  6. import os
  7. # 添加项目根目录到路径
  8. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  9. print("调试 LangChain Milvus 集合字段结构")
  10. print("=" * 50)
  11. def debug_collection():
  12. """调试集合结构"""
  13. try:
  14. from langchain_milvus import Milvus, BM25BuiltInFunction
  15. from langchain_core.documents import Document
  16. from pymilvus import Collection, utility
  17. from foundation.ai.models.model_handler import model_handler
  18. # 连接参数
  19. connection_args = {
  20. "uri": "http://192.168.92.61:19530",
  21. "user": None,
  22. "db_name": "lq_db"
  23. }
  24. collection_name = "debug_simple_fields"
  25. # 获取嵌入模型
  26. emdmodel = model_handler._get_lq_qwen3_8b_emd()
  27. # 创建测试文档
  28. test_docs = [
  29. Document(page_content="测试文档内容", metadata={"category": "test"})
  30. ]
  31. print("1. 创建 LangChain Milvus 混合搜索集合...")
  32. vectorstore = Milvus.from_documents(
  33. documents=test_docs,
  34. embedding=emdmodel,
  35. builtin_function=BM25BuiltInFunction(),
  36. vector_field=["dense", "sparse"],
  37. connection_args=connection_args,
  38. collection_name=collection_name,
  39. consistency_level="Strong",
  40. drop_old=True,
  41. )
  42. print("集合创建成功")
  43. # 等待索引创建
  44. import time
  45. time.sleep(3)
  46. print("\n2. 检查集合结构...")
  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.index_type}")
  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()