autogen.agentchat.contrib.rag.MongoDBQueryEngine
MongoDBQueryEngine
A query engine backed by MongoDB Atlas that supports document insertion and querying.
This engine initializes a vector database, builds an index from input documents,
and allows querying using the chat engine interface.
Attributes:
vector_db (MongoDBAtlasVectorDB): The MongoDB vector database instance.
vector_search_engine (MongoDBAtlasVectorSearch): The vector search engine.
storage_context (StorageContext): The storage context for the vector store.
index (Optional[VectorStoreIndex]): The index built from the documents.
Initializes a MongoDBQueryEngine instance.
Name | Description |
---|---|
connection_string | Type: str |
llm | Type: ForwardRef(‘LLM’) | None Default: None |
database_name | Type: str | None Default: None |
embedding_function | Type: ForwardRef(‘BaseEmbedding’) | Callable[…, Any] | None Default: None |
embedding_model | Type: ForwardRef(‘BaseEmbedding’) | str | None Default: None |
collection_name | Type: str | None Default: None |
Instance Methods
add_docs
Adds new documents to the existing vector store index.
This method validates that the index exists, loads documents from the specified directory or file paths,
and inserts them into the vector store index.
Name | Description |
---|---|
new_doc_dir | Directory containing new documents. Type: pathlib.Path | str | None Default: None |
new_doc_paths_or_urls | List of file paths or URLs for new documents. Type: Sequence[pathlib.Path | str] | None Default: None |
*args | Additional positional arguments. Type: Any |
**kwargs | Additional keyword arguments. Type: Any |
connect_db
Connects to the MongoDB database and initializes the query index from the existing collection.
This method verifies the existence of the collection, sets up the database connection,
builds the vector store index, and pings the MongoDB server.
Returns:
bool: True if connection is successful; False otherwise.
Name | Description |
---|---|
*args | Type: Any |
**kwargs | Type: Any |
Type | Description |
---|---|
bool | bool: True if connection is successful; False otherwise. |
get_collection_name
Retrieves the name of the MongoDB collection.
Returns:
str: The collection name.
Raises:
ValueError: If the collection name is not set.
Type | Description |
---|---|
str | str: The collection name. |
init_db
Initializes the MongoDB database by creating or overwriting the collection and indexing documents.
This method loads documents from a directory or provided file paths, sets up the database (optionally
overwriting any existing collection), builds the vector store index, and inserts the documents.
Name | Description |
---|---|
new_doc_dir | Directory containing documents to be indexed. Type: pathlib.Path | str | None Default: None |
new_doc_paths_or_urls | List of file paths or URLs for documents. Type: Sequence[pathlib.Path | str] | None Default: None |
*args | Additional positional arguments. Type: Any |
**kwargs | Additional keyword arguments. Type: Any |
Type | Description |
---|---|
bool | bool: True if the database is successfully initialized; False otherwise. |
query
Queries the indexed documents using the provided question.
This method validates that the query index is initialized, creates a query engine from the vector store index,
and executes the query. If the response is empty, a default reply is returned.
Name | Description |
---|---|
question | The query question. Type: str |
*args | Type: Any |
**kwargs | Type: Any |
Type | Description |
---|---|
Any | Any: The query response as a string, or a default reply if no results are found. |