You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
56 lines
2.1 KiB
56 lines
2.1 KiB
from Milvus.Utils.MilvusCollectionManager import MilvusCollectionManager
|
|
from Milvus.Utils.MilvusConnectionPool import *
|
|
from Milvus.Config.MulvusConfig import *
|
|
|
|
# 1. 使用连接池管理 Milvus 连接
|
|
milvus_pool = MilvusConnectionPool(host=MS_HOST, port=MS_PORT, max_connections=MS_MAX_CONNECTIONS)
|
|
|
|
# 2. 从连接池中获取一个连接
|
|
connection = milvus_pool.get_connection()
|
|
|
|
# 3. 初始化集合管理器
|
|
collection_name = MS_COLLECTION_NAME
|
|
collection_manager = MilvusCollectionManager(collection_name)
|
|
|
|
# 4. 加载集合到内存
|
|
collection_manager.load_collection()
|
|
print(f"集合 '{collection_name}' 已加载到内存。")
|
|
|
|
# 5. 查询所有数据
|
|
try:
|
|
# 使用 Milvus 的 query 方法查询所有数据
|
|
results = collection_manager.collection.query(
|
|
expr="", # 空表达式表示查询所有数据
|
|
output_fields=["id", "person_id", "user_input", "model_response", "timestamp", "embedding"], # 指定返回的字段
|
|
limit=1000 # 设置最大返回记录数
|
|
)
|
|
print("查询结果:")
|
|
if results:
|
|
for result in results:
|
|
try:
|
|
# 获取字段值
|
|
person_id = result["person_id"]
|
|
user_input = result["user_input"]
|
|
model_response = result["model_response"]
|
|
timestamp = result["timestamp"]
|
|
embedding = result["embedding"]
|
|
# 打印结果
|
|
print(f"ID: {result['id']}")
|
|
print(f"会话 ID: {person_id}")
|
|
print(f"用户问题: {user_input}")
|
|
print(f"大模型回复: {model_response}")
|
|
print(f"时间: {timestamp}")
|
|
print(f"向量: {embedding[:5]}...") # 只打印前 5 维向量
|
|
print("-" * 40) # 分隔线
|
|
except Exception as e:
|
|
print(f"查询失败: {e}")
|
|
else:
|
|
print("未找到相关数据,请检查查询参数或数据。")
|
|
except Exception as e:
|
|
print(f"查询失败: {e}")
|
|
|
|
# 6. 释放连接
|
|
milvus_pool.release_connection(connection)
|
|
|
|
# 7. 关闭连接池
|
|
milvus_pool.close() |