Milvus Hybrid Search
Combine dense embeddings and sparse keyword vectors in one Milvus search with SearchType.hybrid.
For persisted hybrid collections, set the same PYTHONHASHSEED before starting every ingestion and query process:
export PYTHONHASHSEED=0Agno'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
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,
)
knowledge.insert(
url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf",
)
agent = Agent(knowledge=knowledge)
agent.print_response("How to make Tom Kha Gai", markdown=True)Usage
Set up your virtual environment
uv venv --python 3.12
source .venv/bin/activateInstall dependencies
uv pip install -U "pymilvus[milvus-lite]" pypdf openai agnoSet environment variables
export OPENAI_API_KEY=xxxRun Agent
python milvus_db_hybrid_search.py