Nano VectorDB

0.0.4.3 · active · verified Fri Apr 17

Nano VectorDB is a simple, easy-to-hack, in-memory, disk-persisted vector database implementation. It's designed for rapid prototyping, educational purposes, and small-scale applications, offering a lightweight alternative to more complex solutions. The current version is 0.0.4.3, with an active development cadence featuring frequent minor releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize NanoVectorDB, add vectors with associated metadata and unique IDs, perform a similarity search, and persist/load the database from disk. It also includes cleanup for the database directory.

import numpy as np
from nano_vectordb import NanoVectorDB
import os
import shutil

# Initialize the database
db_path = "my_nano_vectordb"
db = NanoVectorDB(db_path, dim=4)

# Add vectors with metadata and IDs
db.add(np.array([1.0, 2.0, 3.0, 4.0]), {"text": "The quick brown fox."}, "doc1")
db.add(np.array([1.1, 2.1, 3.1, 4.1]), {"text": "Jumps over the lazy dog."}, "doc2")
db.add(np.array([0.9, 1.9, 2.9, 3.9]), {"text": "Another relevant document."}, "doc3")

# Perform a similarity search
query_vector = np.array([1.0, 2.0, 3.0, 4.0])
k_results = 2
results = db.search(query_vector, k=k_results)

print(f"\nSearch Results for top {k_results} documents:")
for vector, metadata, vector_id, score in results:
    print(f"  ID: {vector_id}, Metadata: {metadata}, Score: {score:.4f}")

# Save the database to disk
db.save()
print(f"\nDatabase saved to '{db_path}'")

# Load the database from disk
loaded_db = NanoVectorDB(db_path, dim=4) # Re-initialize with path and dim
loaded_db.load()
print(f"Database loaded from '{db_path}'. Number of items: {len(loaded_db.store)}")

# Clean up database files (optional)
if os.path.exists(db_path):
    shutil.rmtree(db_path)
    print(f"Cleaned up database directory: {db_path}")

view raw JSON →