
案例概述
本案例展示了如何使用LangChain的VectorStoreRetrieverMemory来存储和检索对话历史。与其他内存类型不同,VectorStoreRetrieverMemory将对话存储在向量数据库中,并通过语义相似度检索最相关的对话片段,而不是按时间顺序检索。这种基于语义的检索方式特别适合需要根据上下文相关性而非时间顺序来获取历史信息的场景,如智能客服、教育助手等应用。
核心价值:通过向量嵌入和语义检索技术,实现基于内容相关性的智能记忆系统,使AI能够根据语义关联而非时间顺序来检索历史对话,大大提高了上下文感知能力。
案例目标
- 理解
VectorStoreRetrieverMemory的工作原理和使用场景 - 掌握如何使用FAISS构建向量存储系统
- 学习如何通过语义相似度检索相关对话
- 实现基于向量检索的对话记忆系统
- 探索语义检索在AI应用中的实际应用价值
技术栈与核心依赖
核心技术
- LangChain Memory模块 - 内存管理框架
- FAISS - 高效相似度搜索库
- OpenAI Embeddings - 文本向量化
- 向量相似度检索 - 语义匹配技术
依赖包
- langchain - LangChain核心库
- langchain_openai - OpenAI集成
- langchain_community - 社区扩展
- langchain_core - 核心功能
- faiss-cpu - 向量相似度搜索
- langchain-opentutorial - 教程辅助工具
环境配置
1. 安装依赖
pip install langchain-opentutorial
pip install langchain_community langchain_openai langchain_core faiss-cpu
2. 环境变量设置
from langchain_opentutorial import set_env
set_env(
{
"OPENAI_API_KEY": "your-openai-api-key",
"LANGCHAIN_API_KEY": "your-langchain-api-key",
"LANGCHAIN_TRACING_V2": "true",
"LANGCHAIN_ENDPOINT": "https://api.smith.langchain.com",
"LANGCHAIN_PROJECT": "VectorStoreRetrieverMemory",
}
)
3. 加载环境变量
from dotenv import load_dotenv
load_dotenv(override=True)
注意:确保已正确设置OpenAI API密钥,这是使用文本嵌入功能的前提条件。
案例实现
1初始化向量存储
import faiss
from langchain_openai.embeddings import OpenAIEmbeddings
from langchain_community.docstore.in_memory import InMemoryDocstore
from langchain_community.vectorstores.faiss import FAISS
# 初始化OpenAI嵌入模型
embeddings_model = OpenAIEmbeddings()
# 创建FAISS索引
embedding_size = 1536 # OpenAI嵌入的维度大小
index = faiss.IndexFlatL2(embedding_size)
vectorstore = FAISS(embeddings_model, index, InMemoryDocstore({}), {})
这段代码创建了一个基于FAISS的内存向量存储,用于存储和检索对话嵌入向量,实现快速的相似度搜索。
2创建VectorStoreRetrieverMemory
from langchain.memory import VectorStoreRetrieverMemory
# 设置k=1以确保只返回单个最相关的对话
retriever = vectorstore.as_retriever(search_kwargs={"k": 1})
memory = VectorStoreRetrieverMemory(retriever=retriever)
通过设置k=1,我们确保每次查询只返回最相关的一个对话片段。在实际应用中,可以根据需要增加这个值以提供更多上下文。
3保存面试对话
# 保存面试对话
memory.save_context(
inputs={
"human": "Hello, thank you for coming to the interview today. Please introduce yourself."
},
outputs={
"ai": "Hello. I'm a junior developer who majored in Computer Science. In college, I mainly used Java and Python, and recently, I participated in a web development project where I gained experience developing services for real users."
},
)
memory.save_context(
inputs={"human": "What was your role in the project?"},
outputs={
"ai": "My role was as a backend developer. I was responsible for processing user data and developing server logic, implementing RESTful APIs for communication with the frontend. I also participated in database design."
},
)
memory.save_context(
inputs={
"human": "If you faced any difficulties in team projects, how did you resolve them?"
},
outputs={
"ai": "We had some communication issues at the beginning of the project. To resolve this, our team held regular meetings to share each person's progress. Also, when problems arose, we actively shared opinions and worked to find reasonable solutions."
},
)
memory.save_context(
inputs={"human": "What do you consider your strengths as a developer?"},
outputs={
"ai": "My strengths are quick learning ability and problem-solving skills. I can quickly acquire new technologies and tools, and when faced with complex problems, I can propose creative solutions. Also, I value teamwork and consider collaboration with colleagues important."
},
)
这些代码模拟了一个面试场景,保存了多轮面试对话。每段对话都会被转换为向量并存储在向量数据库中。
4检索相关对话
# 查询教育背景
print("Query: What was the interviewee's major?")
print(
memory.load_memory_variables({"prompt": "What was the interviewee's major?"})[
"history"
]
)
# 查询项目经验
print("Query: What was the interviewee's role in the project?")
print(
memory.load_memory_variables(
{"human": "What was the interviewee's role in the project?"}
)["history"]
)
通过语义相似度检索,系统可以根据查询内容找到最相关的历史对话,即使查询与原始对话的表述方式不同。
案例效果
检索效果示例
查询1:面试者的专业
查询:"What was the interviewee's major?"
检索结果:
human: Hello, thank you for coming to the interview today. Please introduce yourself.
ai: Hello. I'm a junior developer who majored in Computer Science. In college, I mainly used Java and Python, and recently, I participated in a web development project where I gained experience developing services for real users.
系统检索到包含"Computer Science"的对话,准确回答了关于专业的问题。
查询2:项目中的角色
查询:"What was the interviewee's role in the project?"
检索结果:
human: What was your role in the project?
ai: My role was as a backend developer. I was responsible for processing user data and developing server logic, implementing RESTful APIs for communication with the frontend. I also participated in database design.
系统准确检索到关于项目角色的对话,提供了详细的后端开发职责描述。
核心优势:即使查询表述与原始对话不同,系统也能通过语义相似度找到最相关的对话片段,实现了基于内容相关性的智能检索。
案例实现思路
VectorStoreRetrieverMemory工作原理
- 向量化处理:将对话内容转换为高维向量表示
- 向量存储:使用FAISS等向量数据库存储对话向量
- 相似度计算:计算查询向量与存储向量的相似度
- 语义检索:根据相似度排序返回最相关的对话
- 上下文注入:将检索到的对话作为上下文提供给LLM
与传统内存的区别
传统内存
- 按时间顺序存储对话
- 基于时间窗口检索
- 保留完整对话历史
- 适合短对话场景
VectorStoreRetrieverMemory
- 按语义相关性存储对话
- 基于相似度检索
- 保留语义相关的对话片段
- 适合长对话和复杂场景
用户查询
向量化查询
相似度计算
检索相关对话
上下文注入
LLM响应
对话历史
向量化存储
向量数据库
扩展建议
功能扩展
- 实现混合检索策略,结合关键词和语义检索
- 添加多语言支持,适应不同语言的对话检索
- 集成时间权重,平衡相关性和时效性
- 实现检索结果重排序,提高检索精度
- 添加对话重要性评分,优先检索关键对话
性能优化
- 使用更高效的向量数据库,如Milvus或Pinecone
- 实现向量索引优化,提高检索速度
- 添加向量缓存机制,减少重复计算
- 实现增量向量化,避免重复处理
- 优化嵌入模型,提高向量表示质量
应用场景扩展
- 智能客服系统:根据问题语义检索相关历史对话
- 教育平台:根据学习内容检索相关知识片段
- 医疗助手:根据症状描述检索相关病史
- 法律咨询:根据案例描述检索相关法律条文
- 个人助理:根据需求检索相关历史记录
总结
VectorStoreRetrieverMemory是LangChain提供的一种基于向量检索的内存管理方案,通过将对话内容向量化并存储在向量数据库中,实现了基于语义相关性的智能检索。这种方案具有以下优势:
🔍
语义检索
基于内容相关性而非时间顺序检索对话,提供更精准的上下文信息
⚡
高效检索
利用FAISS等高效向量数据库,实现快速的相似度搜索
🎯
精准匹配
即使查询表述不同,也能准确找到最相关的对话片段
核心价值:VectorStoreRetrieverMemory通过向量嵌入和语义检索技术,为AI应用提供了更加智能和灵活的内存管理方案。它使AI能够根据语义关联而非时间顺序来检索历史对话,大大提高了上下文感知能力,特别适合需要处理复杂对话场景的应用,如智能客服、教育助手等。
169

被折叠的 条评论
为什么被折叠?



