You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
swarms/docs/rag-vector-databases/milvus-local.md

9.8 KiB

Milvus Local/Lite RAG Integration with Swarms

Overview

Milvus Lite is a lightweight, standalone version of Milvus that runs locally without requiring a full Milvus server deployment. It provides the core vector database functionality of Milvus in a simplified package that's perfect for development, testing, prototyping, and small-scale applications. Milvus Lite maintains compatibility with the full Milvus ecosystem while offering easier setup and deployment.

Key Features

  • Zero Configuration: No server setup or configuration required
  • Lightweight: Minimal resource footprint for local development
  • Full Compatibility: Same API as full Milvus for easy migration
  • Embedded Database: Runs as a library within your application
  • Multiple Index Types: Support for IVF, HNSW, and other algorithms
  • Persistent Storage: Local file-based storage for data persistence
  • Python Native: Pure Python implementation for easy installation
  • Cross-platform: Works on Windows, macOS, and Linux

Architecture

Milvus Lite integrates with Swarms agents as an embedded vector database solution:

[Agent] -> [Milvus Lite Memory] -> [Local Vector Store] -> [Similarity Search] -> [Retrieved Context]

The system runs entirely locally, providing fast vector operations without network overhead or external dependencies.

Setup & Configuration

Installation

pip install pymilvus[lite]  # Install with Milvus Lite support
pip install swarms
pip install litellm

Environment Variables

# Optional: Specify database path
export MILVUS_LITE_DB_PATH="./milvus_lite.db"

# OpenAI API key for LLM
export OPENAI_API_KEY="your-openai-api-key"

Dependencies

  • pymilvus>=2.3.0
  • swarms
  • litellm
  • numpy

Code Example

"""
Agent with Milvus RAG (Retrieval-Augmented Generation)

This example demonstrates using Milvus as a vector database for RAG operations,
allowing agents to store and retrieve documents for enhanced context.
"""

from swarms import Agent
from swarms_memory import MilvusDB


# Initialize Milvus wrapper for RAG operations
rag_db = MilvusDB(
    embedding_model="text-embedding-3-small",  # OpenAI embedding model
    collection_name="swarms_knowledge",        # Collection name
    db_file="swarms_milvus.db",               # Local Milvus Lite database
    metric="COSINE",                          # Distance metric for similarity search
)

# Add documents to the knowledge base
documents = [
    "Milvus is an open-source vector database built for scalable similarity search and AI applications.",
    "RAG combines retrieval and generation for more accurate AI responses.",
    "Vector embeddings enable semantic search across documents.",
    "The swarms framework supports multiple memory backends including Milvus.",
    "Swarms is the first and most reliable multi-agent production-grade framework.",
    "Kye Gomez is Founder and CEO of Swarms."
]

# Add documents individually
for doc in documents:
    rag_db.add(doc)

# Create agent with RAG capabilities
agent = Agent(
    agent_name="RAG-Agent",
    agent_description="Swarms Agent with Milvus-powered RAG for enhanced knowledge retrieval and semantic search",
    model_name="gpt-4o",
    max_loops=1,
    dynamic_temperature_enabled=True,
    long_term_memory=rag_db
)

# Query with RAG
response = agent.run("What is Milvus and how does it relate to RAG? Who is the founder of Swarms?")
print(response)

Use Cases

1. Local Development and Testing

  • Scenario: Developing RAG applications without external dependencies
  • Benefits: Zero setup, fast iteration, offline capability
  • Best For: Prototype development, unit testing, local demos

2. Edge AI Applications

  • Scenario: AI applications running on edge devices or offline environments
  • Benefits: No internet required, low latency, privacy-first
  • Best For: IoT devices, mobile apps, air-gapped systems

3. Desktop AI Applications

  • Scenario: Personal AI assistants or productivity tools
  • Benefits: Private data storage, instant startup, single-file deployment
  • Best For: Personal knowledge management, desktop utilities

4. Small-Scale Production

  • Scenario: Applications with limited data and users
  • Benefits: Simple deployment, low resource usage, cost-effective
  • Best For: MVPs, small businesses, specialized tools

Performance Characteristics

Resource Usage

  • Memory: Low baseline usage (~50MB), scales with data size
  • Storage: Efficient compression, typically 2-10x smaller than raw text
  • CPU: Optimized algorithms, good performance on consumer hardware
  • Startup: Fast initialization, typically < 1 second

Scaling Limits

  • Vectors: Recommended limit ~1M vectors for optimal performance
  • Memory: Depends on available system RAM
  • Query Speed: Sub-second response for most queries
  • Concurrent Access: Single-process access (file locking)

