|
1 | | -# sqlite-vec-client |
| 1 | +# sqlite-vec-client |
| 2 | + |
| 3 | +A tiny, lightweight Pythonic helper around [sqlite-vec](https://github.com/asg017/sqlite-vec) that lets you store texts, JSON metadata, and float32 embeddings in SQLite and run fast similarity search. |
| 4 | + |
| 5 | +## Features |
| 6 | +- **Simple API**: One class, `SQLiteVecClient`, for CRUD and search. |
| 7 | +- **Vector index via sqlite-vec**: Uses a `vec0` virtual table under the hood. |
| 8 | +- **Automatic sync**: Triggers keep the base table and vector index aligned. |
| 9 | +- **Typed results**: Clear return types for results and searches. |
| 10 | +- **Filtering helpers**: Fetch by `rowid`, `text`, or `metadata`. |
| 11 | +- **Pagination & sorting**: List records with `limit`, `offset`, and order. |
| 12 | + |
| 13 | +## Requirements |
| 14 | +- Python 3.9+ |
| 15 | +- [SQLite version 3.41 or higher](https://alexgarcia.xyz/sqlite-vec/python.html#updated-sqlite) |
| 16 | +- [The sqlite-vec extension](https://github.com/asg017/sqlite-vec) |
| 17 | + |
| 18 | +## Installation |
| 19 | +Install from PyPI: |
| 20 | + |
| 21 | +```bash |
| 22 | +pip install sqlite-vec-client |
| 23 | +``` |
| 24 | + |
| 25 | +Or: |
| 26 | + |
| 27 | +```bash |
| 28 | +git clone https://github.com/atasoglu/sqlite-vec-client |
| 29 | +cd sqlite-vec-client |
| 30 | +pip install . |
| 31 | +``` |
| 32 | + |
| 33 | +## Quick start |
| 34 | +```python |
| 35 | +from sqlite_vec_client import SQLiteVecClient |
| 36 | + |
| 37 | +# Initialize a client bound to a specific table in a database file |
| 38 | +client = SQLiteVecClient(table="documents", db_path="./example.db") |
| 39 | + |
| 40 | +# Create schema (base table + vec index); choose embedding dimension and distance |
| 41 | +client.create_table(dim=384, distance="cosine") |
| 42 | + |
| 43 | +# Add some texts with embeddings (one embedding per text) |
| 44 | +texts = ["hello world", "lorem ipsum", "vector databases"] |
| 45 | +embs = [ |
| 46 | + [0.1, 0.2, 0.3, *([0.0] * 381)], |
| 47 | + [0.05, 0.04, 0.03, *([0.0] * 381)], |
| 48 | + [0.2, 0.1, 0.05, *([0.0] * 381)], |
| 49 | +] |
| 50 | +rowids = client.add(texts=texts, embeddings=embs) |
| 51 | + |
| 52 | +# Similarity search returns (rowid, text, distance) |
| 53 | +query_emb = [0.1, 0.2, 0.3, *([0.0] * 381)] |
| 54 | +hits = client.similarity_search(embedding=query_emb, top_k=3) |
| 55 | + |
| 56 | +# Fetch full rows (rowid, text, metadata, embedding) |
| 57 | +rows = client.get_many(rowids) |
| 58 | + |
| 59 | +client.close() |
| 60 | +``` |
| 61 | + |
| 62 | +## How it works |
| 63 | +`SQLiteVecClient` stores data in `{table}` and mirrors embeddings in `{table}_vec` (a `vec0` virtual table). SQLite triggers keep both in sync when rows are inserted, updated, or deleted. Embeddings are serialized as packed float32 bytes for compact storage. |
| 64 | + |
| 65 | +## Contributing |
| 66 | +Contributions are very welcome—issues, ideas, and PRs help this project grow! |
| 67 | + |
| 68 | +## License |
| 69 | + |
| 70 | +MIT |
0 commit comments