【自然语言处理与大模型】向量数据库:Milvus使用指南

Milvus 是一个开源的向量数据库,Milvus Lite 是 Milvus 向量数据库的轻量级版本,能为 AI 应用提供向量相似性搜索功能。它非常适合用于快速原型开发、资源有限的环境。

安装

bash 复制代码
# 安装 milvus 要求Python 3.8+
pip install "pymilvus[model]"

使用

python 复制代码
from pymilvus import MilvusClient
from pymilvus import model



# 创建一个客户端
client = MilvusClient("./milvus_demo.db")  # 指定一个存储所有数据的文件路径

# 加载本地的词嵌入向量模型
"""
如果你安装了,model依赖则默认值为all-MiniLM-L6-v2
"""
sentence_transformer_ef = model.dense.SentenceTransformerEmbeddingFunction(
    model_name='all-MiniLM-L6-v2', # 指定模型路径
    device='cpu' # 指定要使用的设备,例如"cpu"或"cuda:0"
)


# 创建一个集合
# 检查名为"demo_collection"的集合是否存在
if client.has_collection(collection_name="demo_collection"):
    # 如果存在则删除该集合
    client.drop_collection(collection_name="demo_collection")

client.create_collection(
    collection_name="demo_collection",
    dimension=768,  # 指定向量的维度
)

docs = [
    "Artificial intelligence was founded as an academic discipline in 1956.",
    "Alan Turing was the first person to conduct substantial research in AI.",
    "Born in Maida Vale, London, Turing was raised in southern England.",
]

# 将文档向量化
vectors = sentence_transformer_ef.encode_documents(docs)

# 打印embedding后的文档
print("Embeddings:", vectors)
print("Dim:", sentence_transformer_ef.dim, vectors[0].shape)

data = [
    {"id": i, "vector": vectors[i], "text": docs[i], "subject": "history"} 
    for i in range(len(docs))
]




# 插:将数据插入向量数据库
client.insert("demo_collection", data)



# 查:search相似度搜索 或 query关键字匹配
res = client.search(
    collection_name="demo_collection",
    data=[vectors[0]],
    filter="subject == 'history'",   # 过滤条件
    limit=2,  # 最相似的2条
    output_fields=["text", "subject"]
)
print(res)

res = client.query(
    collection_name="demo_collection",
    filter="subject == 'history'",     # 过滤条件
    output_fields=["text", "subject"]  # 只展示的字段
)
print(res)




# 改:修改其中id=1文档
update_docs = ["Artificial intelligence research began in mid-20th century"]
update_vectors = sentence_transformer_ef.encode_documents(update_docs)
update_data = [{
    "id": 1,  # 指定要更新的文档ID
    "text": "Artificial intelligence research began in mid-20th century",  # 新文本
    "vector": update_vectors[0],   # 新向量
    "subject": "computer_science"  # 新分类
}]
res = client.upsert(
    collection_name="demo_collection",
    data=[update_data]  # 注意数据需要是列表格式
)
print(res)




# 删:删除一个文档
res = client.delete(
    collection_name="demo_collection",
    filter="subject == 'history'"  # 过滤条件
)
print(res)

更多案例教学可以查看官方的教程:

Milvus官方文档https://milvus.io/docs/zh/quickstart.md

相关推荐
code_pgf7 小时前
OpenPI / π₀ 系列算法详解、创新点及 Jetson Orin NX 16GB 边缘端部署
人工智能·transformer·agi·palm
FreeBuf_7 小时前
智能攻防元年:渗透测试Agent迎来大考,AI如何从“能打”走向“可控”
人工智能
码农的日常搅屎棍7 小时前
segmentation-models-pytorch 极简实战:快速搭建与训练高精度语义分割模型
人工智能·pytorch·python
李景琰7 小时前
Spring AI + Milvus向量数据库:企业级RAG架构实战
人工智能·spring·milvus
aidesignplus7 小时前
扩散模型在自动驾驶路径规划中的技术演进与产业格局
人工智能·机器学习·自动驾驶
ai产品老杨7 小时前
深度解析:基于 Docker 与异构计算的工业级 AI 视频管理平台架构 —— 从 GB28181 接入到全平台源码交付
人工智能·docker·音视频
2301_776045237 小时前
什么叫流动性:数字货币与美股市场解读
人工智能·区块链
玛卡巴卡ldf7 小时前
【Springboot升级AI】(大模型部署)LangChain4j、会话记忆、隔离消失持久化问题、ollama、RAG知识库、Tools工具
java·开发语言·人工智能·spring boot·后端·springboot
电科一班林耿超7 小时前
机器学习大师课 第 4 课:分类问题入门 —— 逻辑回归(垃圾邮件分类实战)
人工智能·机器学习·分类·逻辑回归
俊哥V7 小时前
每日 AI 研究简报 · 2026-04-30
人工智能·ai