Skip to main content

Lightweight local vector database with persistence to disk, supporting multiple similarity metrics and easy-to-use API.

Project description

microvector

Lightweight local vector database with persistence to disk, supporting multiple similarity metrics and an easy-to-use API.

A refactor and repackaging of HyperDB optimized for CPU-only environments with improved type safety and developer experience.

Features

  • 🚀 Simple API: Clean, intuitive interface with just two main methods: save() and search()
  • 💾 Persistent Storage: Automatically caches vector stores to .pickle.gz files
  • 🔍 Multiple Similarity Metrics: Choose from cosine, dot product, Euclidean, or Derrida distance
  • 🎯 Type Safe: Full type annotations with strict pyright compliance
  • CPU Optimized: Designed for CPU-only environments (no CUDA required)
  • 🔄 Flexible Caching: Use persistent stores or create temporary in-memory collections
  • 📦 Easy Installation: One-command setup with automatic PyTorch CPU configuration

Installation

pip install microvector

Or for development:

git clone https://github.com/loganpowell/microvector.git
cd microvector
uv sync

Quick Start

from microvector import Client

# Initialize the client
client = Client()

# Save a collection with automatic persistence
client.save(
    partition_name="my_documents",
    collection=[
        {"text": "Python is a popular programming language", "category": "tech"},
        {"text": "Machine learning models learn from data", "category": "ai"},
        {"text": "The quick brown fox jumps over the lazy dog", "category": "example"},
    ]
)

# Search the persisted collection
results = client.search(
    term="artificial intelligence and ML",
    partition_name="my_documents",
    key="text",
    top_k=5
)

for result in results:
    print(f"Score: {result['similarity_score']:.4f} - {result['text']}")

API Reference

Client

The main interface for all vector operations.

Client(
    cache_models: str = "./.cached_models",
    cache_vectors: str = "./.vector_cache",
    embedding_model: str = "avsolatorio/GIST-small-Embedding-v0"
)

Parameters:

  • cache_models: Directory for caching downloaded embedding models
  • cache_vectors: Directory for persisting vector stores
  • embedding_model: HuggingFace model name for generating embeddings

save()

Save a collection to a persistent vector store.

client.save(
    partition_name: str,
    collection: list[dict[str, Any]],
    key: str = "text",
    algo: str = "cosine"
) -> dict[str, Any]

Parameters:

  • partition_name: Unique identifier for this vector store
  • collection: List of documents (dictionaries) to vectorize
  • key: Field name to use for embedding (default: "text")
  • algo: Similarity metric - "cosine", "dot", "euclidean", or "derrida"

Returns:

{
    "status": "success",
    "partition": "my_documents",
    "documents_saved": 42,
    "key": "text",
    "algorithm": "cosine"
}

Example:

result = client.save(
    partition_name="products",
    collection=[
        {"description": "Wireless headphones", "price": 99.99},
        {"description": "Smart watch", "price": 299.99},
    ],
    key="description",
    algo="cosine"
)

search()

Search a vector store with semantic similarity.

client.search(
    term: str,
    partition_name: str,
    key: str = "text",
    top_k: int = 5,
    collection: Optional[list[dict[str, Any]]] = None,
    cache: bool = True,
    algo: str = "cosine"
) -> list[dict[str, Any]]

Parameters:

  • term: Search query string
  • partition_name: Name of the vector store to query
  • key: property within each item in the collection to search against (vectorized field)
  • top_k: Maximum number of results to return
  • collection: Optional temporary collection (for non-persistent search)
  • cache: If True, persist the collection; if False, keep in-memory only
  • algo: Similarity metric to use

Returns: List of documents with similarity scores

[
    {
        "text": "Machine learning is awesome",
        "category": "ai",
        "similarity_score": 0.923
    },
    ...
]

Example - Search existing store:

results = client.search(
    term="laptop computers",
    partition_name="products",
    key="description",
    top_k=3
)

Example - Temporary search (no persistence):

