langchain 的向量存储

langchain 的向量存储

langchain内提供向量存储的功能,可以基于

  1. InMemoryVectorStore 完成内存向量存储
  2. chroma,外部数据库存储

向量存储类均提供三个通用接口:

  1. add_document,添加文档到向量存储
  2. delete,从向量存储中删除文档
  3. similarity_search,相似度搜索
python 复制代码
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_chroma import Chroma
from langchain_community.embeddings import DashScopeEmbeddings
from langchain_community.document_loaders import CSVLoader


# vector_store = InMemoryVectorStore(    #内存向量存储
#   embedding=DashScopeEmbeddings()
# )

vector_store = Chroma(   #文件持久化存储向量数据库存储
  collection_name="test",   #当前向量存储名称
  embedding_function=DashScopeEmbeddings(),  #嵌入模型
  persist_directory="./data/chroma_langchain_db"   # 指定数据存放的文件夹 
)

loader = CSVLoader(
  file_path="/Users/apple/Desktop/agent_student/qianwen/data/info.csv",
  encoding="utf-8",
  source_column="source"   #指定本条数据来源哪里
)
document = loader.load()

new_document = vector_store.add_documents(   #添加数据
  documents = document,
  ids=["id" + str(i) for i in range(1,len(document)+1)]
)

vector_store.delete(["id1","id2"])   #删除索引

result = vector_store.similarity_search(
  "python 是什么",
  10,
  filter={"source":"黑马程序员"}
)


print(result)
相关推荐
倾颜1 天前
从本地 Ollama 到线上多模型 Runtime:接入 DeepSeek / Qwen 的实战复盘
langchain·next.js·deepseek
伊布拉西莫1 天前
LangChain LCEL源码深度剖析
python·langchain
沪漂阿龙1 天前
《LangChain 系列》Human-in-the-loop:什么时候必须让人工介入?
人工智能·架构·langchain
桜吹雪1 天前
所有智能体架构(3):Planning(计划任务)
javascript·人工智能·langchain
技术达芬奇1 天前
开启你的 Agent 时代:LangChain + LangGraph 项目开发入门与语言堆栈抉择
langchain·agent
小陈phd1 天前
LCEL(LangChain Expression Language)语法全解
服务器·网络·langchain
沪漂阿龙1 天前
Context Engineering:比 Prompt Engineering 更重要的上下文工程
人工智能·langchain·prompt
沪漂阿龙1 天前
《LangChain 系列》用 LangGraph 搭建智能客服 Agent
人工智能·架构·langchain
zhiSiBuYu05171 天前
LangChain 基础对话链构建新手指南
langchain
Attachment George1 天前
山东大学软件学院-项目实训-个人开发日志(十):材料问答链路开发——文档解析、OCR兜底与持续追问完善
python·ai·langchain·kotlin·rag