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

[复制链接]
发表于 2025-7-7 19:04:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
导读:在AI应用开辟中,向量数据库已成为处理惩罚大规模语义搜索和相似性匹配的核心组件。本文通过详确的代码示例,深入探究LangChain框架与Milvus向量数据库的集成实践,为开辟者提供生产级别的向量数据管明确决方案。
  文章聚焦于向量数据库操作的两个关键环节:数据的高效新增和精准删除。通过DashScope嵌入模子的设置与应用,读者将相识怎样建立稳定的向量化pipeline,实现从文本内容到向量存储的完整流程。特别值得关注的是,文章详细解析了批量文档插入的ID管理机制,以及基于ID的删除操作怎样在分布式环境中包管数据同等性。
  概述

本文将详细先容怎样使用LangChain框架整合Milvus向量数据库,重点演示向量数据的新增和删除操作的完整实现过程。通过实际案例,您将掌握在生产环境中管理向量数据库的核心技能。
   本文继上一篇文章进一步报告:新版LangChain向量数据库VectorStore设计详解-CSDN博客
  技术需求与目标

本次实战的重要目标包罗:


  • 建立LangChain与Milvus向量数据库的集成连接
  • 实现向量数据的批量插入操作
  • 掌握基于ID的数据删除机制
  • 明确向量数据库操作的最佳实践
环境设置与依赖安装

官方文档参考

LangChain官方文档地址:Milvus | 🦜️🔗 LangChain
依赖包安装

  1. pip install langchain_community
  2. pip install dashscope
  3. pip install langchain_milvus
复制代码
核心实现代码

导入必要的库文件

  1. from langchain_community.embeddings import DashScopeEmbeddings
  2. # 注意:旧版本使用 from langchain.vectorstores import Milvus
  3. from langchain_milvus import Milvus  # 推荐使用新版本导入方式
  4. from langchain_core.documents import Document
复制代码
初始化嵌入模子和向量存储

  1. # 配置DashScope嵌入模型
  2. embeddings = DashScopeEmbeddings(
  3.     model="text-embedding-v2",  # 使用第二代通用文本嵌入模型
  4.     max_retries=3,
  5.     dashscope_api_key="sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"  # 请替换为您的实际API密钥
  6. )
  7. # 初始化Milvus向量存储
  8. vector_store = Milvus(
  9.     embeddings,
  10.     connection_args={"uri": "http://192.168.19.152:19530"},  # Milvus服务器连接地址
  11.     collection_name="langchain_example",  # 集合名称
  12. )
复制代码
准备测试数据集

  1. # 创建多样化的文档样本数据
  2. document_1 = Document(
  3.     page_content="I had chocolate chip pancakes and scrambled eggs for breakfast this morning.",
  4.     metadata={"source": "tweet"},
  5. )
  6. document_2 = Document(
  7.     page_content="The weather forecast for tomorrow is cloudy and overcast, with a high of 62 degrees.",
  8.     metadata={"source": "news"},
  9. )
  10. document_3 = Document(
  11.     page_content="Building an exciting new project with LangChain - come check it out!",
  12.     metadata={"source": "tweet"},
  13. )
  14. document_4 = Document(
  15.     page_content="Robbers broke into the city bank and stole $1 million in cash.",
  16.     metadata={"source": "news"},
  17. )
  18. document_5 = Document(
  19.     page_content="Wow! That was an amazing movie. I can't wait to see it again.",
  20.     metadata={"source": "tweet"},
  21. )
  22. document_6 = Document(
  23.     page_content="Is the new iPhone worth the price? Read this review to find out.",
  24.     metadata={"source": "website"},
  25. )
  26. document_7 = Document(
  27.     page_content="The top 10 soccer players in the world right now.",
  28.     metadata={"source": "website"},
  29. )
  30. document_8 = Document(
  31.     page_content="LangGraph is the best framework for building stateful, agentic applications!",
  32.     metadata={"source": "tweet"},
  33. )
  34. document_9 = Document(
  35.     page_content="The stock market is down 500 points today due to fears of a recession.",
  36.     metadata={"source": "news"},
  37. )
  38. document_10 = Document(
  39.     page_content="I have a bad feeling I am going to get deleted :(",
  40.     metadata={"source": "tweet"},
  41. )
  42. # 将所有文档组织为列表
  43. documents = [
  44.     document_1, document_2, document_3, document_4, document_5,
  45.     document_6, document_7, document_8, document_9, document_10,
  46. ]
复制代码
数据插入操作

  1. # 为每个文档生成唯一的ID标识符
  2. ids = [str(i+1) for i in range(len(documents))]
  3. print("生成的文档ID列表:", ids)
  4. # 执行批量文档插入操作
  5. result = vector_store.add_documents(documents=documents, ids=ids)
  6. print("插入操作结果:", result)
复制代码
数据删除操作

  1. # 根据指定ID删除文档
  2. result = vector_store.delete(ids=["1"])
  3. print("删除操作结果:", result)
  4. # 删除操作返回的统计信息解释:
  5. # insert count: 插入数量
  6. # delete count: 删除数量  
  7. # upsert count: 更新插入数量
  8. # timestamp: 操作时间戳
  9. # success count: 成功数量
  10. # err count: 错误数量
复制代码
操作结果分析

删除操作执行后,系统返回详细的统计信息,格式示例如下:
  1. (insert count: 0, delete count: 1, upsert count: 0, timestamp: 456798840753225732, success count: 0, err count: 0)
复制代码
该结果表明乐成删除了一条纪录,操作过程中未出现错误。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
继续阅读请点击广告
回复

使用道具 举报

快速回复 返回顶部 返回列表