results = client.search(
    term="budget phones",
    partition_name="temp_search",
    key="description",
    top_k=5,
    collection=[
        {"description": "iPhone 15 Pro", "price": 999},
        {"description": "Samsung Galaxy S24", "price": 899},
    ],
    cache=False  # Don't save to disk
)

Similarity Algorithms

Algorithm Best For Range
cosine General text similarity (default) 0-1 (higher is more similar)
dot When magnitude matters Unbounded
euclidean Spatial distance 0-∞ (lower is more similar)
derrida Experimental alternative distance 0-∞ (lower is more similar)

Advanced Usage

Custom Embedding Models

Use any HuggingFace sentence-transformer model:

client = Client(
    embedding_model="intfloat/e5-small-v2"
)

Nested Key Paths

Access nested fields using dot notation:

collection = [
    {
        "product": {
            "name": "Laptop",
            "specs": {"cpu": "Intel i7"}
        }
    }
]

client.save(
    partition_name="products",
    collection=collection,
    key="product.name"
)

Working with Multiple Partitions

Organize different datasets in separate partitions:

# Save different content types
client.save("news_articles", news_data, key="content")
client.save("product_reviews", review_data, key="review_text")
client.save("support_tickets", tickets, key="description")

# Search each independently
news_results = client.search("economy", "news_articles", key="content")
review_results = client.search("quality", "product_reviews", key="review_text")

Development Setup

This project uses uv for dependency management and automatically configures CPU-only PyTorch.

Quick Start

  1. Install dependencies:

    uv sync
    
  2. Verify setup:

    uv run python setup_dev.py
    
  3. Run tests:

    uv run pytest
    
  4. Type checking:

    uv run pyright
    

What Gets Installed

  • PyTorch (CPU-only): Automatically from PyTorch CPU index
  • Transformers: HuggingFace transformers library
  • Sentence Transformers: For embedding generation
  • NumPy: Numerical computing

No special flags or manual PyTorch installation needed - just uv sync and go!

Performance Tips

  1. Reuse Client instances - Model loading is expensive
  2. Use persistent caching - Vector computation is cached automatically
  3. Batch your saves - Save collections together when possible
  4. Choose the right algorithm - Cosine is fastest for most use cases
  5. Adjust top_k - Lower values are faster

Architecture

microvector/
├── main.py          # Client API
├── store.py         # Vector storage and similarity search
├── cache.py         # Persistence layer
├── embed.py         # Embedding generation
├── algos.py         # Similarity algorithms
└── utils.py         # Helper functions

License

MIT License - see LICENSE file for details.

Credits

Based on HyperDB by John Dagdelen. Refactored and maintained by Logan Powell.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

microvector-0.1.26.tar.gz (135.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

microvector-0.1.26-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file microvector-0.1.26.tar.gz.

File metadata

  • Download URL: microvector-0.1.26.tar.gz
  • Upload date:
  • Size: 135.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for microvector-0.1.26.tar.gz
Algorithm Hash digest
SHA256 3b6ef2486be4a1a04be81343bdd0448eba9fdd7a3318a6051ce402b18895b191
MD5 ea484e90c6a5827c3adfa61356d9e7b7
BLAKE2b-256 d299f0c2644df4655592b9e947699cb0ada32eac4b054f45cd84f9de743796d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for microvector-0.1.26.tar.gz:

Publisher: publish.yml on loganpowell/microvector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file microvector-0.1.26-py3-none-any.whl.

File metadata

  • Download URL: microvector-0.1.26-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for microvector-0.1.26-py3-none-any.whl
Algorithm Hash digest
SHA256 8ff57ccd5ee8d667f24ef20203d0f6b4b6ee0fb835ad059de7987b3e34181aff
MD5 7b2a577e3e6cc3d042dfb6f08553d9b8
BLAKE2b-256 1ea89efc60c874bbdb5f6960bde697e02d52312bd99eac5340b34c404d71ffe8

See more details on using hashes here.

Provenance

The following attestation bundles were made for microvector-0.1.26-py3-none-any.whl:

Publisher: publish.yml on loganpowell/microvector

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page