Hello,
I am using Mongodb Vector database with LangChain. I would like to add a metadata to each documents
and use the metadata to filter the results.
Can someone guide me?
loader = WebBaseLoader(
[ " http://mongodb.com "
]
)
data = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=4000, chunk_overlap=500)
docs = text_splitter.split_documents(data)
metadata = {"user-id": "your-user-id"}
# Add Metadata to all docs here
client = MongoClient(self.config.mongodb_uri)
MONGODB_COLLECTION = client[self.config.vector_db_name][self.config.collection_name]
MongoDBAtlasVectorSearch.from_documents(
documents=docs,
embedding=OpenAIEmbeddings(disallowed_special=()),
collection=MONGODB_COLLECTION,
index_name=self.config.search_index_name,
metadata=metadata
)
And in retrieval
# Add pre-filter here.
vector_search = MongoDBAtlasVectorSearch.from_connection_string(
self.config.mongodb_uri,
self.config.vector_db_name + "." + self.config.collection_name,
OpenAIEmbeddings(disallowed_special=()),
index_name=self.config.search_index_name,
)
retriever = vector_search.as_retriever()