LangChain ParentDocumentRetriever
LangChain ParentDocumentRetriever
from langchain.retrievers import ParentDocumentRetriever
from langchain.storage import InMemoryStore
from langchain_text_splitters import RecursiveCharacterTextSplitter
parent_splitter = RecursiveCharacterTextSplitter(chunk_size=2000)
child_splitter = RecursiveCharacterTextSplitter(chunk_size=400)
store = InMemoryStore()
retriever = ParentDocumentRetriever(
vectorstore=vectorstore, docstore=store,
child_splitter=child_splitter, parent_splitter=parent_splitter,
)
retriever.add_documents(documents)生产环境把 docstore 换成持久化 KV 存储(如 Redis、Postgres),避免重启后父块丢失。