Setup
Follow the instructions in the Azure Cosmos DB Setup Guide to get the connection string. Install MongoDB packages:uv pip install "pymongo[srv]"
Example
agent_with_knowledge.py
import urllib.parse
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.mongodb import MongoVectorDb
# Azure Cosmos DB MongoDB connection string
"""
Example connection strings:
"mongodb+srv://<username>:<encoded_password>@cluster0.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000"
"""
mdb_connection_string = f"mongodb+srv://<username>:<encoded_password>@cluster0.mongocluster.cosmos.azure.com/?tls=true&authMechanism=SCRAM-SHA-256&retrywrites=false&maxIdleTimeMS=120000"
knowledge_base = Knowledge(
vector_db=MongoVectorDb(
collection_name="recipes",
db_url=mdb_connection_string,
search_index_name="recipes",
cosmos_compatibility=True,
),
)
knowledge.insert(
url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
)
# Create and use the agent
agent = Agent(knowledge=knowledge_base)
agent.print_response("How to make Thai curry?", markdown=True)
Azure Cosmos DB MongoDB vCore Params
| Parameter | Type | Description | Default |
|---|---|---|---|
collection_name | str | Name of the MongoDB collection | Required |
name | Optional[str] | Name of the vector database | None |
description | Optional[str] | Description of the vector database | None |
id | Optional[str] | Unique identifier for the vector database | Auto-generated |
db_url | Optional[str] | MongoDB connection string | "mongodb://localhost:27017/" |
database | str | Database name | "agno" |
embedder | Optional[Embedder] | Embedder instance for generating embeddings | OpenAIEmbedder() |
distance_metric | str | Distance metric for similarity | Distance.cosine |
overwrite | bool | Overwrite existing collection and index if True | False |
wait_until_index_ready_in_seconds | Optional[float] | Time in seconds to wait until the index is ready | 3 |
wait_after_insert_in_seconds | Optional[float] | Time in seconds to wait after inserting documents | 3 |
max_pool_size | int | Maximum number of connections in the connection pool | 100 |
retry_writes | bool | Whether to retry write operations | True |
client | Optional[MongoClient] | An existing MongoClient instance | None |
search_index_name | Optional[str] | Name of the search index | "vector_index_1" |
cosmos_compatibility | Optional[bool] | Whether to use Azure Cosmos DB MongoDB vCore compatibility mode | False |
search_type | SearchType | The search type to use when searching for documents | SearchType.vector |
hybrid_vector_weight | float | Default weight for vector search results in hybrid search | 0.5 |
hybrid_keyword_weight | float | Default weight for keyword search results in hybrid search | 0.5 |
hybrid_rank_constant | int | Default rank constant (k) for Reciprocal Rank Fusion in hybrid search | 60 |