Skip to main content

No project description provided

Project description

lab-1806-vec-db

Lab 1806 Vector Database.

Usage with Python

# See https://pypi.org/project/lab-1806-vec-db/
pip install lab-1806-vec-db

Example usage:

import os

from lab_1806_vec_db import BareVecTable, VecDB, calc_dist


def test_calc_dist():
    print("\n[Test] calc_dist")
    dist0 = calc_dist([1.0, 0.0], [0.0, 1.0])  # default: "cosine"
    print(f"{dist0=}")
    assert abs(dist0 - 1.0) < 1e-6, "Test failed"
    print("Test passed")

    print("\n[Test] calc_dist with invalid metric")
    try:
        dist1 = calc_dist([1.0, 0.0], [0.0, 1.0], "euclidean")
        print(f"{dist1=}")
        assert False, "Test failed"
    except ValueError as e:
        print(f"Got expected exception: {e}")
        print("Test passed")


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()

Warning: All the arguments are positional, do not use keyword arguments like upper_bound=0.5.

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:

Then, you can run the examples to test the database.

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

lab_1806_vec_db-0.3.1-cp311-none-win_amd64.whl (510.3 kB view details)

Uploaded CPython 3.11Windows x86-64

lab_1806_vec_db-0.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (692.0 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

lab_1806_vec_db-0.3.1-cp310-none-win_amd64.whl (510.6 kB view details)

Uploaded CPython 3.10Windows x86-64

lab_1806_vec_db-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (691.4 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

lab_1806_vec_db-0.3.1-cp39-none-win_amd64.whl (510.8 kB view details)

Uploaded CPython 3.9Windows x86-64

lab_1806_vec_db-0.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (691.7 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

lab_1806_vec_db-0.3.1-cp38-none-win_amd64.whl (510.9 kB view details)

Uploaded CPython 3.8Windows x86-64

lab_1806_vec_db-0.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (692.1 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

Details for the file lab_1806_vec_db-0.3.1-cp311-none-win_amd64.whl.

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.1-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 f19cbb1fd1fc7a3cbb0e815885a6fca2e87551af369228177e1235cc1f0f48e6
MD5 8032d9bead8d69a5a91c79aabfc52292
BLAKE2b-256 4e5f811e4a5617e21357c34e67d467680ba1eb70e7b9e1101ffd85a184c392d1

See more details on using hashes here.

File details

Details for the file lab_1806_vec_db-0.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 ef6ab2b9e806a0f87c466896090da845d2807b2e437f816ee00ee75b9f790e1f
MD5 75327e1e994a9d7cc446fb7d5715e50a
BLAKE2b-256 6b1dfa93ff9d522e5e5bcaf10b416ae7552ce222f3bea65dd84e7874f58298a8

See more details on using hashes here.

File details

Details for the file lab_1806_vec_db-0.3.1-cp310-none-win_amd64.whl.

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.1-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 bc600974a73300931332de9d377e702eea123f678d9dc4fabeac905d00a21d3a
MD5 a8302c279571c5245ecc24d97d1066ed
BLAKE2b-256 77cea78b6dc1bb689fb1ee163130fb7efa450811cf6419407683c806ac65440a

See more details on using hashes here.

File details

Details for the file lab_1806_vec_db-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 626b3e1c5f7a31b98cb8a41ea5b7db8242e5f611d977280a2e70fbb923f0aa56
MD5 4757058fea9e8364b323c4ce3412b5c7
BLAKE2b-256 905c535c3c10e6a1669198b73928d4339b98e1a3b2745cb7cf0390370c1be77d

See more details on using hashes here.

File details

Details for the file lab_1806_vec_db-0.3.1-cp39-none-win_amd64.whl.

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.1-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 bf6d88a7adb15ea520b984f017ec4a7807e088c8ddfdf20af1c4871c90f0dc21
MD5 9164fa3bb28f38979eca13bd910f1ac6
BLAKE2b-256 7fd2bf146df4023e277a1d4c24275b222706069b2e5c5cb11088bb838e8d0333

See more details on using hashes here.

File details

Details for the file lab_1806_vec_db-0.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9064da13e7acff0b04650271cb55e8ce9cecbc6c34ff8633b9c0d3e5b0df01a7
MD5 c0ec155a413a8dcb11e5398ebcfaee6a
BLAKE2b-256 d87bb573616a06050a1c2c4877b722a7fe79480bcc960411658954b79deec343

See more details on using hashes here.

File details

Details for the file lab_1806_vec_db-0.3.1-cp38-none-win_amd64.whl.

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.1-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 20a0fe1a80c75d01a186da6e71cddbd28604b37a5e9df3b4b6a3cad511b639d9
MD5 3ec69ea3a08601f165d72fcd08738741
BLAKE2b-256 2e4e03514746956eb5db30fd7f85910b102c1161c85844c50310bda94f37cde1

See more details on using hashes here.

File details

Details for the file lab_1806_vec_db-0.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6ea5d25b6709add5c90935cd1805267141338978465dafa002b0748cdc946f55
MD5 68edc4bf7aa586ce0b7b8448db1650b9
BLAKE2b-256 bce35430f672705a7ce2b85e87abba204f2fbde04bf862c7eaba15d7c199dee4

See more details on using hashes here.

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