Replies: 1 comment
-
To persist LangChain's
Here is a code example demonstrating these steps: from langchain_community.vectorstores.chroma import Chroma
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_aws.embeddings.bedrock import BedrockEmbeddings
from langchain.storage import LocalFileStore
from langchain.retrievers import ParentDocumentRetriever
from langchain.storage._lc_store import create_kv_docstore
# Initialize the retriever
parent_splitter = RecursiveCharacterTextSplitter(chunk_size=2048, chunk_overlap=200)
child_splitter = RecursiveCharacterTextSplitter(chunk_size=512, chunk_overlap=50)
# Assuming `splits` is your list of documents
vectorstore = FAISS.from_documents(
documents=splits,
embedding=BedrockEmbeddings(
client=self.get_bedrock_runtime_client(),
model_id=model_id,
normalize=True
)
)
# The storage layer for the parent documents
local_store_path = os.path.join(config.VECTOR_DB_PERSIST_BASE, db_name, "local_docstore")
local_store = LocalFileStore(local_store_path)
docstore = create_kv_docstore(local_store)
retriever = ParentDocumentRetriever(
vectorstore=vectorstore,
docstore=docstore,
child_splitter=child_splitter,
parent_splitter=parent_splitter,
)
retriever.add_documents(splits)
# Persist the state of vectorstore and docstore
vectorstore.save("path/to/vectorstore")
docstore.save("path/to/docstore")
# Later, reinitialize the retriever
vectorstore = FAISS.load("path/to/vectorstore", embedding=BedrockEmbeddings(
client=self.get_bedrock_runtime_client(),
model_id=model_id,
normalize=True
))
docstore = LocalFileStore.load("path/to/docstore")
retriever = ParentDocumentRetriever(
vectorstore=vectorstore,
docstore=docstore,
child_splitter=child_splitter,
parent_splitter=parent_splitter,
) In this example:
Make sure to replace these placeholders with the actual methods provided by the For more detailed usage and examples, you can refer to the LangChain documentation on the |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Checked other resources
Commit to Help
Example Code
Description
In the above code splits is a list of langchain documents. I want to use ParentDocumentRetriever on them. My issue is how do i persist the ParentDocumentRetriever for a later use.
retriever.add_documents(splits) method creates 2 folders 'local_docstore' and 'local_vectorstore' but i can't figureout how do i use them to recreate aninstance of ParentDocumentRetriever with the added documents.
System Info
System Information
Package Information
Optional packages not installed
Other Dependencies
Beta Was this translation helpful? Give feedback.
All reactions