Skip to content

Commit b3af33a

Browse files
Bennuroot
authored andcommitted
Add a new script for milvus_tool
Signed-off-by: Bennu <[email protected]>
1 parent 04c2e17 commit b3af33a

File tree

2 files changed

+153
-14
lines changed

2 files changed

+153
-14
lines changed

recserving/milvus_tool/README.md

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
# README
22

3-
Milvus 是一款开源的特征向量相似度搜索引擎。本工具是基于 Milvus 实现的提供向量存储与召回的服务。你可以将本工具用在推荐系统中的召回这一过程。
3+
Milvus 是一款开源的特征向量相似度搜索引擎。本工具是基于 Milvus1.1.1 实现的提供向量存储与召回的服务。你可以将本工具用在推荐系统中的召回这一过程。
44

55
Milvus 教程请参考官网:https://milvus.io/cn/
66

77
Milvus 源码详情参考:https://github.com/milvus-io/milvus
88

9-
## 目录
9+
## 目录结构
1010

1111
以下是本工具的简要目录结构及说明:
1212

13-
```
13+
```text
1414
├── readme.md #介绍文档
1515
├── config.py #参数配置
16-
├── milvus_insert.py #向量插入脚本
17-
├── milvus_recall.py #向量召回脚本
16+
├── milvus_insert.py # 向量插入脚本
17+
├── milvus_recall.py # 向量召回脚本
18+
├── milvus_helper.py # Milvus 常用操作
1819
```
1920

2021
## 环境要求
@@ -42,51 +43,52 @@ Docker: 19.03 或以上
4243
Milvus 1.0.0
4344

4445

46+
4547
## 安装启动 Milvus
4648

47-
这里将安装 [Milvus1.0.0 的 CPU 版本](https://milvus.io/cn/docs/v1.0.0/milvus_docker-cpu.md),也可以选择安装 GPU 版本的 Milvus,安装方式请参考: [Milvus1.0.0 GPU 安装](https://milvus.io/cn/docs/v1.0.0/milvus_docker-gpu.md)
49+
这里将安装 [Milvus1.1.1 的 CPU 版本](https://milvus.io/cn/docs/v1.1.1/milvus_docker-cpu.md),也可以选择安装 GPU 版本的 Milvus,安装方式请参考: [Milvus1.1.1 GPU 安装](https://milvus.io/cn/docs/v1.1.1/milvus_docker-gpu.md)
4850

4951
**拉取 CPU 版本的 Milvus 镜像:**
5052

5153
```shell
52-
$ sudo docker pull milvusdb/milvus:1.0.0-cpu-d030521-1ea92e
54+
$ sudo docker pull milvusdb/milvus:1.1.1-cpu-d061621-330cc6
5355
```
5456

5557
**下载配置文件**
5658

5759
```shell
5860
$ mkdir -p /home/$USER/milvus/conf
5961
$ cd /home/$USER/milvus/conf
60-
$ wget https://raw.githubusercontent.com/milvus-io/milvus/v1.0.0/core/conf/demo/server_config.yaml
62+
$ wget https://raw.githubusercontent.com/milvus-io/milvus/v1.1.1/core/conf/demo/server_config.yaml
6163
```
6264

6365
> Milvus 相关的配置可以通过该配置文件指定。
6466
6567
**启动 Milvus Docker 容器**
6668

6769
```shell
68-
$ sudo docker run -d --name milvus_cpu_1.0.0 \
70+
$ sudo docker run -d --name milvus_cpu_1.1.1 \
6971
-p 19530:19530 \
7072
-p 19121:19121 \
7173
-v /home/$USER/milvus/db:/var/lib/milvus/db \
7274
-v /home/$USER/milvus/conf:/var/lib/milvus/conf \
7375
-v /home/$USER/milvus/logs:/var/lib/milvus/logs \
7476
-v /home/$USER/milvus/wal:/var/lib/milvus/wal \
75-
milvusdb/milvus:1.0.0-cpu-d030521-1ea92e
77+
milvusdb/milvus:1.1.1-cpu-d061621-330cc6
7678
```
7779

7880
**确认 Milvus 运行状态**
7981

8082
```shell
81-
$ sudo docker logs milvus_cpu_1.0.0
83+
$ sudo docker logs milvus_cpu_1.1.1
8284
```
8385

8486
> 用以上命令查看 Milvus 服务是否正常启动。
8587
8688
**安装 Milvus Python SDK**
8789

8890
```shell
89-
$ pip install pymilvus== 1.0.1
91+
$ pip install pymilvus==1.1.2
9092
```
9193

9294

@@ -107,7 +109,7 @@ $ pip install pymilvus== 1.0.1
107109

108110
### 向量导入
109111

110-
Milvus_insert.py 脚本提供向量导入功能,在使用该脚本前,需要在config.py 修改对应参数。调用方式如下:
112+
`milvus_insert.py` 脚本提供向量导入功能,在使用该脚本前,需要在config.py 修改对应参数。调用方式如下:
111113

112114
```python
113115
from milvus_tool.milvus_insert import VecToMilvus
@@ -125,14 +127,16 @@ status, ids = client.insert(collection_name=collection_name, vectors=embeddings,
125127
> **ids**: 和向量一一对应的 ID,这里要求的 ids 是一维列表的形式,示例:[1,2],这里表示上述两条向量对应的 ID 分别是 1 和 2. 这里的 ids 也可以为空,不传入参数,此时插入的向量将由 Milvus 自动分配 ID。
126128
>
127129
> **partition_tag**: 指定向量要插入的分区名称,Milvus 中可以通过标签将一集合分割为若干个分区 。该参数可以为空,为空时向量直接插入集合中。
130+
>
131+
> 在像 Milvus 指定的集合 collection 或者分区 partition 中插入参数时,如果 Milvus 中不存在该集合或者分区,该脚本会自动建立对应的集合或者分区。
128132
129133
**返回结果**:向量导入后将返回 `status``ids` 两个参数。status 返回的是插入的状态,插入成功或者失败。ids 返回的是插入向量对应的 ID,是一个一维列表。
130134

131135
具体使用可参考项目 movie_recommender/to_milvus.py
132136

133137
### 向量召回
134138

135-
milvus_recall.py 提供向量召回功能,在使用该脚本前,需要在config.py 修改对应参数,调用方式如下:
139+
`milvus_recall.py` 提供向量召回功能,在使用该脚本前,需要在config.py 修改对应参数,调用方式如下:
136140

137141
```python
138142
from milvus_tool.milvus_recall import RecallByMilvus
@@ -161,3 +165,52 @@ status, results = self.milvus_client.search(collection_name=collection_name, vec
161165
```
162166

163167
具体使用可参考项目 movie_recommender/recall.py
168+
169+
### Milvus 基本操作
170+
171+
`milvus_helper.py` 脚本中提供了以下几个 Milvus 常用操作:
172+
173+
- 在 Milvus 中建立 collection
174+
- 查看 Milvus 中是否存在指定 collection
175+
- 查看指定 collection 中导入的向量数
176+
- 查看 Milvus 中所有的 collection
177+
- 删除指定 collection
178+
179+
调用方式如下:
180+
181+
```python
182+
from milvus_tool.milvus_helper import MilvusHelper
183+
client = MilvusHelper()
184+
collection_name = 'test'
185+
```
186+
187+
- 查看 Milvus 中是否存在某 collection
188+
189+
```python
190+
print(client.has_collection(collection_name))
191+
```
192+
193+
- 在 Milvus 中建立 collection,建立 collection 的参数可修改 `config.py` 中的 `collection_param`
194+
195+
```python
196+
client.creat_collection(collection_name)
197+
```
198+
199+
- 查看指定 collection 中的向量数
200+
201+
```python
202+
print(client.count(collection_name))
203+
```
204+
205+
- 查看 Milvus 中所有的 collection
206+
207+
```python
208+
print(client.list_collection())
209+
```
210+
211+
- 删除 Milvus 中的指定 collection
212+
213+
```python
214+
client.delete_collection(collection_name)
215+
```
216+
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from milvus import Milvus, IndexType
2+
from config import MILVUS_HOST, MILVUS_PORT, collection_param
3+
import sys
4+
5+
6+
class MilvusHelper():
7+
def __init__(self):
8+
try:
9+
self.client = Milvus(host=MILVUS_HOST, port=MILVUS_PORT)
10+
print("Successfully connect to Milvus with IP:{} and PORT:{}".format(MILVUS_HOST, MILVUS_PORT))
11+
except Exception as e:
12+
print("Failed to connect Milvus: {}".format(e))
13+
sys.exit(1)
14+
15+
# Return if Milvus has the collection
16+
def has_collection(self, collection_name):
17+
try:
18+
status, ok = self.client.has_collection(collection_name)
19+
return ok
20+
except Exception as e:
21+
print("Failed to load data to Milvus: {}".format(e))
22+
sys.exit(1)
23+
24+
# Create a collection in Milvus
25+
def creat_collection(self, collection_name):
26+
try:
27+
collection_param['collection_name'] = collection_name
28+
status = self.client.create_collection(collection_param)
29+
print(status)
30+
except Exception as e:
31+
print("Milvus create collection error:", e)
32+
33+
# Delete Milvus collection
34+
def delete_collection(self, collection_name):
35+
try:
36+
if self.has_collection(collection_name):
37+
status = self.client.drop_collection(collection_name=collection_name)
38+
print(status)
39+
else:
40+
print("collection {} doesn't exist".format(collection_name))
41+
except Exception as e:
42+
print("Failed to drop collection: {}".format(e))
43+
44+
# Get the number of vectors in a collection
45+
def count(self, collection_name):
46+
try:
47+
if self.has_collection(collection_name):
48+
status, num = self.client.count_entities(collection_name=collection_name)
49+
return num
50+
else:
51+
print("collection {} doesn't exist".format(collection_name))
52+
return 0
53+
except Exception as e:
54+
print("Failed to count vectors in Milvus: {}".format(e))
55+
sys.exit(1)
56+
57+
# Show all collections in Milvus server
58+
def list_collection(self):
59+
try:
60+
status, collections = self.client.list_collections()
61+
return collections
62+
except Exception as e:
63+
print("Failed to list collections in Milvus: {}".format(e))
64+
sys.exit(1)
65+
66+
67+
if __name__ == '__main__':
68+
client = MilvusHelper()
69+
collection_name = 'test_helper'
70+
71+
# delete the collection 'test_helper' if it exists
72+
if client.has_collection(collection_name):
73+
client.delete_collection(collection_name)
74+
75+
# create a collection named 'test_helper'
76+
client.creat_collection(collection_name)
77+
78+
# get the number of vectors in collection 'test_helper'
79+
num = client.count(collection_name)
80+
print(num)
81+
82+
# Show all collections in Milvus server
83+
print(client.list_collection())
84+
85+
# delete the collection
86+
client.delete_collection(collection_name)

0 commit comments

Comments
 (0)