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.3-cp311-none-win_amd64.whl (510.7 kB view details)

Uploaded CPython 3.11Windows x86-64

lab_1806_vec_db-0.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (691.5 kB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

lab_1806_vec_db-0.3.3-cp310-none-win_amd64.whl (510.9 kB view details)

Uploaded CPython 3.10Windows x86-64

lab_1806_vec_db-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (690.5 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

lab_1806_vec_db-0.3.3-cp39-none-win_amd64.whl (511.3 kB view details)

Uploaded CPython 3.9Windows x86-64

lab_1806_vec_db-0.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (691.0 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

lab_1806_vec_db-0.3.3-cp38-none-win_amd64.whl (511.2 kB view details)

Uploaded CPython 3.8Windows x86-64

lab_1806_vec_db-0.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (691.2 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.3-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 498e97d3eab318906adb250dc394e780eab4b99f07f3349ffa40d4609df1a5f2
MD5 7e7d1fb7244c4cf760c3f22737333344
BLAKE2b-256 88780a75d1598e685cf92e554c8616421b065399baec28955f3ce7999d5da2fd

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.3-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 51c922e833f7bc99aa467dfe24c5563d5c575056ca52f298b9924486324f9e26
MD5 67bd33564e424bb0e18f7ab0ecb28017
BLAKE2b-256 c8b4660d3bbe0b7f10e353cd9c7fcd749b3261c727fea0450a40db74f647b5d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.3-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 943cfecf698ceac625a17d8b4d20f189f8e549a85c37ba70186c391ea13521af
MD5 924e94c1421365edbf6af9879ebc9f9a
BLAKE2b-256 702b1e194b58a7e8d3b1789e07f59a8e60293522f52321b210a28c914db70677

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7aa9ca7e0d7eb5530c12a5a3b9cbda04da8171b41b8279c84099a612870d5b03
MD5 f1e10bfd027fc193dba021d0522afdf3
BLAKE2b-256 2e15ced7f6a04abf8ad89504cfdf5ca9b0faf4dbc286e659ea9820ed2d372c95

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.3-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 26f3c6c6f2f15d8ad868979446cfc5f24b8dd181ba45d933a1ed18af8325843f
MD5 21479cb6e198c60b77a5545d4c94905c
BLAKE2b-256 9ea237bf213dca09397f2cb46b9b1ecffd6d68436dad26fc021901a5f2db72d5

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.3-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 f8908f0043ac54b122e7212d3638b48700afff263543e79f84da90d93ff89587
MD5 8606734dbdcb428369ce3f594bcb441b
BLAKE2b-256 d4b73712578a32c7ff3e573afe07d5f4550ff361f768ad19c3abc51f7c36d798

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.3-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 c79dad565f83402195e78bbd370d08131e762ff8e76f524ebf4879f915efa649
MD5 917f40d15b3887951051f5e67c23d131
BLAKE2b-256 1e9f29ee5ecccdb222d784cd86f00fb077f224aa9e4eaa37bbdc5fa0c23eecd1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.3-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 03cac1d42fea2d86830402e1e3eaa88d828212f48aea1af99e5dc942783948c0
MD5 5d295e9e74e29776e428db396e7fc07d
BLAKE2b-256 eac4707593e1c358906ff87499995e6bd4e43083e9435451f5a5e8ff3d4b4af8

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