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

相关推荐
AIGC小尼3 分钟前
8G 显存零基础本地部署 AI 漫剧全流程|ComfyUI+Wan2.2+FFmpeg 离线成片完整方案(含代码 / 指令 / 排坑)
人工智能·ffmpeg·php·comfyui·ai漫剧
悟天特斯4 分钟前
AI驱动的楼宇节能:从“经验省电“到“算法能效“的进化之路
人工智能·物联网
破无差5 分钟前
人工智能训练师(三级)理论知识复习题-KimiK3的参考答案
人工智能
网络毒刘8 分钟前
AtomGit Actions + AI 评审草稿:PR 描述自动生成与安全敏感词门禁示例
人工智能·安全
ai_xiaogui8 分钟前
PanelAI 1.1.1重磅更新:秒级安装脚本优化 + 无公网IP算力节点组网,私有化AI管理平台全面升级
人工智能·网络协议·tcp/ip·api聚合管理·开发者ai一键部署·ai底层架构解析·ai应用快速变现
wflynn12 分钟前
语言判别增强多语言语音模型的语言学习能力
人工智能·ai
neocheng_52213 分钟前
自学、培训、项目还是认证?HR 学习 AI 的四种路径如何组合
人工智能
2501_9336707916 分钟前
2027风控策略岗秋招准备:SQL、Excel、建模的优先级与项目路径
人工智能·sql·excel
小宋102124 分钟前
AI 生成内容如何证明来源:C2PA、Content Credentials 与签名校验
人工智能
zhousenshan26 分钟前
AgentScope 和 Spring AI Alibaba的区别
人工智能