From 795ba9028f51b966b364b7dbc43a9d0d544ac568 Mon Sep 17 00:00:00 2001 From: HuangHai <10402852@qq.com> Date: Mon, 24 Mar 2025 08:07:48 +0800 Subject: [PATCH] 'commit' --- AI/WxMini/Milvus/MulvusConfig.py | 12 ++ AI/WxMini/Milvus/T1.py | 148 ++++++++++++++++++ AI/WxMini/{ => Milvus}/TestMilvusNearWord.py | 0 .../__pycache__/MulvusConfig.cpython-310.pyc | Bin 0 -> 262 bytes AI/WxMini/TestMilvusBase.py | 61 -------- 5 files changed, 160 insertions(+), 61 deletions(-) create mode 100644 AI/WxMini/Milvus/MulvusConfig.py create mode 100644 AI/WxMini/Milvus/T1.py rename AI/WxMini/{ => Milvus}/TestMilvusNearWord.py (100%) create mode 100644 AI/WxMini/Milvus/__pycache__/MulvusConfig.cpython-310.pyc delete mode 100644 AI/WxMini/TestMilvusBase.py diff --git a/AI/WxMini/Milvus/MulvusConfig.py b/AI/WxMini/Milvus/MulvusConfig.py new file mode 100644 index 00000000..ef42f924 --- /dev/null +++ b/AI/WxMini/Milvus/MulvusConfig.py @@ -0,0 +1,12 @@ +MS_HOST = "10.10.14.101" +MS_PORT = "19530" +MS_COLLECTION_NAME = "ds_collection" +MS_MAX_CONNECTIONS = 5 +# "dimension": 128 +# "index_file_size": 1024 +# "metric_type": "L2" +# "nlist": 1024 +# "nprobe": 16 +# "topk": 10 +# "batch_size": 100 +# "index_type": "IVF_FLAT" diff --git a/AI/WxMini/Milvus/T1.py b/AI/WxMini/Milvus/T1.py new file mode 100644 index 00000000..e5160ceb --- /dev/null +++ b/AI/WxMini/Milvus/T1.py @@ -0,0 +1,148 @@ +# 安装 pymilvus 库(如果尚未安装) +# cd D:\anaconda3\envs\py310\Scripts +# pip install pymilvus DBUtils + +# 导入必要的模块 +from pymilvus import connections, FieldSchema, CollectionSchema, DataType, Collection, utility +from queue import Queue +import threading +import random +from MulvusConfig import * + +# 1. 手动实现 Milvus 连接池 +class MilvusConnectionPool: + def __init__(self, host, port, max_connections=5): + """ + 初始化 Milvus 连接池 + :param host: Milvus 主机地址 + :param port: Milvus 端口 + :param max_connections: 最大连接数 + """ + self.host = host + self.port = port + self.max_connections = max_connections + self._pool = Queue(max_connections) + self._lock = threading.Lock() + + # 初始化连接池 + for _ in range(max_connections): + self._pool.put(self._create_connection()) + + def _create_connection(self): + """ + 创建一个新的 Milvus 连接 + :return: Milvus 连接对象 + """ + return connections.connect(host=self.host, port=self.port, alias="default") + + def get_connection(self): + """ + 从连接池中获取一个连接 + :return: Milvus 连接对象 + """ + with self._lock: + if not self._pool.empty(): + return self._pool.get() + else: + raise Exception("连接池已满,无法获取连接") + + def release_connection(self, connection): + """ + 释放连接,将其放回连接池 + :param connection: Milvus 连接对象 + """ + with self._lock: + if self._pool.qsize() < self.max_connections: + self._pool.put(connection) + else: + connections.disconnect("default") + + def close(self): + """ + 关闭连接池,释放所有连接 + """ + with self._lock: + while not self._pool.empty(): + connection = self._pool.get() + connections.disconnect("default") + +# 2. 使用连接池管理 Milvus 连接 +# 创建 Milvus 连接池,指定主机地址和端口 +milvus_pool = MilvusConnectionPool(host=MS_HOST, port=MS_PORT, max_connections=MS_MAX_CONNECTIONS) + +# 从连接池中获取一个连接 +connection = milvus_pool.get_connection() + +# 3. 定义集合的字段和模式 +# 创建字段列表,定义集合的结构 +fields = [ + FieldSchema(name="id", dtype=DataType.INT64, is_primary=True, auto_id=True), # 主键字段,自动生成 ID + FieldSchema(name="embedding", dtype=DataType.FLOAT_VECTOR, dim=128) # 向量字段,维度为 128 +] +# 创建集合模式,包含字段列表和描述 +schema = CollectionSchema(fields, description="Simple demo collection") + +# 4. 创建集合 +# 定义集合名称 +collection_name = "demo_collection" +# 检查集合是否已存在,如果存在则删除 +if utility.has_collection(collection_name): + utility.drop_collection(collection_name) # 删除已存在的集合 +# 使用集合模式和名称创建新集合 +collection = Collection(name=collection_name, schema=schema) + +# 5. 插入数据 +# 生成随机向量数据,模拟插入的向量 +data = [ + [random.random() for _ in range(128)], # 第一个 128 维向量 + [random.random() for _ in range(128)], # 第二个 128 维向量 + [random.random() for _ in range(128)] # 第三个 128 维向量 +] +# 将数据包装为实体列表 +entities = [data] # 插入的数据 +# 将数据插入集合 +collection.insert(entities) + +# 6. 创建索引 +# 定义索引参数 +index_params = { + "index_type": "IVF_FLAT", # 使用 IVF_FLAT 索引类型 + "metric_type": "L2", # 使用 L2 距离度量方式 + "params": {"nlist": 128} # 设置 IVF_FLAT 的 nlist 参数 +} +# 在向量字段上创建索引 +collection.create_index("embedding", index_params) + +# 7. 加载集合到内存 +# 将集合加载到内存中,以便进行查询 +collection.load() + +# 8. 查询数据 +# 生成一个随机查询向量 +query_vector = [random.random() for _ in range(128)] # 查询向量 +# 定义查询参数 +search_params = { + "metric_type": "L2", # 使用 L2 距离度量方式 + "params": {"nprobe": 10} # 设置 IVF_FLAT 的 nprobe 参数 +} +# 执行查询 +results = collection.search( + data=[query_vector], # 查询向量 + anns_field="embedding", # 查询的向量字段 + param=search_params, # 查询参数 + limit=2 # 返回最相似的 2 个结果 +) + +# 9. 输出查询结果 +# 遍历查询结果,输出每个结果的 ID 和距离 +for hits in results: + for hit in hits: + print(f"ID: {hit.id}, Distance: {hit.distance}") + +# 10. 释放连接 +# 将连接放回连接池 +milvus_pool.release_connection(connection) + +# 11. 关闭连接池 +# 关闭 Milvus 连接池 +milvus_pool.close() \ No newline at end of file diff --git a/AI/WxMini/TestMilvusNearWord.py b/AI/WxMini/Milvus/TestMilvusNearWord.py similarity index 100% rename from AI/WxMini/TestMilvusNearWord.py rename to AI/WxMini/Milvus/TestMilvusNearWord.py diff --git a/AI/WxMini/Milvus/__pycache__/MulvusConfig.cpython-310.pyc b/AI/WxMini/Milvus/__pycache__/MulvusConfig.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..1badb2ae5e8649696b92190870f4f145bd344e25 GIT binary patch literal 262 zcmd1j<>g`kf>jG2qzf`KFgylvkO5GP!2yViMSw&KLli>_V-#ZwQxsDQa};w5OE7~b zYZZ^7fgT8%07=7JtcI4R#s*QmDaG;0`8heM$t9Wjc`sRk+WjB`e;*%L=MYbSzj!}KU)Nh8F<-|BptzqOL_ByULlGO$3^4J_*u^R)r8qpl zC_5%FGcVmIKQBGT(K9Bz!Z$N7GsZVFr>wL%#