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

Uploaded CPython 3.11Windows x86-64

lab_1806_vec_db-0.3.2-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.2-cp310-none-win_amd64.whl (510.4 kB view details)

Uploaded CPython 3.10Windows x86-64

lab_1806_vec_db-0.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (691.3 kB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

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

Uploaded CPython 3.9Windows x86-64

lab_1806_vec_db-0.3.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (691.6 kB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

lab_1806_vec_db-0.3.2-cp38-none-win_amd64.whl (510.8 kB view details)

Uploaded CPython 3.8Windows x86-64

lab_1806_vec_db-0.3.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (692.0 kB view details)

Uploaded CPython 3.8manylinux: glibc 2.17+ x86-64

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.2-cp311-none-win_amd64.whl
Algorithm Hash digest
SHA256 8bf3cb93b7c50af4a84d52d69c325b312f544cdac396d62f1898325aac872fd8
MD5 38111d97aad6b57650d45e15d9c1c5b2
BLAKE2b-256 cace090fa6eb9ec69672e76082b6e4e15d9a3ac367162fca8517d0027766c07f

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 89a1d632c9e21052718c17603a33cc0d839a2670f0bf8ecd718bae3eeb36822f
MD5 c00f0b3a58dbbec8f534f80f8759325f
BLAKE2b-256 5176bb953d1d795b95bb3fed6a1848aeb59970d28daecf1458e7992fd846c7c0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.2-cp310-none-win_amd64.whl
Algorithm Hash digest
SHA256 4583e5852046cd736daac621f50f37c117ecafbe2027f985d5ecd3a774df77ad
MD5 1223654a5a171c7ef6f5f6662e7a8db0
BLAKE2b-256 8ad3461c929220966e751ca7546010f86cfdb0343dc1fb5dc56acad083189610

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 67aab63d292a42ec6de09cd4945f814781a91db792bf178386c2843fde9719cd
MD5 bf853302a8ef79445be34fbe3f0da943
BLAKE2b-256 8ac3ea6ad9dd3de27b1509ce5f236210b5c9d7a1deff0f44f644264e55a8cde2

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.2-cp39-none-win_amd64.whl
Algorithm Hash digest
SHA256 1d0dceebe065d1ab8de50f401b0308850d1f4e6038f43b76e5fcd783d97d5c8e
MD5 023bfbb9fcdd86f9f459991007ada2e8
BLAKE2b-256 b4f997476c3d5fc06c8a2286e914c20c33a8f72b8d9786501a26e09e8d0944de

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 e10a1acc5371c7c83a1e5c18f48598d5334c1d834d0b8aedca4c1fb81bf4ace4
MD5 a6978cde5b6f01375efe7c2c8ecc9842
BLAKE2b-256 e94cbac927e0b6facc95ec742f4d7ce79aaa0d7da4ab1222a6acd5992ddb3391

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.2-cp38-none-win_amd64.whl
Algorithm Hash digest
SHA256 e2863d686e95348a5650b3bb070c18be738d8c2fdb4d070df383aba03005d4b4
MD5 de1f4bcfe5a8872a038255b6d113b79a
BLAKE2b-256 0cb695643c51b27e0be6aeb7219041713642e80cba8a1438234c5f835d2475c4

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for lab_1806_vec_db-0.3.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 217f1831bf991524c5cc4678e0facc7461842e626fd9af678ce6dcca9886fc5a
MD5 d1f7e89d2976f915f6b7e48650296f2f
BLAKE2b-256 03d4a9dafaa7410376a49b7534c50588b82fa7eb8989fe485d4028d3deb07268

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