MongoDB Atlas is a document database that can be used as a vector database.In the walkthrough, we’ll demo the
SelfQueryRetriever
with a MongoDB Atlas
vector store.
Creating a MongoDB Atlas vectorstore
First we’ll want to create a MongoDB Atlas VectorStore and seed it with some data. We’ve created a small demo set of documents that contain summaries of movies. NOTE: The self-query retriever requires you to havelark
installed (pip install lark
). We also need the pymongo
package.
OpenAIEmbeddings
so we have to get the OpenAI API Key.
embedding
is the name of the field that contains the embedding vector. Please refer to the documentation to get more details on how to define an Atlas Vector Search index.
You can name the index {COLLECTION_NAME}
and create the index on the namespace {DB_NAME}.{COLLECTION_NAME}
. Finally, write the following definition in the JSON editor on MongoDB Atlas:
Creating our self-querying retriever
Now we can instantiate our retriever. To do this we’ll need to provide some information upfront about the metadata fields that our documents support and a short description of the document contents.Testing it out
And now we can try actually using our retriever!Filter k
We can also use the self query retriever to specifyk
: the number of documents to fetch.
We can do this by passing enable_limit=True
to the constructor.