Azure Cosmos DB MongoDB vCore Vector Database

Use Azure Cosmos DB MongoDB vCore as a vector database for your Knowledge Base.

Setup

Follow the instructions in the Azure Cosmos DB Setup Guide to get the connection string.

Install MongoDB packages:

1uv pip install "pymongo[srv]"

Example

1import urllib.parse
2from kern.agent import Agent
3from kern.knowledge.knowledge import Knowledge
4from kern.vectordb.mongodb import MongoVectorDb
5
6# Azure Cosmos DB MongoDB connection string
7"""
8Example connection strings:
9"mongodb+srv://<username>:<encoded_password>@cluster0.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000"
10"""
11mdb_connection_string = f"mongodb+srv://<username>:<encoded_password>@cluster0.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000"
12
13knowledge_base = Knowledge(
14 vector_db=MongoVectorDb(
15 collection_name="recipes",
16 db_url=mdb_connection_string,
17 search_index_name="recipes",
18 cosmos_compatibility=True,
19 ),
20)
21
22knowledge.insert(
23 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
24)
25
26# Create and use the agent
27agent = Agent(knowledge=knowledge_base)
28agent.print_response("How to make Thai curry?", markdown=True)

Azure Cosmos DB MongoDB vCore 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