Milvus Async Hybrid Search

Run Milvus hybrid search asynchronously with ainsert() and aprint_response().

For persisted hybrid collections, set the same PYTHONHASHSEED before starting every ingestion and query process:

export PYTHONHASHSEED=0

Agno's sparse helper hashes words with Python's process hash; this is not BM25. Rebuild sparse rows created with a different seed before querying them under this setting.

This example uses Milvus Lite with a local .db file. Install the Lite extra on a platform supported by your selected Milvus Lite version; use a server URI instead when local Lite is unavailable.

Code

async_milvus_db_hybrid_search.py
import asyncio
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.milvus import Milvus, SearchType

vector_db = Milvus(
    collection="recipes", uri="/tmp/milvus_hybrid.db", search_type=SearchType.hybrid
)

knowledge = Knowledge(
    vector_db=vector_db,
)

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 Tom Kha Gai", 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 "pymilvus[milvus-lite]" pypdf openai agno

Set environment variables

export OPENAI_API_KEY=xxx

Run Agent

python async_milvus_db_hybrid_search.py