LangChain整合Milvus向量数据库实战:数据新增与删除操作

导读:在AI应用开发中,向量数据库已成为处理大规模语义搜索和相似性匹配的核心组件。本文通过详实的代码示例,深入探讨LangChain框架与Milvus向量数据库的集成实践,为开发者提供生产级别的向量数据管理解决方案。

文章聚焦于向量数据库操作的两个关键环节:数据的高效新增和精准删除。通过DashScope嵌入模型的配置与应用,读者将了解如何建立稳定的向量化pipeline,实现从文本内容到向量存储的完整流程。特别值得关注的是,文章详细解析了批量文档插入的ID管理机制,以及基于ID的删除操作如何在分布式环境中保证数据一致性。

概述

本文将详细介绍如何使用LangChain框架整合Milvus向量数据库,重点演示向量数据的新增和删除操作的完整实现过程。通过实际案例,您将掌握在生产环境中管理向量数据库的核心技能。

本文继上一篇文章进一步讲述:新版LangChain向量数据库VectorStore设计详解-CSDN博客

技术需求与目标

本次实战的主要目标包括:

  • 建立LangChain与Milvus向量数据库的集成连接
  • 实现向量数据的批量插入操作
  • 掌握基于ID的数据删除机制
  • 理解向量数据库操作的最佳实践

环境配置与依赖安装

官方文档参考

LangChain官方文档地址:Milvus | 🦜️🔗 LangChain

依赖包安装

bash 复制代码
pip install langchain_milvus

核心实现代码

导入必要的库文件

python 复制代码
from langchain_community.embeddings import DashScopeEmbeddings
# 注意:旧版本使用 from langchain.vectorstores import Milvus
from langchain_milvus import Milvus  # 推荐使用新版本导入方式
from langchain_core.documents import Document

初始化嵌入模型和向量存储

python 复制代码
# 配置DashScope嵌入模型
embeddings = DashScopeEmbeddings(
    model="text-embedding-v2",  # 使用第二代通用文本嵌入模型
    max_retries=3,
    dashscope_api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # 请替换为您的实际API密钥
)

# 初始化Milvus向量存储
vector_store = Milvus(
    embeddings,
    connection_args={"uri": "http://192.168.19.152:19530"},  # Milvus服务器连接地址
    collection_name="langchain_example",  # 集合名称
)

准备测试数据集

python 复制代码
# 创建多样化的文档样本数据
document_1 = Document(
    page_content="I had chocolate chip pancakes and scrambled eggs for breakfast this morning.",
    metadata={"source": "tweet"},
)

document_2 = Document(
    page_content="The weather forecast for tomorrow is cloudy and overcast, with a high of 62 degrees.",
    metadata={"source": "news"},
)

document_3 = Document(
    page_content="Building an exciting new project with LangChain - come check it out!",
    metadata={"source": "tweet"},
)

document_4 = Document(
    page_content="Robbers broke into the city bank and stole $1 million in cash.",
    metadata={"source": "news"},
)

document_5 = Document(
    page_content="Wow! That was an amazing movie. I can't wait to see it again.",
    metadata={"source": "tweet"},
)

document_6 = Document(
    page_content="Is the new iPhone worth the price? Read this review to find out.",
    metadata={"source": "website"},
)

document_7 = Document(
    page_content="The top 10 soccer players in the world right now.",
    metadata={"source": "website"},
)

document_8 = Document(
    page_content="LangGraph is the best framework for building stateful, agentic applications!",
    metadata={"source": "tweet"},
)

document_9 = Document(
    page_content="The stock market is down 500 points today due to fears of a recession.",
    metadata={"source": "news"},
)

document_10 = Document(
    page_content="I have a bad feeling I am going to get deleted :(",
    metadata={"source": "tweet"},
)

# 将所有文档组织为列表
documents = [
    document_1, document_2, document_3, document_4, document_5,
    document_6, document_7, document_8, document_9, document_10,
]

数据插入操作

python 复制代码
# 为每个文档生成唯一的ID标识符
ids = [str(i+1) for i in range(len(documents))]
print("生成的文档ID列表:", ids)

# 执行批量文档插入操作
result = vector_store.add_documents(documents=documents, ids=ids)
print("插入操作结果:", result)

数据删除操作

python 复制代码
# 根据指定ID删除文档
result = vector_store.delete(ids=["1"])
print("删除操作结果:", result)

# 删除操作返回的统计信息解释:
# insert count: 插入数量
# delete count: 删除数量  
# upsert count: 更新插入数量
# timestamp: 操作时间戳
# success count: 成功数量
# err count: 错误数量

操作结果分析

删除操作执行后,系统返回详细的统计信息,格式示例如下:

复制代码
(insert count: 0, delete count: 1, upsert count: 0, timestamp: 456798840753225732, success count: 0, err count: 0)

该结果表明成功删除了一条记录,操作过程中未出现错误。

相关推荐
zhangfeng11337 小时前
国家超算中心 scnet.cn 跨用户文件分享流程总结 多个用户之间 文件共享 不需要反复下载上传
人工智能·语言模型·大模型
MATLAB代码顾问10 小时前
5大智能算法优化标准测试函数对比(Python实现)
开发语言·python
ting945200010 小时前
Tornado 全栈技术深度指南:从原理到实战
人工智能·python·架构·tornado
果汁华10 小时前
Browserbase Skills:让 Claude Agent 真正“看见“网页世界
人工智能·python
ZhengEnCi10 小时前
04-缩放点积注意力代码实现 💻
人工智能·python
DeepReinforce11 小时前
三、AI量化投资:使用akshare获取A股主板20260430所有的涨停股票
python·量化·akshare·龙头战法
HackTwoHub11 小时前
AI大模型网关存在SQL注入、附 POC 复现、影响版本LiteLLM 1.81.16~1.83.7(CVE-2026-42208)
数据库·人工智能·sql·网络安全·系统安全·网络攻击模型·安全架构
段一凡-华北理工大学11 小时前
【高炉炼铁领域炉温监测、预警、调控智能体设计与应用】~系列文章08:多模态数据融合:让数据更聪明
人工智能·python·高炉炼铁·ai赋能·工业智能体·高炉炉温
万粉变现经纪人11 小时前
如何解决 pip install llama-cpp-python 报错 未安装 CMake/Ninja 或 CPU 不支持 AVX 问题
开发语言·python·开源·aigc·pip·ai写作·llama
其实防守也摸鱼11 小时前
CTF密码学综合教学指南--第五章
开发语言·网络·笔记·python·安全·网络安全·密码学