seekmix
Version:
🔍 A local semantic caching library for Node.js.
204 lines (157 loc) • 4.37 kB
Markdown
- [Installation](
- [Loading the Extension](
- [Binary Serialization Helper](
- [NumPy Integration](
- [Connection Setup Pattern](
- [SQLite Version Requirements](
- [MacOS Considerations](
```bash
pip install sqlite-vec
```
- Node.js: `pnpm add sqlite-vec`
- Ruby: `gem install sqlite-vec`
- Go: `go get -u github.com/asg017/sqlite-vec/bindings/go`
- Rust: `cargo add sqlite-vec`
```python
import sqlite3
import sqlite_vec
db = sqlite3.connect(":memory:")
db.enable_load_extension(True)
sqlite_vec.load(db)
db.enable_load_extension(False)
vec_version, = db.execute("select vec_version()").fetchone()
print(f"vec_version={vec_version}")
```
```sql
.load ./vec0
select vec_version();
```
Converts a Python list of floats into the compact BLOB format sqlite-vec expects:
```python
from sqlite_vec import serialize_float32
import struct
embedding = [0.1, 0.2, 0.3, 0.4]
blob = serialize_float32(embedding)
blob = struct.pack("%sf" % len(embedding), *embedding)
db.execute(
"INSERT INTO vec_items(rowid, embedding) VALUES (?, ?)",
[]
)
```
For int8 vectors:
```python
from sqlite_vec import serialize_int8
int_vector = [1, 2, 3, 4]
blob = serialize_int8(int_vector)
```
NumPy arrays can be passed directly as they implement the Buffer protocol. Cast to float32:
```python
import numpy as np
embedding = np.array([0.1, 0.2, 0.3, 0.4])
db.execute(
"SELECT vec_length(?)",
[]
)
```
For advanced NumPy integration with static blobs:
```python
from sqlite_vec import register_numpy
import numpy as np
vectors = np.array([
[],
[],
[]
], dtype=np.float32)
register_numpy(db, "my_vectors", vectors)
results = db.execute(
"SELECT rowid, vector FROM my_vectors"
).fetchall()
```
Complete setup pattern for Python:
```python
import sqlite3
import sqlite_vec
from sqlite_vec import serialize_float32
import struct
db = sqlite3.connect(":memory:")
db.enable_load_extension(True)
sqlite_vec.load(db)
db.enable_load_extension(False)
sqlite_version, vec_version = db.execute(
"select sqlite_version(), vec_version()"
).fetchone()
print(f"sqlite_version={sqlite_version}, vec_version={vec_version}")
db.execute("""
CREATE VIRTUAL TABLE vec_items USING vec0(
embedding float[4]
)
""")
items = [
(1, [0.1, 0.1, 0.1, 0.1]),
(2, [0.2, 0.2, 0.2, 0.2]),
(3, [0.3, 0.3, 0.3, 0.3])
]
with db:
for rowid, vector in items:
db.execute(
"INSERT INTO vec_items(rowid, embedding) VALUES (?, ?)",
[]
)
query = [0.25, 0.25, 0.25, 0.25]
results = db.execute(
"""
SELECT rowid, distance
FROM vec_items
WHERE embedding MATCH ?
AND k = 2
ORDER BY distance
""",
[]
).fetchall()
```
sqlite-vec requires SQLite 3.41+ for full functionality. Check version:
```bash
python -c 'import sqlite3; print(sqlite3.sqlite_version)'
```
1. Use `pysqlite3` package (bundles updated SQLite)
2. Compile custom SQLite with LD_PRELOAD/DYLD_LIBRARY_PATH
3. Upgrade Python version (3.12+ usually has recent SQLite)
## MacOS Considerations
Default MacOS Python doesn't support SQLite extensions. Solutions:
1. Use Homebrew Python: `brew install python`
2. Use `/opt/homebrew/bin/python3` instead of system Python
3. Install `pysqlite3` package
Error indicating this issue:
```
AttributeError: 'sqlite3.Connection' object has no attribute 'enable_load_extension'
```