fromscratch-pyvecdb 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fromscratch_pyvecdb-0.3.0/.github/workflows/ci.yml +14 -0
- fromscratch_pyvecdb-0.3.0/.github/workflows/docs.yml +16 -0
- fromscratch_pyvecdb-0.3.0/.github/workflows/release.yml +58 -0
- fromscratch_pyvecdb-0.3.0/.gitignore +11 -0
- fromscratch_pyvecdb-0.3.0/.pre-commit-config.yaml +56 -0
- fromscratch_pyvecdb-0.3.0/CLAUDE.md +52 -0
- fromscratch_pyvecdb-0.3.0/LICENSE +21 -0
- fromscratch_pyvecdb-0.3.0/PKG-INFO +147 -0
- fromscratch_pyvecdb-0.3.0/README.md +115 -0
- fromscratch_pyvecdb-0.3.0/conftest.py +21 -0
- fromscratch_pyvecdb-0.3.0/docs/concepts/distance.md +66 -0
- fromscratch_pyvecdb-0.3.0/docs/concepts/metadata.md +108 -0
- fromscratch_pyvecdb-0.3.0/docs/concepts/saving-safely.md +25 -0
- fromscratch_pyvecdb-0.3.0/docs/concepts/search.md +68 -0
- fromscratch_pyvecdb-0.3.0/docs/concepts/similarity.md +48 -0
- fromscratch_pyvecdb-0.3.0/docs/concepts/vectors.md +62 -0
- fromscratch_pyvecdb-0.3.0/docs/index.md +53 -0
- fromscratch_pyvecdb-0.3.0/docs/property-testing.md +377 -0
- fromscratch_pyvecdb-0.3.0/mkdocs.yml +81 -0
- fromscratch_pyvecdb-0.3.0/overrides/main.html +7 -0
- fromscratch_pyvecdb-0.3.0/pyproject.toml +156 -0
- fromscratch_pyvecdb-0.3.0/src/pyvecdb/__init__.py +68 -0
- fromscratch_pyvecdb-0.3.0/src/pyvecdb/_version.py +24 -0
- fromscratch_pyvecdb-0.3.0/src/pyvecdb/atomic.py +66 -0
- fromscratch_pyvecdb-0.3.0/src/pyvecdb/distance.py +138 -0
- fromscratch_pyvecdb-0.3.0/src/pyvecdb/errors.py +61 -0
- fromscratch_pyvecdb-0.3.0/src/pyvecdb/filtering.py +128 -0
- fromscratch_pyvecdb-0.3.0/src/pyvecdb/persistence.py +149 -0
- fromscratch_pyvecdb-0.3.0/src/pyvecdb/py.typed +0 -0
- fromscratch_pyvecdb-0.3.0/src/pyvecdb/record.py +94 -0
- fromscratch_pyvecdb-0.3.0/src/pyvecdb/search.py +165 -0
- fromscratch_pyvecdb-0.3.0/src/pyvecdb/store.py +250 -0
- fromscratch_pyvecdb-0.3.0/src/pyvecdb/vector.py +180 -0
- fromscratch_pyvecdb-0.3.0/tests/__init__.py +1 -0
- fromscratch_pyvecdb-0.3.0/tests/strategies.py +79 -0
- fromscratch_pyvecdb-0.3.0/tests/test_atomic.py +75 -0
- fromscratch_pyvecdb-0.3.0/tests/test_distance.py +149 -0
- fromscratch_pyvecdb-0.3.0/tests/test_filtering.py +143 -0
- fromscratch_pyvecdb-0.3.0/tests/test_model.py +294 -0
- fromscratch_pyvecdb-0.3.0/tests/test_persistence.py +235 -0
- fromscratch_pyvecdb-0.3.0/tests/test_properties.py +442 -0
- fromscratch_pyvecdb-0.3.0/tests/test_record.py +104 -0
- fromscratch_pyvecdb-0.3.0/tests/test_search.py +177 -0
- fromscratch_pyvecdb-0.3.0/tests/test_store.py +324 -0
- fromscratch_pyvecdb-0.3.0/tests/test_vector.py +161 -0
- fromscratch_pyvecdb-0.3.0/uv.lock +920 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
name: Deploy Docs
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [main]
|
|
6
|
+
workflow_dispatch: {}
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
docs:
|
|
10
|
+
uses: robcox2910/robcox2910.github.io/.github/workflows/docs.yml@v1
|
|
11
|
+
permissions:
|
|
12
|
+
contents: read
|
|
13
|
+
pages: write
|
|
14
|
+
id-token: write
|
|
15
|
+
with:
|
|
16
|
+
coverage-report: false
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
name: Release
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags:
|
|
6
|
+
- "v*.*.*"
|
|
7
|
+
|
|
8
|
+
permissions:
|
|
9
|
+
contents: write
|
|
10
|
+
id-token: write
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build:
|
|
14
|
+
runs-on: ubuntu-latest
|
|
15
|
+
steps:
|
|
16
|
+
- uses: actions/checkout@v7
|
|
17
|
+
with:
|
|
18
|
+
fetch-depth: 0
|
|
19
|
+
|
|
20
|
+
- name: Install uv
|
|
21
|
+
uses: astral-sh/setup-uv@v7
|
|
22
|
+
with:
|
|
23
|
+
version: "latest"
|
|
24
|
+
|
|
25
|
+
- name: Set up Python
|
|
26
|
+
run: uv python install 3.14
|
|
27
|
+
|
|
28
|
+
- name: Build distributions
|
|
29
|
+
run: uv build
|
|
30
|
+
|
|
31
|
+
- name: Upload build artifacts
|
|
32
|
+
uses: actions/upload-artifact@v7
|
|
33
|
+
with:
|
|
34
|
+
name: dist
|
|
35
|
+
path: dist/
|
|
36
|
+
|
|
37
|
+
- name: Upload to GitHub Release
|
|
38
|
+
uses: softprops/action-gh-release@v3
|
|
39
|
+
with:
|
|
40
|
+
files: dist/*
|
|
41
|
+
|
|
42
|
+
publish:
|
|
43
|
+
needs: build
|
|
44
|
+
runs-on: ubuntu-latest
|
|
45
|
+
environment:
|
|
46
|
+
name: pypi
|
|
47
|
+
url: https://pypi.org/project/fromscratch-pyvecdb/
|
|
48
|
+
permissions:
|
|
49
|
+
id-token: write
|
|
50
|
+
steps:
|
|
51
|
+
- name: Download build artifacts
|
|
52
|
+
uses: actions/download-artifact@v8
|
|
53
|
+
with:
|
|
54
|
+
name: dist
|
|
55
|
+
path: dist/
|
|
56
|
+
|
|
57
|
+
- name: Publish to PyPI
|
|
58
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
default_language_version:
|
|
2
|
+
python: python3.14
|
|
3
|
+
|
|
4
|
+
repos:
|
|
5
|
+
- repo: https://github.com/pre-commit/pre-commit-hooks
|
|
6
|
+
rev: v5.0.0
|
|
7
|
+
hooks:
|
|
8
|
+
# Pages paste real pytest output, trailing spaces included.
|
|
9
|
+
- id: trailing-whitespace
|
|
10
|
+
exclude: ^docs/property-testing\.md$
|
|
11
|
+
- id: end-of-file-fixer
|
|
12
|
+
exclude: ^docs/property-testing\.md$
|
|
13
|
+
- id: check-yaml
|
|
14
|
+
args: ['--unsafe']
|
|
15
|
+
- id: check-added-large-files
|
|
16
|
+
- id: check-json
|
|
17
|
+
- id: check-toml
|
|
18
|
+
- id: check-merge-conflict
|
|
19
|
+
- id: debug-statements
|
|
20
|
+
|
|
21
|
+
- repo: https://github.com/astral-sh/ruff-pre-commit
|
|
22
|
+
rev: v0.15.4
|
|
23
|
+
hooks:
|
|
24
|
+
- id: ruff
|
|
25
|
+
args: [--fix, --exit-non-zero-on-fix]
|
|
26
|
+
- id: ruff-format
|
|
27
|
+
|
|
28
|
+
- repo: https://github.com/RobertCraigie/pyright-python
|
|
29
|
+
rev: v1.1.408
|
|
30
|
+
hooks:
|
|
31
|
+
- id: pyright
|
|
32
|
+
|
|
33
|
+
- repo: local
|
|
34
|
+
hooks:
|
|
35
|
+
- id: pip-audit
|
|
36
|
+
name: pip-audit
|
|
37
|
+
language: system
|
|
38
|
+
pass_filenames: false
|
|
39
|
+
files: ^(pyproject\.toml|uv\.lock)$
|
|
40
|
+
entry: >-
|
|
41
|
+
bash -c 'f=$(mktemp) &&
|
|
42
|
+
uv export --frozen --no-emit-project --all-extras --no-hashes -o "$f" >/dev/null &&
|
|
43
|
+
uvx pip-audit==2.10.0 -r "$f" --disable-pip --no-deps;
|
|
44
|
+
status=$?; rm -f "$f"; exit $status'
|
|
45
|
+
|
|
46
|
+
- repo: https://github.com/rhysd/actionlint
|
|
47
|
+
rev: v1.7.7
|
|
48
|
+
hooks:
|
|
49
|
+
- id: actionlint
|
|
50
|
+
|
|
51
|
+
- repo: https://github.com/commitizen-tools/commitizen
|
|
52
|
+
rev: v4.13.8
|
|
53
|
+
hooks:
|
|
54
|
+
- id: commitizen
|
|
55
|
+
stages: [commit-msg]
|
|
56
|
+
additional_dependencies: [typing_extensions]
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# Claude Code Instructions
|
|
2
|
+
|
|
3
|
+
<!-- standard:start -->
|
|
4
|
+
## Code Style
|
|
5
|
+
|
|
6
|
+
- All public functions, methods, classes, and modules **must** have a docstring
|
|
7
|
+
- Use imperative mood: "Return the result." not "Returns the result."
|
|
8
|
+
- One-line docstrings for simple functions; multi-line with summary + details for complex ones
|
|
9
|
+
- Follow the Google docstring convention (D211, D212 selected via ruff)
|
|
10
|
+
|
|
11
|
+
## Linting
|
|
12
|
+
|
|
13
|
+
- Ruff enforces a comprehensive rule set -- run `uv run ruff check .` before committing
|
|
14
|
+
- Pyright runs in strict mode -- run `uv run pyright src tests` before committing
|
|
15
|
+
- All violations must be fixed, not ignored. Exceptions are the repo-owned `ignore` and `per-file-ignores` lists in `pyproject.toml`, which belong to this repo; the series sync never edits them
|
|
16
|
+
|
|
17
|
+
## Testing
|
|
18
|
+
|
|
19
|
+
- TDD workflow: Red-Green-Refactor
|
|
20
|
+
- Coverage is enforced by pytest-cov; the floor in `pyproject.toml` is repo-owned
|
|
21
|
+
- Test classes and methods also require docstrings
|
|
22
|
+
- Use named constants instead of magic values (PLR2004)
|
|
23
|
+
- Run `uv run pytest` before committing
|
|
24
|
+
- Property-based tests (Hypothesis) live in `tests/test_properties.py` and `tests/test_model.py`; the shared profiles are in the root `conftest.py` (owned by `series sync`). Run `uv run pytest --hypothesis-profile=thorough` before a release.
|
|
25
|
+
|
|
26
|
+
## Commits
|
|
27
|
+
|
|
28
|
+
- Use conventional commits: `feat:`, `fix:`, `refactor:`, `test:`, `docs:`, `ci:`, `chore:`
|
|
29
|
+
- All changes go via PR from a feature branch (main is protected); merge when CI is green
|
|
30
|
+
|
|
31
|
+
## Dependencies
|
|
32
|
+
|
|
33
|
+
- Package manager: uv
|
|
34
|
+
- Run `uv sync --all-extras` after modifying dependencies
|
|
35
|
+
- `uv.lock` is committed; CI and pre-commit audit it with pip-audit through `uvx`, so pip-audit is not a project dependency
|
|
36
|
+
- Sibling projects, where used, are pinned release tags in `[tool.uv.sources]` (git + tag); never use `path` sources
|
|
37
|
+
|
|
38
|
+
## Releasing
|
|
39
|
+
|
|
40
|
+
- A release is a `vX.Y.Z` tag on main after the PR is merged
|
|
41
|
+
- Docs-only and tooling-only changes do not get a release tag
|
|
42
|
+
- Docs are built with `mkdocs build --strict` and deployed to GitHub Pages by the docs workflow
|
|
43
|
+
<!-- standard:end -->
|
|
44
|
+
|
|
45
|
+
## Project notes
|
|
46
|
+
|
|
47
|
+
- Pyright in strict mode -- run `uv run pyright src tests` before committing
|
|
48
|
+
- All violations must be fixed, not ignored (except D203/D213 incompatible pair)
|
|
49
|
+
- Minimum 90% coverage enforced by pytest-cov
|
|
50
|
+
- Use conventional commits (enforced by commitizen): `feat:`, `fix:`, `refactor:`, `test:`, `docs:`, `ci:`, `chore:`
|
|
51
|
+
- All changes go via PR from a feature branch (main is protected)
|
|
52
|
+
- Add production deps to `[project.dependencies]`
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Rob Cox
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: fromscratch-pyvecdb
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: An educational vector database library built from scratch in Python
|
|
5
|
+
Project-URL: Homepage, https://github.com/robcox2910/pyvecdb
|
|
6
|
+
Project-URL: Repository, https://github.com/robcox2910/pyvecdb
|
|
7
|
+
Project-URL: Issues, https://github.com/robcox2910/pyvecdb/issues
|
|
8
|
+
Project-URL: Documentation, https://robcox2910.github.io/pyvecdb/
|
|
9
|
+
Author: Rob Cox
|
|
10
|
+
License: MIT
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: database,education,search,similarity,vector
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Education
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
18
|
+
Classifier: Topic :: Education
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.14
|
|
22
|
+
Provides-Extra: dev
|
|
23
|
+
Requires-Dist: commitizen>=4.4.0; extra == 'dev'
|
|
24
|
+
Requires-Dist: hypothesis>=6.170; extra == 'dev'
|
|
25
|
+
Requires-Dist: pip-audit>=2.10.0; extra == 'dev'
|
|
26
|
+
Requires-Dist: pre-commit>=4.0.1; extra == 'dev'
|
|
27
|
+
Requires-Dist: pyright>=1.1.400; extra == 'dev'
|
|
28
|
+
Requires-Dist: pytest-cov>=6.0.0; extra == 'dev'
|
|
29
|
+
Requires-Dist: pytest>=8.3.4; extra == 'dev'
|
|
30
|
+
Requires-Dist: ruff>=0.9.3; extra == 'dev'
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# pyvecdb
|
|
34
|
+
|
|
35
|
+
An educational vector database library built from scratch in Python. Part of the **PyLearn** platform for kids aged 12+.
|
|
36
|
+
|
|
37
|
+
## What is a Vector Database?
|
|
38
|
+
|
|
39
|
+
A vector database stores **vectors** (lists of numbers) and finds **similar** ones. Think of it like a music app that turns songs into lists of numbers and finds other songs with similar numbers!
|
|
40
|
+
|
|
41
|
+
## Features
|
|
42
|
+
|
|
43
|
+
- **Vector Operations** -- add, subtract, scale, normalize, dot product, magnitude
|
|
44
|
+
- **Vector Store** -- CRUD operations for vector records with dimensionality checks
|
|
45
|
+
- **Distance Metrics** -- cosine similarity, Euclidean, Manhattan, dot product
|
|
46
|
+
- **KNN Search** -- find the K most similar vectors to a query
|
|
47
|
+
- **Metadata Filtering** -- filter results by metadata (eq, ne, gt, gte, lt, lte, in)
|
|
48
|
+
- **Batch Operations** -- insert and search multiple vectors at once
|
|
49
|
+
- **Persistence** -- save/load to JSON files
|
|
50
|
+
- **Pure Python** -- no external dependencies
|
|
51
|
+
|
|
52
|
+
## Quick Start
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from pyvecdb import VectorStore, knn_search
|
|
56
|
+
|
|
57
|
+
# Create a store for 3-dimensional vectors
|
|
58
|
+
store = VectorStore(dimensions=3)
|
|
59
|
+
|
|
60
|
+
# Insert some songs as vectors (energy, happiness, danceability)
|
|
61
|
+
store.insert("thunder", [0.9, 0.6, 0.7], metadata={"genre": "rock", "year": 2017})
|
|
62
|
+
store.insert("lullaby", [0.1, 0.8, 0.2], metadata={"genre": "folk", "year": 2020})
|
|
63
|
+
store.insert("party-rock", [0.95, 0.9, 0.99], metadata={"genre": "pop", "year": 2011})
|
|
64
|
+
store.insert("ballad", [0.3, 0.4, 0.1], metadata={"genre": "rock", "year": 2019})
|
|
65
|
+
|
|
66
|
+
# Find the 2 most similar songs to a high-energy query
|
|
67
|
+
results = knn_search(store, query=[0.85, 0.7, 0.8], k=2)
|
|
68
|
+
for record, score in results:
|
|
69
|
+
print(f"{record.record_id}: {score:.3f}")
|
|
70
|
+
# party-rock: 0.998
|
|
71
|
+
# thunder: 0.995
|
|
72
|
+
|
|
73
|
+
# Filter by metadata -- only rock songs
|
|
74
|
+
results = knn_search(
|
|
75
|
+
store,
|
|
76
|
+
query=[0.85, 0.7, 0.8],
|
|
77
|
+
k=2,
|
|
78
|
+
metadata_filter={"genre": "rock"},
|
|
79
|
+
)
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
## Installation
|
|
83
|
+
|
|
84
|
+
```bash
|
|
85
|
+
git clone https://github.com/robcox2910/pyvecdb.git
|
|
86
|
+
cd pyvecdb
|
|
87
|
+
uv sync --all-extras
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Development
|
|
91
|
+
|
|
92
|
+
```bash
|
|
93
|
+
uv run ruff check src/ tests/ # Lint
|
|
94
|
+
uv run ruff format src/ tests/ # Format
|
|
95
|
+
uv run pyright src tests # Type check
|
|
96
|
+
uv run pytest # Test (90%+ coverage)
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## Releasing
|
|
100
|
+
|
|
101
|
+
Other projects in the series ([PyStack](https://github.com/robcox2910/pystack)) use a pinned release of this one,
|
|
102
|
+
not whatever is on `main`. To publish a change for them:
|
|
103
|
+
|
|
104
|
+
1. Merge the change to `main`.
|
|
105
|
+
2. Tag it: `git tag -a vX.Y.Z -m vX.Y.Z && git push origin vX.Y.Z`.
|
|
106
|
+
A change to the public API bumps the minor version (`v0.1.0` → `v0.2.0`);
|
|
107
|
+
anything else bumps the patch (`v0.1.0` → `v0.1.1`).
|
|
108
|
+
3. In each project that uses it, edit the tag in `[tool.uv.sources]`, run
|
|
109
|
+
`uv lock --upgrade-package <name>`, and open a pull request so CI checks
|
|
110
|
+
the pair together.
|
|
111
|
+
|
|
112
|
+
<!-- series:start -->
|
|
113
|
+
## Related Projects
|
|
114
|
+
|
|
115
|
+
This project is one of a series that builds real systems "from scratch in Python" to teach how they work. See the [series home](https://robcox2910.github.io/) for the full story.
|
|
116
|
+
|
|
117
|
+
| Project | What it teaches | Repository | Docs |
|
|
118
|
+
| --- | --- | --- | --- |
|
|
119
|
+
| PyOS | Operating systems | [py-os](https://github.com/robcox2910/py-os) | [Docs](https://robcox2910.github.io/py-os/) |
|
|
120
|
+
| Pebble | Compilers and programming languages | [pebble-lang](https://github.com/robcox2910/pebble-lang) | [Docs](https://robcox2910.github.io/pebble-lang/) |
|
|
121
|
+
| PyDB | Relational databases | [pydb](https://github.com/robcox2910/pydb) | [Docs](https://robcox2910.github.io/pydb/) |
|
|
122
|
+
| PyKV | Key-value stores | [pykv](https://github.com/robcox2910/pykv) | [Docs](https://robcox2910.github.io/pykv/) |
|
|
123
|
+
| PyDocDB | Document databases | [pydoc-db](https://github.com/robcox2910/pydoc-db) | [Docs](https://robcox2910.github.io/pydoc-db/) |
|
|
124
|
+
| PyGraphDB | Graph databases | [pygraphdb](https://github.com/robcox2910/pygraphdb) | [Docs](https://robcox2910.github.io/pygraphdb/) |
|
|
125
|
+
| PyTSDB | Time-series databases | [pytsdb](https://github.com/robcox2910/pytsdb) | [Docs](https://robcox2910.github.io/pytsdb/) |
|
|
126
|
+
| **PyVecDB** | **Vector databases** | [pyvecdb](https://github.com/robcox2910/pyvecdb) | [Docs](https://robcox2910.github.io/pyvecdb/) |
|
|
127
|
+
| PyColDB | Column-family databases | [pycoldb](https://github.com/robcox2910/pycoldb) | [Docs](https://robcox2910.github.io/pycoldb/) |
|
|
128
|
+
| PyCrypt | Cryptography | [pycrypt](https://github.com/robcox2910/pycrypt) | [Docs](https://robcox2910.github.io/pycrypt/) |
|
|
129
|
+
| PyNet | Computer networking | [pynet](https://github.com/robcox2910/pynet) | [Docs](https://robcox2910.github.io/pynet/) |
|
|
130
|
+
| PyWeb | HTTP web servers | [pyweb](https://github.com/robcox2910/pyweb) | [Docs](https://robcox2910.github.io/pyweb/) |
|
|
131
|
+
| PyGit | Version control | [pygit](https://github.com/robcox2910/pygit) | [Docs](https://robcox2910.github.io/pygit/) |
|
|
132
|
+
| PySearch | Search engines | [pysearch](https://github.com/robcox2910/pysearch) | [Docs](https://robcox2910.github.io/pysearch/) |
|
|
133
|
+
| PyMQ | Message queues | [pymq](https://github.com/robcox2910/pymq) | [Docs](https://robcox2910.github.io/pymq/) |
|
|
134
|
+
| PyLLM | Language models (the brain) | [pyllm](https://github.com/robcox2910/pyllm) | [Docs](https://robcox2910.github.io/pyllm/) |
|
|
135
|
+
| PyScheduler | Job scheduling | [pyscheduler](https://github.com/robcox2910/pyscheduler) | [Docs](https://robcox2910.github.io/pyscheduler/) |
|
|
136
|
+
| PyStack | Full-stack integration | [pystack](https://github.com/robcox2910/pystack) | [Docs](https://robcox2910.github.io/pystack/) |
|
|
137
|
+
<!-- series:end -->
|
|
138
|
+
|
|
139
|
+
<!-- docs:start -->
|
|
140
|
+
## Documentation
|
|
141
|
+
|
|
142
|
+
Full docs (with kid-friendly explanations of every idea): https://robcox2910.github.io/pyvecdb/
|
|
143
|
+
<!-- docs:end -->
|
|
144
|
+
|
|
145
|
+
## License
|
|
146
|
+
|
|
147
|
+
MIT
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
# pyvecdb
|
|
2
|
+
|
|
3
|
+
An educational vector database library built from scratch in Python. Part of the **PyLearn** platform for kids aged 12+.
|
|
4
|
+
|
|
5
|
+
## What is a Vector Database?
|
|
6
|
+
|
|
7
|
+
A vector database stores **vectors** (lists of numbers) and finds **similar** ones. Think of it like a music app that turns songs into lists of numbers and finds other songs with similar numbers!
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- **Vector Operations** -- add, subtract, scale, normalize, dot product, magnitude
|
|
12
|
+
- **Vector Store** -- CRUD operations for vector records with dimensionality checks
|
|
13
|
+
- **Distance Metrics** -- cosine similarity, Euclidean, Manhattan, dot product
|
|
14
|
+
- **KNN Search** -- find the K most similar vectors to a query
|
|
15
|
+
- **Metadata Filtering** -- filter results by metadata (eq, ne, gt, gte, lt, lte, in)
|
|
16
|
+
- **Batch Operations** -- insert and search multiple vectors at once
|
|
17
|
+
- **Persistence** -- save/load to JSON files
|
|
18
|
+
- **Pure Python** -- no external dependencies
|
|
19
|
+
|
|
20
|
+
## Quick Start
|
|
21
|
+
|
|
22
|
+
```python
|
|
23
|
+
from pyvecdb import VectorStore, knn_search
|
|
24
|
+
|
|
25
|
+
# Create a store for 3-dimensional vectors
|
|
26
|
+
store = VectorStore(dimensions=3)
|
|
27
|
+
|
|
28
|
+
# Insert some songs as vectors (energy, happiness, danceability)
|
|
29
|
+
store.insert("thunder", [0.9, 0.6, 0.7], metadata={"genre": "rock", "year": 2017})
|
|
30
|
+
store.insert("lullaby", [0.1, 0.8, 0.2], metadata={"genre": "folk", "year": 2020})
|
|
31
|
+
store.insert("party-rock", [0.95, 0.9, 0.99], metadata={"genre": "pop", "year": 2011})
|
|
32
|
+
store.insert("ballad", [0.3, 0.4, 0.1], metadata={"genre": "rock", "year": 2019})
|
|
33
|
+
|
|
34
|
+
# Find the 2 most similar songs to a high-energy query
|
|
35
|
+
results = knn_search(store, query=[0.85, 0.7, 0.8], k=2)
|
|
36
|
+
for record, score in results:
|
|
37
|
+
print(f"{record.record_id}: {score:.3f}")
|
|
38
|
+
# party-rock: 0.998
|
|
39
|
+
# thunder: 0.995
|
|
40
|
+
|
|
41
|
+
# Filter by metadata -- only rock songs
|
|
42
|
+
results = knn_search(
|
|
43
|
+
store,
|
|
44
|
+
query=[0.85, 0.7, 0.8],
|
|
45
|
+
k=2,
|
|
46
|
+
metadata_filter={"genre": "rock"},
|
|
47
|
+
)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Installation
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
git clone https://github.com/robcox2910/pyvecdb.git
|
|
54
|
+
cd pyvecdb
|
|
55
|
+
uv sync --all-extras
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
## Development
|
|
59
|
+
|
|
60
|
+
```bash
|
|
61
|
+
uv run ruff check src/ tests/ # Lint
|
|
62
|
+
uv run ruff format src/ tests/ # Format
|
|
63
|
+
uv run pyright src tests # Type check
|
|
64
|
+
uv run pytest # Test (90%+ coverage)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## Releasing
|
|
68
|
+
|
|
69
|
+
Other projects in the series ([PyStack](https://github.com/robcox2910/pystack)) use a pinned release of this one,
|
|
70
|
+
not whatever is on `main`. To publish a change for them:
|
|
71
|
+
|
|
72
|
+
1. Merge the change to `main`.
|
|
73
|
+
2. Tag it: `git tag -a vX.Y.Z -m vX.Y.Z && git push origin vX.Y.Z`.
|
|
74
|
+
A change to the public API bumps the minor version (`v0.1.0` → `v0.2.0`);
|
|
75
|
+
anything else bumps the patch (`v0.1.0` → `v0.1.1`).
|
|
76
|
+
3. In each project that uses it, edit the tag in `[tool.uv.sources]`, run
|
|
77
|
+
`uv lock --upgrade-package <name>`, and open a pull request so CI checks
|
|
78
|
+
the pair together.
|
|
79
|
+
|
|
80
|
+
<!-- series:start -->
|
|
81
|
+
## Related Projects
|
|
82
|
+
|
|
83
|
+
This project is one of a series that builds real systems "from scratch in Python" to teach how they work. See the [series home](https://robcox2910.github.io/) for the full story.
|
|
84
|
+
|
|
85
|
+
| Project | What it teaches | Repository | Docs |
|
|
86
|
+
| --- | --- | --- | --- |
|
|
87
|
+
| PyOS | Operating systems | [py-os](https://github.com/robcox2910/py-os) | [Docs](https://robcox2910.github.io/py-os/) |
|
|
88
|
+
| Pebble | Compilers and programming languages | [pebble-lang](https://github.com/robcox2910/pebble-lang) | [Docs](https://robcox2910.github.io/pebble-lang/) |
|
|
89
|
+
| PyDB | Relational databases | [pydb](https://github.com/robcox2910/pydb) | [Docs](https://robcox2910.github.io/pydb/) |
|
|
90
|
+
| PyKV | Key-value stores | [pykv](https://github.com/robcox2910/pykv) | [Docs](https://robcox2910.github.io/pykv/) |
|
|
91
|
+
| PyDocDB | Document databases | [pydoc-db](https://github.com/robcox2910/pydoc-db) | [Docs](https://robcox2910.github.io/pydoc-db/) |
|
|
92
|
+
| PyGraphDB | Graph databases | [pygraphdb](https://github.com/robcox2910/pygraphdb) | [Docs](https://robcox2910.github.io/pygraphdb/) |
|
|
93
|
+
| PyTSDB | Time-series databases | [pytsdb](https://github.com/robcox2910/pytsdb) | [Docs](https://robcox2910.github.io/pytsdb/) |
|
|
94
|
+
| **PyVecDB** | **Vector databases** | [pyvecdb](https://github.com/robcox2910/pyvecdb) | [Docs](https://robcox2910.github.io/pyvecdb/) |
|
|
95
|
+
| PyColDB | Column-family databases | [pycoldb](https://github.com/robcox2910/pycoldb) | [Docs](https://robcox2910.github.io/pycoldb/) |
|
|
96
|
+
| PyCrypt | Cryptography | [pycrypt](https://github.com/robcox2910/pycrypt) | [Docs](https://robcox2910.github.io/pycrypt/) |
|
|
97
|
+
| PyNet | Computer networking | [pynet](https://github.com/robcox2910/pynet) | [Docs](https://robcox2910.github.io/pynet/) |
|
|
98
|
+
| PyWeb | HTTP web servers | [pyweb](https://github.com/robcox2910/pyweb) | [Docs](https://robcox2910.github.io/pyweb/) |
|
|
99
|
+
| PyGit | Version control | [pygit](https://github.com/robcox2910/pygit) | [Docs](https://robcox2910.github.io/pygit/) |
|
|
100
|
+
| PySearch | Search engines | [pysearch](https://github.com/robcox2910/pysearch) | [Docs](https://robcox2910.github.io/pysearch/) |
|
|
101
|
+
| PyMQ | Message queues | [pymq](https://github.com/robcox2910/pymq) | [Docs](https://robcox2910.github.io/pymq/) |
|
|
102
|
+
| PyLLM | Language models (the brain) | [pyllm](https://github.com/robcox2910/pyllm) | [Docs](https://robcox2910.github.io/pyllm/) |
|
|
103
|
+
| PyScheduler | Job scheduling | [pyscheduler](https://github.com/robcox2910/pyscheduler) | [Docs](https://robcox2910.github.io/pyscheduler/) |
|
|
104
|
+
| PyStack | Full-stack integration | [pystack](https://github.com/robcox2910/pystack) | [Docs](https://robcox2910.github.io/pystack/) |
|
|
105
|
+
<!-- series:end -->
|
|
106
|
+
|
|
107
|
+
<!-- docs:start -->
|
|
108
|
+
## Documentation
|
|
109
|
+
|
|
110
|
+
Full docs (with kid-friendly explanations of every idea): https://robcox2910.github.io/pyvecdb/
|
|
111
|
+
<!-- docs:end -->
|
|
112
|
+
|
|
113
|
+
## License
|
|
114
|
+
|
|
115
|
+
MIT
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""Shared Hypothesis profiles for the series.
|
|
2
|
+
|
|
3
|
+
Two profiles are registered, and ``ci`` is loaded by default:
|
|
4
|
+
|
|
5
|
+
- ``ci`` runs 100 examples with no deadline, a fixed seed and no example
|
|
6
|
+
database, so the same code gives the same result on every machine and a
|
|
7
|
+
green build can be trusted.
|
|
8
|
+
- ``thorough`` runs 2000 examples with a random seed, to hunt for the rare
|
|
9
|
+
counterexample before a release.
|
|
10
|
+
|
|
11
|
+
Run the long one with ``uv run pytest --hypothesis-profile=thorough``.
|
|
12
|
+
|
|
13
|
+
This file is owned by ``series sync``. Edit it in the series home repo
|
|
14
|
+
(robcox2910.github.io), not here, or the next sync will put it back.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
from hypothesis import settings
|
|
18
|
+
|
|
19
|
+
settings.register_profile("ci", max_examples=100, deadline=None, derandomize=True, database=None)
|
|
20
|
+
settings.register_profile("thorough", max_examples=2000, deadline=None, derandomize=False)
|
|
21
|
+
settings.load_profile("ci")
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# Distance: How Far Apart Are Two Spots?
|
|
2
|
+
|
|
3
|
+
## The Big Idea
|
|
4
|
+
|
|
5
|
+
If vectors are points on a map, **distance** tells you how far apart two points are. There are different ways to measure "how far" -- just like there are different ways to get across town!
|
|
6
|
+
|
|
7
|
+
## Four Ways to Measure Distance
|
|
8
|
+
|
|
9
|
+
### 1. Euclidean Distance (Straight Line)
|
|
10
|
+
|
|
11
|
+
This is the "as the crow flies" distance -- a straight line between two points. It uses the Pythagorean theorem extended to any number of dimensions.
|
|
12
|
+
|
|
13
|
+
```python
|
|
14
|
+
from pyvecdb import euclidean_distance
|
|
15
|
+
euclidean_distance([0.0, 0.0], [3.0, 4.0]) # 5.0
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
**When to use**: When you care about the actual magnitude of differences.
|
|
19
|
+
|
|
20
|
+
### 2. Cosine Similarity (Direction Only)
|
|
21
|
+
|
|
22
|
+
Cosine similarity ignores how long the vectors are and only checks if they point the same direction. It returns a number from -1 to 1:
|
|
23
|
+
- **1.0** = pointing the same way (identical taste!)
|
|
24
|
+
- **0.0** = perpendicular (completely unrelated)
|
|
25
|
+
- **-1.0** = pointing opposite ways (opposite taste!)
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from pyvecdb import cosine_similarity
|
|
29
|
+
cosine_similarity([1.0, 0.0], [1.0, 0.0]) # 1.0 (same!)
|
|
30
|
+
cosine_similarity([1.0, 0.0], [0.0, 1.0]) # 0.0 (unrelated)
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
**When to use**: Most popular for AI embeddings. This is the default in pyvecdb!
|
|
34
|
+
|
|
35
|
+
### 3. Manhattan Distance (City Blocks)
|
|
36
|
+
|
|
37
|
+
Imagine walking through a city grid -- you can only go along streets, not diagonally through buildings. Manhattan distance counts the total blocks walked.
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
from pyvecdb import manhattan_distance
|
|
41
|
+
manhattan_distance([0.0, 0.0], [3.0, 4.0]) # 7.0 (3 + 4 blocks)
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
**When to use**: When dimensions are independent (like different categories).
|
|
45
|
+
|
|
46
|
+
### 4. Dot Product (Agreement Score)
|
|
47
|
+
|
|
48
|
+
The dot product multiplies matching components and adds them up. Bigger numbers mean more "agreement."
|
|
49
|
+
|
|
50
|
+
```python
|
|
51
|
+
from pyvecdb import dot_product
|
|
52
|
+
dot_product([1.0, 2.0, 3.0], [4.0, 5.0, 6.0]) # 32.0
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
**When to use**: When vector length matters (not just direction).
|
|
56
|
+
|
|
57
|
+
## Which One Should I Use?
|
|
58
|
+
|
|
59
|
+
| Metric | Best For | Range |
|
|
60
|
+
|--------|----------|-------|
|
|
61
|
+
| Cosine | AI embeddings, text | -1 to 1 |
|
|
62
|
+
| Euclidean | Physical distance | 0 to infinity |
|
|
63
|
+
| Manhattan | Grid-like data | 0 to infinity |
|
|
64
|
+
| Dot Product | When magnitude matters | -infinity to infinity |
|
|
65
|
+
|
|
66
|
+
For most AI applications, **cosine similarity** is the go-to choice!
|