惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cisco Talos Blog
Cisco Talos Blog
V
V2EX
C
Check Point Blog
GbyAI
GbyAI
D
Docker
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
B
Blog RSS Feed
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
T
Troy Hunt's Blog
博客园 - Franky
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
Microsoft Security Blog
Microsoft Security Blog
P
Privacy & Cybersecurity Law Blog
WordPress大学
WordPress大学
The Cloudflare Blog
S
SegmentFault 最新的问题
Latest news
Latest news
Microsoft Azure Blog
Microsoft Azure Blog
P
Proofpoint News Feed
I
InfoQ
博客园 - 【当耐特】
NISL@THU
NISL@THU
A
About on SuperTechFans
T
Tailwind CSS Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
The Hacker News
The Hacker News
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Scott Helme
Scott Helme
雷峰网
雷峰网
C
CXSECURITY Database RSS Feed - CXSecurity.com
Security Latest
Security Latest
V
Vulnerabilities – Threatpost
Security Archives - TechRepublic
Security Archives - TechRepublic
A
Arctic Wolf
Hacker News: Ask HN
Hacker News: Ask HN
N
News and Events Feed by Topic
IT之家
IT之家
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
aimingoo的专栏
aimingoo的专栏
T
Threat Research - Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
阮一峰的网络日志
阮一峰的网络日志
SecWiki News
SecWiki News
大猫的无限游戏
大猫的无限游戏
S
Security Affairs
The Register - Security
The Register - Security
www.infosecurity-magazine.com
www.infosecurity-magazine.com
L
LINUX DO - 热门话题
T
Tor Project blog

Eson Wong's Blog

我为什么开发 AI 英语听力生成器:Im-Listening Playwright E2E 测试框架入门教程 如何使用 LlamaIndex 构建一个 RAG 检索系统 批评 怎么在网页中实现自动调整视频的清晰度 什么是检索增强生成(Retrieval-Augmented Generation)? Phonics 自然拼读常见规则 - 辅音字母的发音规律 ESP8266EX 自动下载电路 浅聊 esp8266 墨水屏 Todo List 制作教程 2023 年我买过的最好用的商品 转变自我的密码:《Atomic Habits》读后感 Phonics 自然拼读常见规则 - 音节和元音字母的发音规律 怎么用 Github Actions 部署 Next.js 项目到服务器 使用 v4l 采集摄像头 怎么解决 Prettier 和 ESLint 在 VSCode 里的冲突? 《微习惯》 - 读书笔记 在 iPad 使用 VS Code 写代码 在 Macbook 上运行 ChatGLM-6B
Milvus 入门教程
本文作者: Eson Wong · 2024-10-10 · via Eson Wong's Blog

Milvus

Milvus 是一个开源的向量数据库,它是为了生成式 AI 的应用设计的。它支持大规模的向量检索。

Milvus 的发音是 /ˈmil vəs/。

本文将是 Milvus 的使用入门。

先准备好 Python 环境,然后使用 pip 安装 Milvus:

1
pip install -U pymilvus

使用 Milvus

创建一个本地的 Milvus 文件

1
2
3
from pymilvus import MilvusClient

client = MilvusClient("milvus_demo.db")

运行上面的代码,在当前目录下会生成一个名为 milvus_demo.db 的文件。

创建一个集合 Collection

我们通常把相关联的数据放在 Milvus 中的一个集合(Collection)中,我们可以把集合想象成一个传统数据库中的表。

下面的代码会创建一个集合,它存储的向量数据的的维度数是 768,这个维度数是接下来准备的向量数据的维度。

1
2
collection_name = "milvus_demo"
client.create_collection(collection_name, dimension=768)

为了避免重复创建 Collection,我们可以先检查 Collection 是否存在:

1
2
3
collection_name = "milvus_demo"
if not client.has_collection(collection_name):
client.create_collection(collection_name, dimension=768)

准备数据

pymilvus[model] python 库中提供了一些工具函数,可以帮助我们把文本数据转换成向量,来进行 Milvus 的学习。

下面会把hello world你好世界milvus is great这三个文本数据转换成向量数据:

1
2
3
4
5
6
7
8
from pymilvus import model

embedding_fn = model.DefaultEmbeddingFunction()
docs = ["hello world", "hi", "你好世界", "milvus is great"]

vectors = embedding_fn.encode_documents(docs)

print ("向量数据的维度:", len(vectors[0]))

我们会得到一个向量数据的列表,每个向量数据的维度是 768。

然后我们可以把向量数据和文本数据组合成由idvectortext组成的数据列表:

1
2
3
4
5
6
7
data = [
{"id": i, "vector": vectors[i], "text": docs[i]}
for i in range(len(vectors))
]

print("数据包含", len(data), "个条目,每个条目包含字段: ", data[0].keys())
print("vector 维度:", len(data[0]["vector"]))

插入数据

使用 insert(collection_name, data) 方法把列数据插入到 Milvus 的 milvus_demo 集合中:

1
2
response = client.insert(collection_name, data)
print(response)

此时我们插入了三条数据,每条数据包含了向量和原始文本。

检索数据

Retrieval Augmented Generation(RAG) 的应用场景中,通常要检索与输入文本最关度最高的文本数据。向量数据之间的关度可以通过计算向量之间的距离来衡量。

首先我们要把输入的数据转换成向量:

1
2
3

query = "hi"
query_vectors = embedding_fn.encode_queries([query])

然后我们可以使用 search(collection_name, data) 方法来检索与输入文本在向量空间中最相近的数据:

1
2
3
4
5
6
7
8

query_response = client.search(
collection_name,
data=query_vectors,
limit=2,
output_fields=["id", "text"],
)
print(query_response)

我们会得到一个列表,列表中的每个元素包含了检索到的数据的iddistanceentity字段,其中distance字段表示检索到的数据与输入数据的距离。

1
2
3
4
5
data: ["[
{'id': 1, 'distance': 1.0, 'entity': {'text': 'hi', 'id': 1}},
{'id': 0, 'distance': 0.5280660390853882, 'entity': {'text': 'hello world', 'id': 0}},
{'id': 2, 'distance': 0.44701865315437317, 'entity': {'text': '你好世界', 'id': 2}}
]"]

distance 越接近 1,表示两个向量距离越近。从上面的结果中,我们可以看到,查询结果中的 hi 的与查询的文本相同,所以 distance 值为 1 。与查询文本hi距离最近的文本依次是hihello world你好世界。这意味着它们之间的相关度排序也是这样的。

删除数据

delete() 方法可以删除集合中的数据:

通过 id 删除数据:

1
client.delete(collection_name, ids=[1])

通过条件删除数据:

1
client.delete(collection_name, filter="id == 1")

删除集合

drop_collection() 方法可以删除集合:

1
client.drop_collection(collection_name)