Milvus Vector Database

Use Milvus as a vector database for your Knowledge Base.

Setup

1uv pip install pymilvus

Initialize 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, use your_username:your_password as the token, otherwise don't set the token.
  • If you use Zilliz Cloud, the fully managed cloud service for Milvus, adjust the uri and token, which correspond to the Public Endpoint and API key in Zilliz Cloud.

Example

1from kern.agent import Agent
2from kern.knowledge.knowledge import Knowledge
3from kern.vectordb.milvus import Milvus
4
5vector_db = Milvus(
6 collection="recipes",
7 uri="./milvus.db",
8)
9# Create knowledge base
10knowledge_base = Knowledge(
11 vector_db=vector_db,
12)
13
14# Create and use the agent
15agent = Agent(knowledge=knowledge_base)
16
17if __name__ == "__main__":
18 knowledge_base.insert(
19 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
20 )
21
22 agent.print_response("How to make Tom Kha Gai", markdown=True)
23 agent.print_response("What was my last question?", stream=True)

Async Support ⚡

Milvus also supports asynchronous operations, enabling concurrency and leading to better performance.

1# install pymilvus - `pip install pymilvus`
2import asyncio
3
4from kern.agent import Agent
5from kern.knowledge.knowledge import Knowledge
6from kern.vectordb.milvus import Milvus
7
8# Initialize Milvus with local file
9vector_db = Milvus(
10 collection="recipes",
11 uri="tmp/milvus.db", # For local file-based storage
12)
13
14# Create knowledge base
15knowledge_base = Knowledge(
16 vector_db=vector_db,
17)
18
19# Create agent with knowledge base
20agent = Agent(knowledge=knowledge_base)
21
22if __name__ == "__main__":
23 # Load knowledge base asynchronously
24 asyncio.run(knowledge_base.ainsert(
25 url="https://kern-public.s3.amazonaws.com/recipes/ThaiRecipes.pdf"
26 )
27 )
28
29 # Query the agent asynchronously
30 asyncio.run(agent.aprint_response("How to make Tom Kha Gai", markdown=True))
Tip

Use aload() and aprint_response() methods with asyncio.run() for non-blocking operations in high-throughput applications.

Milvus Params

ParameterTypeDescriptionDefault
collectionstrName of the Milvus collectionRequired
embedderOptional[Embedder]Embedder to use for embedding documentsOpenAIEmbedder()
distanceDistanceDistance metric to use for vector similarityDistance.cosine
uristrURI of the Milvus server or path to local file"http://localhost:19530"
tokenOptional[str]Token for authentication with the Milvus serverNone

Advanced options can be passed as additional keyword arguments to the MilvusClient constructor.