Copy
Ask AI
"""
MongoDB Hybrid Search
=====================
Demonstrates MongoDB vector + keyword hybrid retrieval.
"""
import typer
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.mongodb import MongoVectorDb
from agno.vectordb.search import SearchType
from rich.prompt import Prompt
# ---------------------------------------------------------------------------
# Setup
# ---------------------------------------------------------------------------
mdb_connection_string = "mongodb+srv://<username>:<password>@cluster0.mongodb.net/?retryWrites=true&w=majority"
vector_db = MongoVectorDb(
collection_name="recipes",
db_url=mdb_connection_string,
search_index_name="recipes",
search_type=SearchType.hybrid,
)
# ---------------------------------------------------------------------------
# Create Knowledge Base
# ---------------------------------------------------------------------------
knowledge_base = Knowledge(vector_db=vector_db)
# ---------------------------------------------------------------------------
# Create Agent
# ---------------------------------------------------------------------------
def mongodb_agent(user: str = "user"):
agent = Agent(
user_id=user,
knowledge=knowledge_base,
search_knowledge=True,
)
while True:
message = Prompt.ask(f"[bold]{user}[/bold]")
if message in ("exit", "bye"):
break
agent.print_response(message)
# ---------------------------------------------------------------------------
# Run Agent
# ---------------------------------------------------------------------------
def main() -> None:
knowledge_base.insert(
name="Recipes",
url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
metadata={"doc_type": "recipe_book"},
)
typer.run(mongodb_agent)
if __name__ == "__main__":
main()
Run the Example
Copy
Ask AI
# Clone and setup repo
git clone https://github.com/agno-agi/agno.git
cd agno/cookbook/07_knowledge/vector_db/mongo_db
# Create and activate virtual environment
./scripts/demo_setup.sh
source .venvs/demo/bin/activate
python mongo_db_hybrid_search.py