debug_simple.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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_lq_qwen3_8b_emd()
  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. if utility.has_collection(collection_name):
  46. collection = Collection(collection_name)
  47. # 获取集合信息
  48. print(f"集合名称: {collection.name}")
  49. print(f"集合数量: {collection.num_entities}")
  50. # 获取字段信息
  51. schema = collection.schema
  52. print(f"\n字段结构:")
  53. for field in schema.fields:
  54. print(f" - 字段名: {field.name}")
  55. print(f" 类型: {field.dtype}")
  56. print(f" 是否主键: {field.is_primary}")
  57. if hasattr(field, 'dim'):
  58. print(f" 维度: {field.dim}")
  59. if hasattr(field, 'max_length'):
  60. print(f" 最大长度: {field.max_length}")
  61. print()
  62. # 获取索引信息
  63. print("索引信息:")
  64. try:
  65. indexes = collection.indexes
  66. for index in indexes:
  67. print(f" - 索引字段: {index.field_name}")
  68. print(f" 索引类型: {index.index_type}")
  69. print(f" 索引参数: {index.params}")
  70. print()
  71. except Exception as e:
  72. print(f"获取索引失败: {e}")
  73. # 清理
  74. if utility.has_collection(collection_name):
  75. utility.drop_collection(collection_name)
  76. print(f"清理测试集合: {collection_name}")
  77. return True
  78. except Exception as e:
  79. print(f"调试失败: {e}")
  80. import traceback
  81. traceback.print_exc()
  82. return False
  83. if __name__ == "__main__":
  84. debug_collection()