Py.Cafe

maartenbreddels/

sqlite-vec-demo

Dynamically loading an sqlite sqlite3 extension (sqlite_vec) on WASM, Pyodide, Emscripten, PyCafe

DocsPricing
  • app.py
  • requirements.txt
  • sqlite3-1.0.0-py2.py3-none-any.whl
  • sqlite_vec-0.1.3-cp312-cp312-pyodide_2024_0_wasm32.whl
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# see https://github.com/asg017/sqlite-vec/issues/135
import sqlite3
import sqlite_vec
import streamlit as st

db = conn = sqlite3.connect(":memory:")
db.execute("CREATE TABLE movie(title, year, score)")

conn.enable_load_extension(True)


sqlite_vec.load(db)

# Instead of an unpatched load, we can use
#import os
#path = os.path.abspath(os.path.dirname(sqlite_vec.__file__))
#ext_path = os.path.join(path, "vec0.so")
#print(os.listdir(path))
#conn.load_extension(ext_path)

conn.execute("""

create virtual table vec_examples using vec0(
  sample_embedding float[8]
);
""")
def out(r):
    results = r.fetchall()
    print("results", results)
    for row in results:
        print(row)
        st.write(row)

out(conn.execute("""
-- vectors can be provided as JSON or in a compact binary format
insert into vec_examples(rowid, sample_embedding)
  values
    (1, '[-0.200, 0.250, 0.341, -0.211, 0.645, 0.935, -0.316, -0.924]'),
    (2, '[0.443, -0.501, 0.355, -0.771, 0.707, -0.708, -0.185, 0.362]'),
    (3, '[0.716, -0.927, 0.134, 0.052, -0.669, 0.793, -0.634, -0.162]'),
    (4, '[-0.710, 0.330, 0.656, 0.041, -0.990, 0.726, 0.385, -0.958]');
"""))

out(conn.execute("""
select
  rowid,
  distance
from vec_examples
where sample_embedding match '[0.890, 0.544, 0.825, 0.961, 0.358, 0.0196, 0.521, 0.175]'
 AND k = 3;
"""))

out(conn.execute("""
select
  rowid,
  distance
from vec_examples
where k = 3;
"""))
requirements.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

streamlit==1.27.2  # currently pinned to this version
#pyarrow==17.0.0

sqlite3 @ https://py.cafe/files/maartenbreddels/sqlite-vec-demo/sqlite3-1.0.0-py2.py3-none-any.whl
sqlite_vec @ https://py.cafe/files/maartenbreddels/sqlite-vec-demo/sqlite_vec-0.1.3-cp312-cp312-pyodide_2024_0_wasm32.whl