Milvus Vector Database
Use Milvus as a vector database for your Knowledge Base.
The example uses OpenAI-backed embeddings or models. Set your key before running it:
export OPENAI_API_KEY="your-api-key"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.
Setup
uv pip install -U "pymilvus[milvus-lite]" pypdf openai agnoInitialize Milvus
Set the uri and token for your Milvus server.
- If you only need a local vector database for small scale data or prototyping, setting the uri as a local file, e.g.
./milvus.db, is the most convenient method, as it automatically utilizes Milvus Lite to store all data in this file. - If you have large scale data, say more than a million vectors, you can set up a more performant Milvus server on Docker or Kubernetes.
In this setup, please use the server address and port as your uri, e.g.
http://localhost:19530. If you enable the authentication feature on Milvus, useyour_username:your_passwordas the token. Otherwise, don't set the token. - If you use Zilliz Cloud, the fully managed cloud service for Milvus, adjust the
uriandtoken, which correspond to the Public Endpoint and API key in Zilliz Cloud.
Example
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.milvus import Milvus
vector_db = Milvus(
collection="recipes",
uri="./milvus.db",
)
# Create knowledge base
knowledge_base = Knowledge(
vector_db=vector_db,
)
# Create and use the agent
agent = Agent(knowledge=knowledge_base)
if __name__ == "__main__":
knowledge_base.insert(
url="https://agno-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
)
agent.print_response("How to make Tom Kha Gai", markdown=True)Async Support ⚡
Milvus also supports asynchronous operations, enabling concurrency and leading to better performance.
import asyncio
from agno.agent import Agent
from agno.knowledge.knowledge import Knowledge
from agno.vectordb.milvus import Milvus
# Initialize Milvus with local file
vector_db = Milvus(
collection="recipes",
uri="/tmp/milvus.db", # For local file-based storage
)
# Create knowledge base
knowledge_base = Knowledge(
vector_db=vector_db,
)
# Create agent with knowledge base
agent = Agent(knowledge=knowledge_base)
async def main():
await knowledge_base.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())Use ainsert() and aprint_response() methods with asyncio.run() for non-blocking operations in high-throughput applications.
Milvus Params
| Parameter | Type | Description | Default |
|---|---|---|---|
collection | str | Name of the Milvus collection | Required |
name | Optional[str] | Name of the vector database | None |
description | Optional[str] | Description of the vector database | None |
id | Optional[str] | Unique identifier for the vector database | Auto-generated |
embedder | Optional[Embedder] | Embedder to use for embedding documents | OpenAIEmbedder() |
distance | Distance | Distance metric to use for vector similarity | Distance.cosine |
uri | str | URI of the Milvus server or path to local file | "http://localhost:19530" |
token | Optional[str] | Token for authentication with the Milvus server | None |
search_type | SearchType | Type of search to perform (vector or hybrid) | SearchType.vector |
reranker | Optional[Reranker] | Reranker to use for hybrid search results | None |
sparse_vector_dimensions | int | Dimensions of the sparse vector used for hybrid search | 10000 |
Advanced options can be passed as additional keyword arguments to the MilvusClient constructor.