MongoDB Async

Insert knowledge and query it asynchronously with ainsert() and aprint_response() on MongoDB.

Code

async_mongo_db.py
import asyncio

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,
    ),
)

agent = Agent(knowledge=knowledge)

async def main():
    await knowledge.ainsert(
        url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
    )
    await agent.aprint_response("How to make Thai curry?", markdown=True)

if __name__ == "__main__":
    asyncio.run(main())

Usage

Set up your virtual environment

uv venv --python 3.12
source .venv/bin/activate

Install dependencies

uv pip install -U pymongo pypdf openai agno

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

Set environment variables

export OPENAI_API_KEY=xxx

Run Agent

python async_mongo_db.py