【自然语言处理与大模型】向量数据库: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

相关推荐
巴里巴气10 分钟前
安装GPU版本的Pytorch
人工智能·pytorch·python
「、皓子~19 分钟前
后台管理系统的诞生 - 利用AI 1天完成整个后台管理系统的微服务后端+前端
前端·人工智能·微服务·小程序·go·ai编程·ai写作
笑衬人心。1 小时前
初学Spring AI 笔记
人工智能·笔记·spring
luofeiju1 小时前
RGB下的色彩变换:用线性代数解构色彩世界
图像处理·人工智能·opencv·线性代数
测试者家园1 小时前
基于DeepSeek和crewAI构建测试用例脚本生成器
人工智能·python·测试用例·智能体·智能化测试·crewai
张较瘦_1 小时前
[论文阅读] 人工智能 + 软件工程 | Call Me Maybe:用图神经网络增强JavaScript调用图构建
论文阅读·人工智能·软件工程
大模型真好玩1 小时前
准确率飙升!Graph RAG如何利用知识图谱提升RAG答案质量(四)——微软GraphRAG代码实战
人工智能·python·mcp
Baihai_IDP1 小时前
vec2text 技术已开源!一定条件下,文本嵌入向量可“近乎完美地”还原
人工智能·面试·llm
江太翁1 小时前
Pytorch torch
人工智能·pytorch·python
拓端研究室2 小时前
专题:2025即时零售与各类人群消费行为洞察报告|附400+份报告PDF、原数据表汇总下载
大数据·人工智能