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

相关推荐
Godspeed Zhao1 小时前
自动驾驶中的传感器技术24.3——Camera(18)
人工智能·机器学习·自动驾驶
顾北123 小时前
MCP协议实战|Spring AI + 高德地图工具集成教程
人工智能
wfeqhfxz25887823 小时前
毒蝇伞品种识别与分类_Centernet模型优化实战
人工智能·分类·数据挖掘
中杯可乐多加冰3 小时前
RAG 深度实践系列(七):从“能用”到“好用”——RAG 系统优化与效果评估
人工智能·大模型·llm·大语言模型·rag·检索增强生成
珠海西格电力科技4 小时前
微电网系统架构设计:并网/孤岛双模式运行与控制策略
网络·人工智能·物联网·系统架构·云计算·智慧城市
FreeBuf_4 小时前
AI扩大攻击面,大国博弈引发安全新挑战
人工智能·安全·chatgpt
weisian1515 小时前
进阶篇-8-数学篇-7--特征值与特征向量:AI特征提取的核心逻辑
人工智能·pca·特征值·特征向量·降维
Java程序员 拥抱ai5 小时前
撰写「从0到1构建下一代游戏AI客服」系列技术博客的初衷
人工智能
186******205315 小时前
AI重构项目开发全流程:效率革命与实践指南
人工智能·重构
森之鸟5 小时前
多智能体系统开发入门:用鸿蒙实现设备间的AI协同决策
人工智能·harmonyos·m