No project description provided
Project description
lab-1806-vec-db
Lab 1806 Vector Database.
Getting Started with Python
# See https://pypi.org/project/lab-1806-vec-db/
pip install lab-1806-vec-db
Warning: All the arguments are positional, DO NOT use keyword arguments like upper_bound=0.5.
Basic Usage
VecDB is recommended for most cases as a high-level API.
Low-level APIs are also provided. But before using them, make sure you know what you are doing.
BareVecTable is a low-level API designed for a single table without auto-saving or multi-threading support.
calc_dist is a helper function to calculate the distance between two vectors. It supports "cosine" and "l2sqr", default to "cosine". To make sure smaller is closer, we make cosine_dist = 1 - cosine_similarity.
import os
from lab_1806_vec_db import BareVecTable, VecDB, calc_dist
def test_calc_dist():
print("\n[Test] calc_dist")
a = [0.3, 0.4]
b = [0.4, 0.3]
# norm_a = sqrt(0.3^2 + 0.4^2) = 0.5
# norm_b = sqrt(0.4^2 + 0.3^2) = 0.5
# dot_product = 0.3 * 0.4 + 0.4 * 0.3 = 0.24
# cosine_dist = 1 - (a dot b) / (|a| * |b|)
# = 1 - 0.24 / (0.5 * 0.5) = 0.04
cosine_dist = calc_dist(a, b)
print(f"{cosine_dist=}")
assert abs(cosine_dist - 0.04) < 1e-6, "Test failed"
test_calc_dist()
def test_bare_vec_table():
print("\n[Test] BareVecTable")
table = BareVecTable(dim=4)
table.add([1.0, 0.0, 0.0, 0.0], {"content": "a"})
table.add([0.0, 1.0, 0.0, 0.0], {"content": "b"})
table.add([0.0, 0.0, 1.0, 0.0], {"content": "c"})
table.batch_add(
[[1.0, 0.0, 0.0, 0.1], [0.0, 1.0, 0.0, 0.1], [0.0, 0.0, 1.0, 0.1]],
[{"content": x} for x in ["aa", "bb", "cc"]],
)
# Save and load <<<<
table.save("test_table.local.db")
table = BareVecTable.load("test_table.local.db")
os.remove("test_table.local.db")
# Save and load >>>>
results = table.search([1.0, 0.0, 0.0, 0.0], 2)
contents: list[str] = []
for metadata, d in results:
print(metadata["content"], d)
contents.append(metadata["content"])
assert (contents[0], contents[1]) == ("a", "aa"), "Test failed"
print("Test passed")
test_bare_vec_table()
def test_vec_db():
print("\n[Test] VecDB")
db = VecDB("./tmp/vec_db")
for key in db.get_all_keys():
db.delete_table(key)
keys = db.get_all_keys()
assert len(keys) == 0, "Test failed"
db.create_table_if_not_exists("table_1", 4)
db.add("table_1", [1.0, 0.0, 0.0, 0.0], {"content": "a"})
db.add("table_1", [0.0, 1.0, 0.0, 0.0], {"content": "b"})
db.add("table_1", [0.0, 0.0, 1.0, 0.0], {"content": "c"})
db.create_table_if_not_exists("table_2", 4)
db.batch_add(
"table_2",
[[1.0, 0.0, 0.0, 0.1], [0.0, 1.0, 0.0, 0.1], [0.0, 0.0, 1.0, 0.1]],
[{"content": x} for x in ["aa", "bb", "cc"]],
)
result = db.search("table_1", [1.0, 0.0, 0.0, 0.0], 3, None, 0.5)
print(result)
assert len(result) == 1, "Test failed"
assert result[0][0]["content"] == "a", "Test failed"
results = db.join_search({"table_1", "table_2"}, [1.0, 0.0, 0.0, 0.0], 2)
for key, metadata, d in results:
print(key, metadata["content"], d)
assert len(results) == 2, "Test failed"
assert results[0][0] == "table_1", "Test failed"
assert results[0][1]["content"] == "a", "Test failed"
assert results[1][0] == "table_2", "Test failed"
assert results[1][1]["content"] == "aa", "Test failed"
print("Test passed")
test_vec_db()
About auto-saving
Safe to interrupt the process on Python Level at any time with Exception or KeyboardInterrupt.
import os
from lab_1806_vec_db import VecDB
if os.path.exists("./tmp/vec_db"):
for file in os.listdir("./tmp/vec_db"):
os.remove(f"./tmp/vec_db/{file}")
# Wait for the user to see the empty dir
input("Press Enter to continue...")
# Create the database
db = VecDB("./tmp/vec_db")
db.create_table_if_not_exists("table_1", 1)
db.add("table_1", [0.0], {"content": "0"})
# Data will be written to the disk every 30 seconds
# Here we can see the dir contains a lock file.
# And after 5 seconds, the `brief.toml` will be created
# And after 30 seconds, `*.db` files will be created
# Try interrupting the process at different stages
# And check if the data appears in the disk at last
# Cases:
# - Wait for 30 seconds without doing anything
# - Enter to exit normally
# - Type "raise" to raise an exception
# - Type "dim" to do a wrong operation
# - Press Ctrl+C to interrupt the process
cmd = input("Type to choose the action: ")
if cmd == "":
exit(0) # Exit normally
elif cmd == "raise":
raise Exception("Deliberate exception")
elif cmd == "dim":
# Dimension mismatch
db.add("table_1", [0.0, 1.0], {"content": "0, 1"})
elif cmd == "len":
# Length mismatch
db.batch_add("table_1", [[0.0], [1.0]], [{"content": "0"}])
# File appears before the program exits in all cases, even with Exception or KeyboardInterrupt
Development with Rust
# Install Rustup
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
. "$HOME/.cargo/env"
# Then install the rust-analyzer extension in VSCode.
# You may need to set "rust-analyzer.runnables.extraEnv" in VSCode Machine settings.
# The value should be like {"PATH":""} and make sure that `/home/YOUR_NAME/.cargo/bin` is in it.
# Otherwise you may fail when press the `Run test` button.
# Run tests
# Add `-r` to test with release mode
cargo test
# Or you can click the 'Run Test' button in VSCode to show output.
# Our GitHub Actions will also run the tests.
Test the python binding with test_pyo3.py.
# Install Python 3.10
brew install python@3.10
# or on Windows
scoop bucket add versions
scoop install python310
# Install uv.
# See https://github.com/astral-sh/uv for alternatives.
pip install uv
# or on Windows
scoop install uv
# Run the Python test
uv sync --reinstall-package lab_1806_vec_db
uv run ./test_pyo3.py
# Build the Python Wheel Release
# This will be automatically run in GitHub Actions.
uv build
Examples Binaries
See also the Binaries at src/bin/, and the Examples at examples/.
src/bin/convert_fvecs.rs: Convert the fvecs format to the binary format.src/bin/gen_ground_truth.rs: Generate the ground truth for the query.examples/bench.rs: The benchmark for index algorithms.
Check the comments at the end of the source files for the usage.
Dataset
Download Gist1M dataset from:
-
Official: http://corpus-texmex.irisa.fr/
-
Ours: Recommended faster, and already converted to the binary format. We also provide pre-built config file & ground truth & HNSW index.
https://huggingface.co/datasets/pku-lab-1806-llm/gist-for-lab-1806-vec-db
Then, you can run the examples to test the database.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
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 lab_1806_vec_db-0.4.0-cp311-none-win_amd64.whl.
File metadata
- Download URL: lab_1806_vec_db-0.4.0-cp311-none-win_amd64.whl
- Upload date:
- Size: 541.9 kB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a56d18ced706cce34b7c0abb295b23aac7574f9a68db573760b8752c75cc2a9
|
|
| MD5 |
1007ae055de5bfd1fc64c584bd1781c9
|
|
| BLAKE2b-256 |
a35ecb666989df25445ba6ea612766e83ccba1aaa37eac96e3933b3f1dc307a6
|
File details
Details for the file lab_1806_vec_db-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: lab_1806_vec_db-0.4.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 738.7 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
724f0ef8fd34809b3e0d8864a226a9c1825b0af00a6d9d4f0df972f883bad3f1
|
|
| MD5 |
53a507a4c1283e399d584b2979cc5a07
|
|
| BLAKE2b-256 |
aecc955e49defb4ea87f3c9b2259e0a07c11d4885cf3e6770d389551aaed3743
|
File details
Details for the file lab_1806_vec_db-0.4.0-cp310-none-win_amd64.whl.
File metadata
- Download URL: lab_1806_vec_db-0.4.0-cp310-none-win_amd64.whl
- Upload date:
- Size: 542.5 kB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
925035fec7e39950ac7e0a34c9e3c75af6f7b57c056a016d1c55f8e186cb7b6a
|
|
| MD5 |
79cef1d300b3bb80d5a7c55a8351c11e
|
|
| BLAKE2b-256 |
05398674af0b909ace2551183e2cfc1df626770f4cf8a001254d8ddbbfea8c38
|
File details
Details for the file lab_1806_vec_db-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: lab_1806_vec_db-0.4.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 738.9 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a711b78359abf812e691f33bd9ecd74d3e1b4792fb52fbd7b742e4f330563f68
|
|
| MD5 |
06ec3f8952735c590bf8b447b4ffae2f
|
|
| BLAKE2b-256 |
8e34723cec3118d34e4babf0b717731888b3e704224a6bf23dc4c9fbd5c92473
|
File details
Details for the file lab_1806_vec_db-0.4.0-cp39-none-win_amd64.whl.
File metadata
- Download URL: lab_1806_vec_db-0.4.0-cp39-none-win_amd64.whl
- Upload date:
- Size: 542.6 kB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e2e51768cafec30680145c10c4d612e0982d5f7d0fde8da70844b08a990a59c
|
|
| MD5 |
5d5471a45ec4ddcd8e1d3788e3c2f398
|
|
| BLAKE2b-256 |
b46332242a6ebfb2c126890c10c309d46cef8cd6afe222265758d9337d505294
|
File details
Details for the file lab_1806_vec_db-0.4.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: lab_1806_vec_db-0.4.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 738.9 kB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1daedcf37c2350a677f52236783c3dc71544042378dfaec41c448a3f72126e90
|
|
| MD5 |
3e6427f5d12dbce8a598951f3f5b021c
|
|
| BLAKE2b-256 |
9a958d0dca3412bf0368b5a30fcf1a6e328d6a6152c5f1559e2332f840a4df4c
|
File details
Details for the file lab_1806_vec_db-0.4.0-cp38-none-win_amd64.whl.
File metadata
- Download URL: lab_1806_vec_db-0.4.0-cp38-none-win_amd64.whl
- Upload date:
- Size: 542.5 kB
- Tags: CPython 3.8, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cd4b895bccf893850426b72a42b3132014156fb464a032abea73eb37bf664005
|
|
| MD5 |
4bc0b3fad0e51c8696e5193f7daf12c6
|
|
| BLAKE2b-256 |
8d5e15f61cd176ebe4b303e9796d423a4a3cdfdedc1bf0187f61bea721f2a2d0
|
File details
Details for the file lab_1806_vec_db-0.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: lab_1806_vec_db-0.4.0-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 739.4 kB
- Tags: CPython 3.8, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: maturin/1.7.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8289ee5d9f57e643ca57ca9fbac5e2bb7ff6dbb6149efac20f0161abfacc7bd0
|
|
| MD5 |
fd14b77e61e4f78cba262aa01a389f31
|
|
| BLAKE2b-256 |
3ef3295133f2563afad3ea1655ead0c087a3aa1744496cfe1bc7d4bb23d4ae53
|