numpy-vector-store 0.1.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.
- numpy_vector_store-0.1.0/.github/FUNDING.yml +3 -0
- numpy_vector_store-0.1.0/.github/workflows/publish-pypi.yml +35 -0
- numpy_vector_store-0.1.0/.github/workflows/publish-testpypi.yml +35 -0
- numpy_vector_store-0.1.0/.gitignore +207 -0
- numpy_vector_store-0.1.0/LICENSE +21 -0
- numpy_vector_store-0.1.0/PKG-INFO +205 -0
- numpy_vector_store-0.1.0/README.md +182 -0
- numpy_vector_store-0.1.0/justfile +4 -0
- numpy_vector_store-0.1.0/pyproject.toml +82 -0
- numpy_vector_store-0.1.0/src/numpy_vector_store/__init__.py +13 -0
- numpy_vector_store-0.1.0/src/numpy_vector_store/vector_store.py +253 -0
- numpy_vector_store-0.1.0/tests/__init__.py +1 -0
- numpy_vector_store-0.1.0/tests/test_vector_store.py +549 -0
- numpy_vector_store-0.1.0/uv.lock +588 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types:
|
|
6
|
+
- published
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
publish-pypi:
|
|
10
|
+
name: Publish to PyPI
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
permissions:
|
|
13
|
+
contents: read
|
|
14
|
+
id-token: write
|
|
15
|
+
|
|
16
|
+
steps:
|
|
17
|
+
- name: Check out repository
|
|
18
|
+
uses: actions/checkout@v4
|
|
19
|
+
|
|
20
|
+
- name: Set up Python
|
|
21
|
+
uses: actions/setup-python@v5
|
|
22
|
+
with:
|
|
23
|
+
python-version: "3.12"
|
|
24
|
+
|
|
25
|
+
- name: Install build tooling
|
|
26
|
+
run: python -m pip install --upgrade build twine
|
|
27
|
+
|
|
28
|
+
- name: Build distributions
|
|
29
|
+
run: python -m build
|
|
30
|
+
|
|
31
|
+
- name: Check distribution metadata
|
|
32
|
+
run: python -m twine check dist/*
|
|
33
|
+
|
|
34
|
+
- name: Publish package distributions to PyPI
|
|
35
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
name: Publish to TestPyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
workflow_dispatch:
|
|
5
|
+
|
|
6
|
+
jobs:
|
|
7
|
+
publish-testpypi:
|
|
8
|
+
name: Publish to TestPyPI
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
permissions:
|
|
11
|
+
contents: read
|
|
12
|
+
id-token: write
|
|
13
|
+
|
|
14
|
+
steps:
|
|
15
|
+
- name: Check out repository
|
|
16
|
+
uses: actions/checkout@v4
|
|
17
|
+
|
|
18
|
+
- name: Set up Python
|
|
19
|
+
uses: actions/setup-python@v5
|
|
20
|
+
with:
|
|
21
|
+
python-version: "3.12"
|
|
22
|
+
|
|
23
|
+
- name: Install build tooling
|
|
24
|
+
run: python -m pip install --upgrade build twine
|
|
25
|
+
|
|
26
|
+
- name: Build distributions
|
|
27
|
+
run: python -m build
|
|
28
|
+
|
|
29
|
+
- name: Check distribution metadata
|
|
30
|
+
run: python -m twine check dist/*
|
|
31
|
+
|
|
32
|
+
- name: Publish package distributions to TestPyPI
|
|
33
|
+
uses: pypa/gh-action-pypi-publish@release/v1
|
|
34
|
+
with:
|
|
35
|
+
repository-url: https://test.pypi.org/legacy/
|
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
#Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
#uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
#poetry.lock
|
|
109
|
+
#poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
#pdm.lock
|
|
116
|
+
#pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
#pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# SageMath parsed files
|
|
135
|
+
*.sage.py
|
|
136
|
+
|
|
137
|
+
# Environments
|
|
138
|
+
.env
|
|
139
|
+
.envrc
|
|
140
|
+
.venv
|
|
141
|
+
env/
|
|
142
|
+
venv/
|
|
143
|
+
ENV/
|
|
144
|
+
env.bak/
|
|
145
|
+
venv.bak/
|
|
146
|
+
|
|
147
|
+
# Spyder project settings
|
|
148
|
+
.spyderproject
|
|
149
|
+
.spyproject
|
|
150
|
+
|
|
151
|
+
# Rope project settings
|
|
152
|
+
.ropeproject
|
|
153
|
+
|
|
154
|
+
# mkdocs documentation
|
|
155
|
+
/site
|
|
156
|
+
|
|
157
|
+
# mypy
|
|
158
|
+
.mypy_cache/
|
|
159
|
+
.dmypy.json
|
|
160
|
+
dmypy.json
|
|
161
|
+
|
|
162
|
+
# Pyre type checker
|
|
163
|
+
.pyre/
|
|
164
|
+
|
|
165
|
+
# pytype static type analyzer
|
|
166
|
+
.pytype/
|
|
167
|
+
|
|
168
|
+
# Cython debug symbols
|
|
169
|
+
cython_debug/
|
|
170
|
+
|
|
171
|
+
# PyCharm
|
|
172
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
173
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
174
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
175
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
176
|
+
#.idea/
|
|
177
|
+
|
|
178
|
+
# Abstra
|
|
179
|
+
# Abstra is an AI-powered process automation framework.
|
|
180
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
181
|
+
# Learn more at https://abstra.io/docs
|
|
182
|
+
.abstra/
|
|
183
|
+
|
|
184
|
+
# Visual Studio Code
|
|
185
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
186
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
188
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
189
|
+
# .vscode/
|
|
190
|
+
|
|
191
|
+
# Ruff stuff:
|
|
192
|
+
.ruff_cache/
|
|
193
|
+
|
|
194
|
+
# PyPI configuration file
|
|
195
|
+
.pypirc
|
|
196
|
+
|
|
197
|
+
# Cursor
|
|
198
|
+
# Cursor is an AI-powered code editor. `.cursorignore` specifies files/directories to
|
|
199
|
+
# exclude from AI features like autocomplete and code analysis. Recommended for sensitive data
|
|
200
|
+
# refer to https://docs.cursor.com/context/ignore-files
|
|
201
|
+
.cursorignore
|
|
202
|
+
.cursorindexingignore
|
|
203
|
+
|
|
204
|
+
# Marimo
|
|
205
|
+
marimo/_static/
|
|
206
|
+
marimo/_lsp/
|
|
207
|
+
__marimo__/
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Tim VanReenen
|
|
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,205 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: numpy-vector-store
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A fast, lightweight, and zero-setup in-memory vector store powered by NumPy
|
|
5
|
+
Project-URL: Homepage, https://github.com/tvanreenen/numpy-vector-store
|
|
6
|
+
Project-URL: Repository, https://github.com/tvanreenen/numpy-vector-store
|
|
7
|
+
Project-URL: Issues, https://github.com/tvanreenen/numpy-vector-store/issues
|
|
8
|
+
Author: Tim VanReenen
|
|
9
|
+
License: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: embeddings,numpy,search,store,vector
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Requires-Python: >=3.9
|
|
21
|
+
Requires-Dist: numpy>=1.20.0
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
|
|
24
|
+
# NumPy Vector Store
|
|
25
|
+
|
|
26
|
+
A fast, lightweight, and zero-setup in-memory vector store powered by NumPy.
|
|
27
|
+
|
|
28
|
+
- **High-performance** index-free O(n) cosine similarity searches
|
|
29
|
+
- **Vectorized** metadata queries with NumPy operations
|
|
30
|
+
- **Easy persistence** to and from compressed NumPy binary files (.npz)
|
|
31
|
+
- **Zero dependency** on external services or data stores
|
|
32
|
+
|
|
33
|
+
## Why?
|
|
34
|
+
|
|
35
|
+
This library is purpose-built for small to medium-scale vector search tasks and offers a simple, lightweight alternative to heavyweight solutions like Pinecone, Qdrant, Weaviate, Postgres + pgvector, or Azure AI Search—no complex setup or infrastructure required.
|
|
36
|
+
|
|
37
|
+
> Sometimes you don't need a sledgehammer to crack a nut.
|
|
38
|
+
|
|
39
|
+
## When/Where?
|
|
40
|
+
|
|
41
|
+
Below are benchmark results for the module's search method to help you assess its suitability for your use case.
|
|
42
|
+
|
|
43
|
+
| Embedding Type | Dimensions | ~5ms | ~25ms | ~100ms | ~500ms |
|
|
44
|
+
|----------------|------------|------|-------|--------|--------|
|
|
45
|
+
| **Sentence Transformers** | 384 | 1K vectors<br/>1.5MB | 10K vectors<br/>15MB | 100K vectors<br/>147MB | 500K vectors<br/>732MB |
|
|
46
|
+
| **OpenAI Small** | 1536 | 500 vectors<br/>3MB | 5K vectors<br/>29MB | 25K vectors<br/>147MB | 100K vectors<br/>586MB |
|
|
47
|
+
| **OpenAI Large** | 3072 | 200 vectors<br/>2MB | 2.5K vectors<br/>29MB | 5K vectors<br/>59MB | 25K vectors<br/>293MB |
|
|
48
|
+
|
|
49
|
+
*Benchmarks performed on Apple M2 hardware*
|
|
50
|
+
|
|
51
|
+
## Installation
|
|
52
|
+
|
|
53
|
+
**⚠️ Pending submission to PyPI**
|
|
54
|
+
|
|
55
|
+
```bash
|
|
56
|
+
uv add numpy-vector-store
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## Quick Start
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
import numpy as np
|
|
63
|
+
from numpy_vector_store import VectorStore
|
|
64
|
+
|
|
65
|
+
# Load your vector store
|
|
66
|
+
store = VectorStore(dimensions=1536, file_path='vectors.npz')
|
|
67
|
+
store.load()
|
|
68
|
+
|
|
69
|
+
# Embed your search query
|
|
70
|
+
query = np.array([0.2, 0.3, 0.4, ...])
|
|
71
|
+
|
|
72
|
+
# Search using cosine similarity
|
|
73
|
+
results = store.search(query, top_k=3)
|
|
74
|
+
|
|
75
|
+
# Compare the results
|
|
76
|
+
for index, similarity, meta in results:
|
|
77
|
+
print(f"{meta['title']}: {similarity:.3f}")
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
## Usage Examples
|
|
81
|
+
|
|
82
|
+
### Adding Vectors
|
|
83
|
+
|
|
84
|
+
#### Adding Vectors in Batch
|
|
85
|
+
|
|
86
|
+
The `add_vectors` method takes a **2D NumPy array** where each row is a vector, and a **1D NumPy array** of metadata objects.
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
# 2D np.array of vectors
|
|
90
|
+
embeddings = np.array([
|
|
91
|
+
[0.1, 0.2, 0.3, ...], # Text embedding 1
|
|
92
|
+
[0.4, 0.5, 0.6, ...], # Text embedding 2
|
|
93
|
+
[0.7, 0.8, 0.9, ...] # Text embedding 3
|
|
94
|
+
])
|
|
95
|
+
|
|
96
|
+
# 1D np.array of metadata objects
|
|
97
|
+
metadata = np.array([
|
|
98
|
+
{"title": "AI Overview", "word_count": 12},
|
|
99
|
+
{"title": "Python Guide", "word_count": 10},
|
|
100
|
+
{"title": "Vector DBs", "word_count": 8}
|
|
101
|
+
])
|
|
102
|
+
|
|
103
|
+
# Vectors and metadata added using efficient vectorized NumPy operations
|
|
104
|
+
store.add_vectors(embeddings, metadata)
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
#### Adding Vectors Individually
|
|
108
|
+
|
|
109
|
+
Batch operations are generally preferable and more efficient. But individual vectors can be added with the same method.
|
|
110
|
+
|
|
111
|
+
```python
|
|
112
|
+
store.add_vectors(
|
|
113
|
+
np.array([new_embedding]), # or np.atleast_2d(new_embedding)
|
|
114
|
+
np.array([{"title": "Neural Networks Paper", "word_count": 15}])
|
|
115
|
+
)
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Save the Vector Store
|
|
119
|
+
|
|
120
|
+
Save to a gzip compressed NumPy binary file directly
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
store.save()
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
Or use context manager for automatic persistence
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
with VectorStore(dimensions=3, file_path="vectors.npz") as store:
|
|
130
|
+
store.add_vectors(vectors_2d, metadata_array)
|
|
131
|
+
|
|
132
|
+
# Automatically saves when exiting the context
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
### Working with Metadata
|
|
136
|
+
|
|
137
|
+
Work with flexible, unstructured metadata using standard Python operations. No schema required - perfect for getting started quickly:
|
|
138
|
+
|
|
139
|
+
```python
|
|
140
|
+
# Access individual metadata
|
|
141
|
+
first_metadata = store.metadata[0]
|
|
142
|
+
print(f"First entry: {first_metadata}")
|
|
143
|
+
|
|
144
|
+
# Iterate through all metadata
|
|
145
|
+
for i, metadata in enumerate(store.metadata):
|
|
146
|
+
print(f"Entry {i}: {metadata}")
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
### Advanced: Structured Metadata for Performance
|
|
150
|
+
|
|
151
|
+
If you define a homogeneous NumPy schema upfront, you get **significant performance improvements** for metadata operations. Instead of Python loops and dictionary lookups, you get **vectorized NumPy operations** that are orders of magnitude faster.
|
|
152
|
+
|
|
153
|
+
> **Learn more**: [NumPy Structured Arrays Documentation](https://numpy.org/doc/stable/user/basics.rec.html)
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
# Define schema for performance
|
|
157
|
+
store = VectorStore(
|
|
158
|
+
dimensions=512,
|
|
159
|
+
metadata_schema={'title': 'U200', 'year': 'i4', 'citations': 'i4'}
|
|
160
|
+
)
|
|
161
|
+
store.add_vectors(
|
|
162
|
+
np.array([vector1, vector2]),
|
|
163
|
+
np.array([
|
|
164
|
+
{"title": "Paper 1", "year": 2023, "citations": 100},
|
|
165
|
+
{"title": "Paper 2", "year": 2022, "citations": 50}
|
|
166
|
+
])
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
# Perform vectorized NumPy operations on metadata
|
|
170
|
+
recent_mask = store.metadata['year'] == 2023
|
|
171
|
+
recent_vectors = np.array(store.vectors)[recent_mask]
|
|
172
|
+
|
|
173
|
+
# Sort by citations
|
|
174
|
+
sorted_indices = np.argsort(store.metadata['citations'])[::-1]
|
|
175
|
+
top_vectors = np.array(store.vectors)[sorted_indices]
|
|
176
|
+
|
|
177
|
+
# Complex filtering
|
|
178
|
+
high_impact = store.metadata['citations'] > 100
|
|
179
|
+
recent = store.metadata['year'] > 2020
|
|
180
|
+
combined_mask = high_impact & recent
|
|
181
|
+
filtered_vectors = np.array(store.vectors)[combined_mask]
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
## Contributing
|
|
185
|
+
|
|
186
|
+
### Setup Development Environment
|
|
187
|
+
|
|
188
|
+
```bash
|
|
189
|
+
git clone https://github.com/tvanreenen/numpy-vector-store.git
|
|
190
|
+
cd numpy-vector-store
|
|
191
|
+
uv sync --frozen --group dev
|
|
192
|
+
```
|
|
193
|
+
|
|
194
|
+
### Before Submitting a Pull Request
|
|
195
|
+
|
|
196
|
+
Please ensure:
|
|
197
|
+
|
|
198
|
+
1. **Code Quality**: Run `uv run ruff check` - should show no issues
|
|
199
|
+
2. **Formatting**: Run `uv run ruff format` - should show "files left unchanged"
|
|
200
|
+
3. **Type Checking**: Run `uv run mypy src/` - should show no errors
|
|
201
|
+
4. **Tests**: Run `uv run pytest` - all tests should pass
|
|
202
|
+
|
|
203
|
+
## License
|
|
204
|
+
|
|
205
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
# NumPy Vector Store
|
|
2
|
+
|
|
3
|
+
A fast, lightweight, and zero-setup in-memory vector store powered by NumPy.
|
|
4
|
+
|
|
5
|
+
- **High-performance** index-free O(n) cosine similarity searches
|
|
6
|
+
- **Vectorized** metadata queries with NumPy operations
|
|
7
|
+
- **Easy persistence** to and from compressed NumPy binary files (.npz)
|
|
8
|
+
- **Zero dependency** on external services or data stores
|
|
9
|
+
|
|
10
|
+
## Why?
|
|
11
|
+
|
|
12
|
+
This library is purpose-built for small to medium-scale vector search tasks and offers a simple, lightweight alternative to heavyweight solutions like Pinecone, Qdrant, Weaviate, Postgres + pgvector, or Azure AI Search—no complex setup or infrastructure required.
|
|
13
|
+
|
|
14
|
+
> Sometimes you don't need a sledgehammer to crack a nut.
|
|
15
|
+
|
|
16
|
+
## When/Where?
|
|
17
|
+
|
|
18
|
+
Below are benchmark results for the module's search method to help you assess its suitability for your use case.
|
|
19
|
+
|
|
20
|
+
| Embedding Type | Dimensions | ~5ms | ~25ms | ~100ms | ~500ms |
|
|
21
|
+
|----------------|------------|------|-------|--------|--------|
|
|
22
|
+
| **Sentence Transformers** | 384 | 1K vectors<br/>1.5MB | 10K vectors<br/>15MB | 100K vectors<br/>147MB | 500K vectors<br/>732MB |
|
|
23
|
+
| **OpenAI Small** | 1536 | 500 vectors<br/>3MB | 5K vectors<br/>29MB | 25K vectors<br/>147MB | 100K vectors<br/>586MB |
|
|
24
|
+
| **OpenAI Large** | 3072 | 200 vectors<br/>2MB | 2.5K vectors<br/>29MB | 5K vectors<br/>59MB | 25K vectors<br/>293MB |
|
|
25
|
+
|
|
26
|
+
*Benchmarks performed on Apple M2 hardware*
|
|
27
|
+
|
|
28
|
+
## Installation
|
|
29
|
+
|
|
30
|
+
**⚠️ Pending submission to PyPI**
|
|
31
|
+
|
|
32
|
+
```bash
|
|
33
|
+
uv add numpy-vector-store
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
## Quick Start
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
import numpy as np
|
|
40
|
+
from numpy_vector_store import VectorStore
|
|
41
|
+
|
|
42
|
+
# Load your vector store
|
|
43
|
+
store = VectorStore(dimensions=1536, file_path='vectors.npz')
|
|
44
|
+
store.load()
|
|
45
|
+
|
|
46
|
+
# Embed your search query
|
|
47
|
+
query = np.array([0.2, 0.3, 0.4, ...])
|
|
48
|
+
|
|
49
|
+
# Search using cosine similarity
|
|
50
|
+
results = store.search(query, top_k=3)
|
|
51
|
+
|
|
52
|
+
# Compare the results
|
|
53
|
+
for index, similarity, meta in results:
|
|
54
|
+
print(f"{meta['title']}: {similarity:.3f}")
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
## Usage Examples
|
|
58
|
+
|
|
59
|
+
### Adding Vectors
|
|
60
|
+
|
|
61
|
+
#### Adding Vectors in Batch
|
|
62
|
+
|
|
63
|
+
The `add_vectors` method takes a **2D NumPy array** where each row is a vector, and a **1D NumPy array** of metadata objects.
|
|
64
|
+
|
|
65
|
+
```python
|
|
66
|
+
# 2D np.array of vectors
|
|
67
|
+
embeddings = np.array([
|
|
68
|
+
[0.1, 0.2, 0.3, ...], # Text embedding 1
|
|
69
|
+
[0.4, 0.5, 0.6, ...], # Text embedding 2
|
|
70
|
+
[0.7, 0.8, 0.9, ...] # Text embedding 3
|
|
71
|
+
])
|
|
72
|
+
|
|
73
|
+
# 1D np.array of metadata objects
|
|
74
|
+
metadata = np.array([
|
|
75
|
+
{"title": "AI Overview", "word_count": 12},
|
|
76
|
+
{"title": "Python Guide", "word_count": 10},
|
|
77
|
+
{"title": "Vector DBs", "word_count": 8}
|
|
78
|
+
])
|
|
79
|
+
|
|
80
|
+
# Vectors and metadata added using efficient vectorized NumPy operations
|
|
81
|
+
store.add_vectors(embeddings, metadata)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
#### Adding Vectors Individually
|
|
85
|
+
|
|
86
|
+
Batch operations are generally preferable and more efficient. But individual vectors can be added with the same method.
|
|
87
|
+
|
|
88
|
+
```python
|
|
89
|
+
store.add_vectors(
|
|
90
|
+
np.array([new_embedding]), # or np.atleast_2d(new_embedding)
|
|
91
|
+
np.array([{"title": "Neural Networks Paper", "word_count": 15}])
|
|
92
|
+
)
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
### Save the Vector Store
|
|
96
|
+
|
|
97
|
+
Save to a gzip compressed NumPy binary file directly
|
|
98
|
+
|
|
99
|
+
```python
|
|
100
|
+
store.save()
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Or use context manager for automatic persistence
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
with VectorStore(dimensions=3, file_path="vectors.npz") as store:
|
|
107
|
+
store.add_vectors(vectors_2d, metadata_array)
|
|
108
|
+
|
|
109
|
+
# Automatically saves when exiting the context
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Working with Metadata
|
|
113
|
+
|
|
114
|
+
Work with flexible, unstructured metadata using standard Python operations. No schema required - perfect for getting started quickly:
|
|
115
|
+
|
|
116
|
+
```python
|
|
117
|
+
# Access individual metadata
|
|
118
|
+
first_metadata = store.metadata[0]
|
|
119
|
+
print(f"First entry: {first_metadata}")
|
|
120
|
+
|
|
121
|
+
# Iterate through all metadata
|
|
122
|
+
for i, metadata in enumerate(store.metadata):
|
|
123
|
+
print(f"Entry {i}: {metadata}")
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
### Advanced: Structured Metadata for Performance
|
|
127
|
+
|
|
128
|
+
If you define a homogeneous NumPy schema upfront, you get **significant performance improvements** for metadata operations. Instead of Python loops and dictionary lookups, you get **vectorized NumPy operations** that are orders of magnitude faster.
|
|
129
|
+
|
|
130
|
+
> **Learn more**: [NumPy Structured Arrays Documentation](https://numpy.org/doc/stable/user/basics.rec.html)
|
|
131
|
+
|
|
132
|
+
```python
|
|
133
|
+
# Define schema for performance
|
|
134
|
+
store = VectorStore(
|
|
135
|
+
dimensions=512,
|
|
136
|
+
metadata_schema={'title': 'U200', 'year': 'i4', 'citations': 'i4'}
|
|
137
|
+
)
|
|
138
|
+
store.add_vectors(
|
|
139
|
+
np.array([vector1, vector2]),
|
|
140
|
+
np.array([
|
|
141
|
+
{"title": "Paper 1", "year": 2023, "citations": 100},
|
|
142
|
+
{"title": "Paper 2", "year": 2022, "citations": 50}
|
|
143
|
+
])
|
|
144
|
+
)
|
|
145
|
+
|
|
146
|
+
# Perform vectorized NumPy operations on metadata
|
|
147
|
+
recent_mask = store.metadata['year'] == 2023
|
|
148
|
+
recent_vectors = np.array(store.vectors)[recent_mask]
|
|
149
|
+
|
|
150
|
+
# Sort by citations
|
|
151
|
+
sorted_indices = np.argsort(store.metadata['citations'])[::-1]
|
|
152
|
+
top_vectors = np.array(store.vectors)[sorted_indices]
|
|
153
|
+
|
|
154
|
+
# Complex filtering
|
|
155
|
+
high_impact = store.metadata['citations'] > 100
|
|
156
|
+
recent = store.metadata['year'] > 2020
|
|
157
|
+
combined_mask = high_impact & recent
|
|
158
|
+
filtered_vectors = np.array(store.vectors)[combined_mask]
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Contributing
|
|
162
|
+
|
|
163
|
+
### Setup Development Environment
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
git clone https://github.com/tvanreenen/numpy-vector-store.git
|
|
167
|
+
cd numpy-vector-store
|
|
168
|
+
uv sync --frozen --group dev
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Before Submitting a Pull Request
|
|
172
|
+
|
|
173
|
+
Please ensure:
|
|
174
|
+
|
|
175
|
+
1. **Code Quality**: Run `uv run ruff check` - should show no issues
|
|
176
|
+
2. **Formatting**: Run `uv run ruff format` - should show "files left unchanged"
|
|
177
|
+
3. **Type Checking**: Run `uv run mypy src/` - should show no errors
|
|
178
|
+
4. **Tests**: Run `uv run pytest` - all tests should pass
|
|
179
|
+
|
|
180
|
+
## License
|
|
181
|
+
|
|
182
|
+
MIT License - see [LICENSE](LICENSE) file for details.
|