Milvus Vector Store for LlamaIndex

1.1.0 · active · verified Thu Apr 16

The `llama-index-vector-stores-milvus` library provides an integration for LlamaIndex, enabling users to build Retrieval-Augmented Generation (RAG) systems by storing and querying vector embeddings in Milvus. Milvus is an open-source vector database designed for scalable similarity search and AI applications. As of version 1.1.0, it is an active part of the rapidly evolving LlamaIndex ecosystem, which regularly releases updates and new integrations.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up `MilvusVectorStore`, load documents using `SimpleDirectoryReader`, create a `VectorStoreIndex`, and query it. It uses Milvus Lite by default for ease of local setup and relies on OpenAI for embeddings and LLM.

import os
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader
from llama_index.vector_stores.milvus import MilvusVectorStore
from llama_index.core.settings import Settings
from llama_index.llms.openai import OpenAI
from llama_index.embeddings.openai import OpenAIEmbedding

# Set up OpenAI API key (replace with your actual key or use environment variable)
os.environ["OPENAI_API_KEY"] = os.environ.get("OPENAI_API_KEY", "sk-YOUR_OPENAI_API_KEY")

# Configure LlamaIndex settings
Settings.llm = OpenAI(model="gpt-3.5-turbo")
Settings.embed_model = OpenAIEmbedding(model="text-embedding-ada-002")

# 1. Prepare some dummy data
# Create a dummy data directory and file if they don't exist
if not os.path.exists("data"): os.makedirs("data")
with open("data/milvus_doc.txt", "w") as f:
    f.write("The Milvus vector database is designed for AI applications and similarity search. It supports efficient storage and querying of billions of vectors. LlamaIndex provides a robust framework to integrate with various vector stores, including Milvus, to build powerful RAG systems. This integration allows users to leverage Milvus's capabilities for high-performance vector search within their LlamaIndex applications.")

# 2. Load documents
documents = SimpleDirectoryReader("data").load_data()

# 3. Initialize MilvusVectorStore (using Milvus Lite for local setup)
# For Milvus Lite, uri='./milvus.db' is convenient. For a Milvus server, use 'http://localhost:19530' or your server address.
# dim must match the dimension of your embedding model (e.g., 1536 for text-embedding-ada-002)
vector_store = MilvusVectorStore(
    uri="./milvus_test.db",
    dim=Settings.embed_model.embed_dimension,
    collection_name="llama_index_milvus_collection",
    overwrite=True # Set to True for a fresh start, False to append
)

# 4. Create an index
index = VectorStoreIndex.from_documents(documents, vector_store=vector_store)

# 5. Query the index
query_engine = index.as_query_engine()
response = query_engine.query("What is Milvus used for?")

print(response)

# To demonstrate loading an existing index (if overwrite was False or after a run)
# vector_store_load = MilvusVectorStore(
#     uri="./milvus_test.db",
#     dim=Settings.embed_model.embed_dimension,
#     collection_name="llama_index_milvus_collection",
#     overwrite=False
# )
# index_loaded = VectorStoreIndex.from_vector_store(vector_store_load)
# response_loaded = index_loaded.as_query_engine().query("What is LlamaIndex?")
# print(response_loaded)

view raw JSON →