Milvus is an open-source vector database specifically designed to handle and query large amounts of high-dimensional vector data, such as embeddings. It’s optimized for similarity search and machine learning applications, making it well-suited for applications like recommendation systems, image and video search, natural language processing, and other AI-driven use cases.
Milvus Lite is a lightweight version of the Milvus vector database designed specifically for local or small-scale deployments. Unlike the standard Milvus setup, which can be resource-intensive and often runs in distributed environments (such as Kubernetes or on cloud infrastructure), Milvus Lite is streamlined for lower resource usage, making it ideal for development, testing, and small production workloads.
Milvus Lite currently supports the following environmnets:
Ubuntu >= 20.04 (x86_64 and arm64)
MacOS >= 11.0 (Apple Silicon M1/M2 and x86_64)
Memory: 2 GB RAM or more
CPU: Any modern multi-core CPU
Milvus Lite can be imported into your Python application, providing the core vector search functionality of Milvus. Milvus Lite is already included in the Python SDK of Milvus. It can be simply deployed with pip.
We recommend using pymilvus. Since milvus-lite is included in pymilvus version 2.4.2 or above, you can pip install with -U to force update to the latest version and milvus-lite is automatically installed.
pip install -U pymilvus
In pymilvus, specify a local file name as uri parameter of MilvusClient will use Milvus Lite.
from pymilvus import MilvusClient client = MilvusClient("./milvus_demo.db")
After running the above code snippet, a database file named milvus_demo.db will be generated in the current folder.
In Milvus, we need a collection to store vectors and their associated metadata. You can think of it as a table in traditional SQL databases. For now, let’s just focus on the basics and use default for everything possible. At minimum, you only need to set the collection name and the dimension of the vector field of the collection.
if client.has_collection(collection_name="demo_collection"): client.drop_collection(collection_name="demo_collection") client.create_collection( collection_name="demo_collection", dimension=768, # The vectors we will use in this demo has 768 dimensions )
In this guide, we use vectors to perform semantic search on text. We need to generate vectors for text by downloading embedding models. This can be easily done by using the utility functions from pymilvus[model] library.
pip install "pymilvus[model]"
Generate vector embeddings with default model. Milvus expects data to be inserted organized as a list of dictionaries, where each dictionary represents a data record, termed as an entity.
from pymilvus import model embedding_fn = model.DefaultEmbeddingFunction() docs = [ "Artificial intelligence was founded as an academic discipline in 1956.", "Alan Turing was the first person to conduct substantial research in AI.", "Born in Maida Vale, London, Turing was raised in southern England.", ] vectors = embedding_fn.encode_documents(docs) print("Dim:", embedding_fn.dim, vectors[0].shape) # Dim: 768 (768,) data = [ {"id": i, "vector": vectors[i], "text": docs[i], "subject": "history"} for i in range(len(vectors)) ] print("Data has", len(data), "entities, each with fields: ", data[0].keys()) print("Vector dim:", len(data[0]["vector"]))
Output samples:
Dim: 768 (768,) Data has 3 entities, each with fields: dict_keys(['id', 'vector', 'text', 'subject']) Vector dim: 768
Let’s insert the data into the collection:
res = client.insert(collection_name="demo_collection", data=data) print(res)
Output samples:
{'insert_count': 3, 'ids': [0, 1, 2], 'cost': 0}
Now we can do semantic searches by representing the search query text as vector, and conduct vector similarity search on Milvus. Milvus accepts one or multiple vector search requests at the same time. The value of the query_vectors variable is a list of vectors, where each vector is an array of float numbers.
query_vectors = embedding_fn.encode_queries(["Who is Alan Turing?"]) res = client.search( collection_name="demo_collection", # target collection data=query_vectors, # query vectors limit=2, # number of returned entities output_fields=["text", "subject"], # specifies fields to be returned ) print(res)
Output samples:
data: ["[{'id': 2, 'distance': 0.5859944820404053, 'entity': {'text': 'Born in Maida Vale, London, Turing was raised in southern England.', 'subject': 'history'}}, {'id': 1, 'distance': 0.5118255615234375, 'entity': {'text': 'Alan Turing was the first person to conduct substantial research in AI.', 'subject': 'history'}}]"] , extra_info: {'cost': 0}
If you’d like to purge data, you can delete entities specifying the primary key or delete all entities matching a particular filter expression.
res = client.delete(collection_name="demo_collection", ids=[0, 2]) print(res) res = client.delete( collection_name="demo_collection", filter="subject == 'biology'", ) print(res)
Output samples:
[0, 2] [3, 4, 5]
If you would like to delete all the data in a collection, you can drop the collection with
client.drop_collection(collection_name="demo_collection")
Milvus Lite is great for getting started with a local python program. If you have large scale data or would like to use Milvus in production, you can learn about deploying Milvus on Docker and Kubernetes. All deployment modes of Milvus share the same API, so your client side code doesn’t need to change much if moving to another deployment mode. Simply specify the URI and Token of a Milvus server deployed anywhere:
client = MilvusClient(uri="http://localhost:19530", token="root:Milvus")
Milvus provides REST and gRPC API, with client libraries in languages such as Python, Java, Go, C# and Node.js.