Code
mongo_db.py
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.mongodb import MongoVectorDb
mdb_connection_string = "mongodb://localhost:27017"
knowledge = Knowledge(
vector_db=MongoVectorDb(
collection_name="recipes",
db_url=mdb_connection_string,
search_index_name="recipes",
),
)
knowledge.insert(
name="Recipes",
url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
metadata={"doc_type": "recipe_book"},
)
# Create and use the agent
agent = Agent(knowledge=knowledge)
agent.print_response("How to make Thai curry?", markdown=True)
Usage
1
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activate
uv venv --python 3.12
.venv\Scripts\activate
2
Install dependencies
uv pip install -U pymongo pypdf openai agno
3
Run MongoDB
docker run -d \
--rm \
--name mongodb-container \
-p 27017:27017 \
-v ./tmp/mongo-data:/data/db \
mongodb/mongodb-atlas-local:8.0.3
4
Set environment variables
export OPENAI_API_KEY=xxx
5
Run Agent
python mongo_db.py