Metadata-Version: 2.5
Name: graphvec-ann
Version: 0.2.0
Summary: Local-first, file-based approximate nearest-neighbor vector search engine
Project-URL: Homepage, https://github.com/zahidhasann88/graphvec
Project-URL: Source, https://github.com/zahidhasann88/graphvec
Project-URL: Issues, https://github.com/zahidhasann88/graphvec/issues
Project-URL: Changelog, https://github.com/zahidhasann88/graphvec/blob/main/CHANGELOG.md
Author-email: Zahid Hasan <jahidhasann67@gmail.com>
License: MIT License
        
        Copyright (c) 2026 GraphVec Contributors
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: ann,embeddings,hnsw,vector-search
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Topic :: Database
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: numpy>=1.24
Requires-Dist: rich>=13
Requires-Dist: typer>=0.9
Provides-Extra: bench
Requires-Dist: faiss-cpu; extra == 'bench'
Requires-Dist: psutil; extra == 'bench'
Provides-Extra: dev
Requires-Dist: build>=1.2; extra == 'dev'
Requires-Dist: hypothesis>=6; extra == 'dev'
Requires-Dist: mypy>=1.10; extra == 'dev'
Requires-Dist: pytest-benchmark>=4; extra == 'dev'
Requires-Dist: pytest>=7; extra == 'dev'
Requires-Dist: ruff>=0.4; extra == 'dev'
Provides-Extra: parquet
Requires-Dist: pyarrow; extra == 'parquet'
Description-Content-Type: text/markdown

# GraphVec

GraphVec is a local, file-based vector search library for Python. It stores an
index in a directory on disk and finds nearest neighbours with its own
HNSW-style graph. It needs no database server, cloud service, or background process.

It is written in pure Python (3.11+) and NumPy. That makes it easy to read and embed,
but it is slower than compiled libraries such as FAISS or hnswlib.

## Installation

```bash
pip install graphvec-ann           # the import name and command are `graphvec`
pip install "graphvec-ann[parquet]"  # optional Parquet import
```

## Quick start

```python
import numpy as np
from graphvec import GraphIndex

rng = np.random.default_rng(42)
vectors = rng.random((1_000, 128), dtype=np.float32)
ids = [f"doc-{i}" for i in range(len(vectors))]

with GraphIndex.create("./vectors.gvec", dimensions=128, metric="cosine") as index:
    index.add(vectors, ids=ids)
    index.save()  # nothing is written to disk until save()

with GraphIndex.open("./vectors.gvec", readonly=True) as index:
    query = rng.random(128, dtype=np.float32)
    for result in index.search(query, k=5, ef_search=100):
        print(result.id, result.score)
```

## Python API

| Method | Purpose |
|---|---|
| `GraphIndex.create(path, dimensions, metric="cosine", max_neighbors=16, construction_ef=200, seed=42, overwrite=False)` | Create a new index directory |
| `GraphIndex.open(path, readonly=False, verify_checksums=False)` | Open an index (read-only mode memory-maps vectors) |
| `add(vectors, ids=None, replace=False)` | Insert vectors; duplicate IDs raise `DuplicateIDError` unless `replace=True` |
| `search(query, k=10, ef_search=100)` / `search_batch(queries, ...)` | Approximate search; returns `SearchResult(id, score, internal_id)` |
| `search_exact(query, k=10)` | Brute-force search, useful to measure recall |
| `get(id)` / `contains(id)` / `delete(ids)` / `iter_ids()` | Read, check, delete, or list IDs (deletes are tombstones) |
| `compact()` / `build()` | Drop deleted rows, rebuild the graph and save / only rebuild the graph |
| `save()` / `close()` / `validate()` | Commit to disk, release the writer lock, check consistency |
| `GraphIndex.force_unlock(path)` | Remove a stale writer lock after a crash |

Higher `ef_search` gives better recall but slower queries. All errors inherit from `graphvec.GraphVecError`.

**Metrics** (`metric=`):
- `cosine`: score is cosine similarity, higher is better. Vectors are normalised
  when added, so `get()` returns the normalised vector. Zero vectors score 0.
- `dot`: score is the dot product, higher is better.
- `squared_euclidean`: score is the squared L2 distance, lower is better.

## Command line

```bash
graphvec create ./index.gvec --dimensions 128 --metric cosine
graphvec import ./index.gvec vectors.npy --ids ids.json   # or .jsonl / .parquet
graphvec search ./index.gvec --query-file query.npy --top-k 10
graphvec info ./index.gvec
graphvec validate ./index.gvec
graphvec benchmark ./index.gvec --queries queries.npy --top-k 10
graphvec compact ./index.gvec
graphvec unlock ./index.gvec                              # after a crashed writer
graphvec export ./index.gvec --output vectors.npy         # also writes vectors.npy.ids.json
```

JSONL records look like `{"id": "doc-1", "vector": [0.1, ...]}`; Parquet files need `id` and
`vector` columns and the `parquet` extra. Most commands accept `--json`; see `graphvec --help`.

## Limitations
- Read-only opens memory-map vectors and the graph; writable indexes load everything into RAM.
- One writer at a time. A crash leaves a stale lock; clear it with `graphvec unlock`.
- Deleted IDs cannot be reused until `compact()`.
- Insertion is slow (hundreds of vectors per second in our measurements).
- More detail and crash-safety guarantees: [docs/limitations.md](https://github.com/zahidhasann88/graphvec/blob/main/docs/limitations.md).

## Documentation

[Architecture and algorithms](https://github.com/zahidhasann88/graphvec/blob/main/docs/architecture.md) · [File format](https://github.com/zahidhasann88/graphvec/blob/main/docs/file-format.md) · [Benchmarks](https://github.com/zahidhasann88/graphvec/blob/main/docs/benchmarks.md) · [Changelog](https://github.com/zahidhasann88/graphvec/blob/main/CHANGELOG.md) · [Security](https://github.com/zahidhasann88/graphvec/blob/main/SECURITY.md)

## Development

Add tests for behaviour changes, and never commit benchmark numbers you did not measure.
```bash
pip install -e ".[dev]"
pytest                                   # add -m slow for large tests
ruff check . && ruff format --check .
mypy src/graphvec
```

## License

MIT. See [LICENSE](https://github.com/zahidhasann88/graphvec/blob/main/LICENSE).