Performance Optimization

# Optimize for small datasets
memory = MilvusLiteMemory(
    index_type="HNSW",
    metric_type="COSINE"
)

# Optimize for memory usage
memory = MilvusLiteMemory(
    index_type="IVF_FLAT",
    metric_type="L2"
)

# Batch operations for better performance
doc_ids = memory.add_documents(documents, metadata)

Local vs Cloud Deployment

Milvus Lite Advantages

  • No External Dependencies: Runs completely offline
  • Privacy: All data stays on local machine
  • Cost: No cloud service fees
  • Simplicity: Single file deployment
  • Development: Fast iteration and debugging

Limitations Compared to Full Milvus

  • Scalability: Limited to single machine resources
  • Concurrency: No multi-client support
  • Clustering: No distributed deployment
  • Enterprise Features: Limited monitoring and management tools

Migration Path

# Development with Milvus Lite
dev_memory = MilvusLiteMemory(
    db_path="./dev_database.db",
    collection_name="dev_collection"
)

# Production with full Milvus (same API)
# from pymilvus import connections
# connections.connect(host="prod-server", port="19530")
# prod_collection = Collection("prod_collection")

File Management and Persistence

Database Files

# Default location
db_path = "./milvus_lite.db"

# Custom location with directory structure
db_path = "./data/vector_db/knowledge_base.db"

# Multiple databases for different domains
medical_memory = MilvusLiteMemory(db_path="./data/medical.db")
legal_memory = MilvusLiteMemory(db_path="./data/legal.db")

Backup Strategies

import shutil
import datetime

# Manual backup
backup_name = f"backup_{datetime.now().strftime('%Y%m%d_%H%M%S')}.db"
memory.backup_database(f"./backups/{backup_name}")

# Automated backup function
def create_scheduled_backup():
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    backup_path = f"./backups/auto_backup_{timestamp}.db"
    memory.backup_database(backup_path)
    return backup_path

Data Migration

# Export data for migration
def export_collection_data(memory):
    """Export all data from collection for migration"""
    # This would involve querying all documents and their metadata
    # Implementation depends on specific migration needs
    pass

# Import data from backup
def import_from_backup(source_path, target_memory):
    """Import data from another Milvus Lite database"""
    # Implementation for data transfer between databases
    pass

Development Workflow

Testing Setup

import tempfile
import os

def create_test_memory():
    """Create temporary memory for testing"""
    temp_dir = tempfile.mkdtemp()
    test_db_path = os.path.join(temp_dir, "test.db")
    
    return MilvusLiteMemory(
        db_path=test_db_path,
        collection_name="test_collection"
    )

# Use in tests
def test_rag_functionality():
    memory = create_test_memory()
    # Add test documents and run tests
    memory.add_document("Test document", {"category": "test"})
    results = memory.search("test", limit=1)
    assert len(results["documents"]) == 1

Debug Configuration

# Enable debug logging
import logging
logging.basicConfig(level=logging.DEBUG)

# Create memory with debug info
memory = MilvusLiteMemory(
    db_path="./debug.db",
    collection_name="debug_collection",
    index_type="HNSW"  # Good for debugging
)

# Monitor database growth
print(f"Database size: {memory.get_database_size()} bytes")
stats = memory.get_collection_stats()
print(f"Document count: {stats['row_count']}")

Best Practices

  1. Database Location: Store databases in a dedicated data directory
  2. Backup Strategy: Implement regular backups for important data
  3. Resource Management: Monitor database size and system resources
  4. Error Handling: Handle file I/O errors and database corruption
  5. Testing: Use temporary databases for unit tests
  6. Version Control: Don't commit database files to version control
  7. Documentation: Document schema and metadata conventions
  8. Migration Planning: Plan for eventual migration to full Milvus if needed

Troubleshooting

Common Issues

  1. Database File Errors: Check file permissions and disk space. Ensure directory exists before creating database. Handle concurrent access properly.

  2. Performance Issues: Monitor database size relative to available memory. Consider index type optimization for dataset size. Batch operations for better throughput.

  3. Memory Usage: Use appropriate index parameters for available RAM. Monitor system memory usage. Consider data compression techniques.

  4. Data Corruption: Implement proper backup and recovery procedures. Handle application crashes gracefully. Use database validation tools.

This comprehensive guide provides everything needed to integrate Milvus Lite with Swarms agents for local, lightweight RAG applications using the unified LiteLLM embeddings approach.