TinyVecDB is a high performance, lightweight, embedded vector database for similarity search.
Project description
TinyVecDB Python API Documentation
This document provides a comprehensive overview of the TinyVecDB Python API.
Table of Contents
Installation
pip install tinyvecdb
Core Concepts
TinyVecDB is an embedded vector database that emphasizes speed, low memory usage, and simplicity. The core of TinyVecDB is written in C, and this library provides a Python binding to that engine. The key concepts are:
- Embeddings: Fixed-dimension float vectors (e.g., 512 dimensions)
- Metadata: JSON-serializable data associated with each vector
- Similarity Search: Finding the nearest neighbors to a query vector using cosine similarity
Basic Usage
import asyncio
import random
import numpy as np
from tinyvec import TinyVecClient, TinyVecConfig, TinyVecInsertion
async def example():
# Connect to database (will create the file if it doesn't exist)
client = TinyVecClient()
config = TinyVecConfig(dimensions=512)
client.connect("./vectors.db", config)
# Create a sample vector
vector = np.random.rand(512).astype(np.float32)
insertions = []
for i in range(50):
# Using NumPy (more efficient)
vec = np.random.rand(512).astype(np.float32)
# Or using standard Python lists
# vec = [random.random() for _ in range(512)]
insertions.append(TinyVecInsertion(
vector=vec,
metadata={"id": i}
))
# Insert vectors
inserted = await client.insert(insertions)
print("Inserted:", inserted)
# Search for similar vectors
results = await client.search(vector, 5)
# Example output of search results:
"""
[TinyVecResult(similarity=0.801587700843811, index=8, metadata={'id': 8}),
TinyVecResult(similarity=0.7834401726722717, index=16, metadata={'id': 16}),
TinyVecResult(similarity=0.7815409898757935, index=5, metadata={'id': 5}),
...]
"""
if __name__ == "__main__":
asyncio.run(example())
API Reference
TinyVecClient
The main class you'll interact with is TinyVecClient. It provides all methods for managing the vector database.
Constructor and Connection
TinyVecClient()
Creates a new TinyVecDB client instance.
Example:
client = TinyVecClient()
connect(path, config)
Connects to a TinyVecDB database.
Parameters:
path:str- Path to the database fileconfig:TinyVecConfig- Configuration options
Example:
config = TinyVecConfig(dimensions=512)
client.connect("./vectors.db", config)
Instance Methods
async insert(vectors)
Inserts vectors with metadata into the database. Each metadata item must be a JSON-serializable object.
Parameters:
vectors:List[TinyVecInsertion]- List of vectors to insert
Returns:
int- The number of vectors successfully inserted
Example:
vector = np.zeros(512, dtype=np.float32) + 0.1
count = await client.insert([
TinyVecInsertion(
vector=vector,
metadata={"id": "doc1", "title": "Example Document"}
)
])
async search(query_vector, top_k)
Searches for the most similar vectors to the query vector.
Parameters:
-
query_vector:Union[List[float], np.ndarray]
A query vector to search for, which can be any of the following types:- Python list of numbers
- NumPy array (any numeric dtype)
Internally, it will be converted to a float32 array for similarity calculations.
-
top_k:int- Number of results to return
Returns:
List[TinyVecResult]- List of search results
Example:
results = await client.search(query_vector, 10)
async get_index_stats()
Retrieves statistics about the database.
Parameters:
- None
Returns:
TinyVecStats- Database statistics
Example:
stats = await client.get_index_stats()
print(
f"Database has {stats.vector_count} vectors with {stats.dimensions} dimensions"
)
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tinyvecdb-0.1.3.tar.gz.
File metadata
- Download URL: tinyvecdb-0.1.3.tar.gz
- Upload date:
- Size: 46.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2a22d58b9b56de11e2a9c375d6ff9138a24524a518c9656414db9e3cdaf1c389
|
|
| MD5 |
44375a878735bd10d8fae531bb14e977
|
|
| BLAKE2b-256 |
c28ed99b9bb5169337b6e03beb92e315103e4c2d75be2a850a89a178dc38bcd0
|
File details
Details for the file tinyvecdb-0.1.3-py3-none-win_amd64.whl.
File metadata
- Download URL: tinyvecdb-0.1.3-py3-none-win_amd64.whl
- Upload date:
- Size: 45.7 kB
- Tags: Python 3, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a9214da35aff76ab68548e093317300d0d027a98c8ce34ce583439da25b935c4
|
|
| MD5 |
3be653a54ccaaf09dd4c30c97bda923f
|
|
| BLAKE2b-256 |
d7393842034a026918cfe08573bea3f2896d32ee8bb0d9817f74c2c7d6701f1c
|