MongoDB Vector Database

Use MongoDB as a vector database for your Knowledge Base.

Setup

Follow the instructions in the MongoDB Setup Guide to get connection string

Install MongoDB packages

1uv pip install "pymongo[srv]"

Example

1from kern.agent import Agent
2from kern.knowledge.knowledge import Knowledge
3from kern.vectordb.mongodb import MongoVectorDb
4
5# MongoDB Atlas connection string
6"""
7Example connection strings:
8"mongodb+srv://<username>:<password>@cluster0.mongodb.net/?retryWrites=true&w=majority"
9"mongodb://localhost/?directConnection=true"
10"""
11mdb_connection_string = ""
12
13knowledge_base = Knowledge(
14 vector_db=MongoVectorDb(
15 collection_name="recipes",
16 db_url=mdb_connection_string,
17 wait_until_index_ready_in_seconds=60,
18 wait_after_insert_in_seconds=300
19 ),
20) # adjust wait_after_insert_in_seconds and wait_until_index_ready_in_seconds to your needs
21
22if __name__ == "__main__":
23 knowledge_base.insert(
24 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
25 )
26
27 agent = Agent(knowledge=knowledge_base)
28 agent.print_response("How to make Thai curry?", markdown=True)

Async Support ⚡

MongoDB also supports asynchronous operations, enabling concurrency and leading to better performance.

1import asyncio
2
3from kern.agent import Agent
4from kern.knowledge.knowledge import Knowledge
5from kern.vectordb.mongodb import MongoVectorDb
6
7# MongoDB Atlas connection string
8"""
9Example connection strings:
10"mongodb+srv://<username>:<password>@cluster0.mongodb.net/?retryWrites=true&w=majority"
11"mongodb://localhost:27017/kern?authSource=admin"
12"""
13mdb_connection_string = "mongodb+srv://<username>:<password>@cluster0.mongodb.net/?retryWrites=true&w=majority"
14
15knowledge_base = Knowledge(
16 vector_db=MongoVectorDb(
17 collection_name="recipes",
18 db_url=mdb_connection_string,
19 ),
20)
21
22# Create and use the agent
23agent = Agent(knowledge=knowledge_base)
24
25if __name__ == "__main__":
26 # Load knowledge base asynchronously
27 asyncio.run(knowledge_base.ainsert(
28 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
29 )
30 )
31
32 # Create and use the agent asynchronously
33 asyncio.run(agent.aprint_response("How to make Thai curry?", markdown=True))
Tip

Use aload() and aprint_response() methods with asyncio.run() for non-blocking operations in high-throughput applications.

MongoDB Params

ParameterTypeDescriptionDefault
collection_namestrName of the MongoDB collectionRequired
nameOptional[str]Name of the vector databaseNone
descriptionOptional[str]Description of the vector databaseNone
idOptional[str]Unique identifier for the vector databaseAuto-generated
db_urlOptional[str]MongoDB connection string"mongodb://localhost:27017/"
databasestrDatabase name"kern"
embedderOptional[Embedder]Embedder instance for generating embeddingsOpenAIEmbedder()
distance_metricstrDistance metric for similarityDistance.cosine
overwriteboolOverwrite existing collection and index if TrueFalse
wait_until_index_ready_in_secondsOptional[float]Time in seconds to wait until the index is ready3
wait_after_insert_in_secondsOptional[float]Time in seconds to wait after inserting documents3
max_pool_sizeintMaximum number of connections in the connection pool100
retry_writesboolWhether to retry write operationsTrue
clientOptional[MongoClient]An existing MongoClient instanceNone
search_index_nameOptional[str]Name of the search index"vector_index_1"
cosmos_compatibilityOptional[bool]Whether to use Azure Cosmos DB MongoDB vCore compatibility modeFalse
search_typeSearchTypeThe search type to use when searching for documentsSearchType.vector
hybrid_vector_weightfloatDefault weight for vector search results in hybrid search0.5
hybrid_keyword_weightfloatDefault weight for keyword search results in hybrid search0.5
hybrid_rank_constantintDefault rank constant (k) for Reciprocal Rank Fusion in hybrid search60