Skip to content

Commit e88a793

Browse files
committed
Python DB API: Add basic example about i/o with vector embeddings
1 parent 5ae72b4 commit e88a793

File tree

4 files changed

+124
-56
lines changed

4 files changed

+124
-56
lines changed

by-language/python-dbapi/insert_basic.py renamed to by-language/python-dbapi/basic.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
docker run --rm -it --publish=4200:4200 crate
2222
2323
# Invoke example program.
24-
time python insert_basic.py
24+
time python basic.py
2525
2626
"""
2727
import sys
@@ -31,11 +31,28 @@
3131
import crate.client
3232

3333

34-
def insert_basic():
34+
CRATEDB_URL = "http://localhost:4200"
35+
36+
37+
def select_summits():
38+
"""
39+
Demonstrate a basic conversation with CrateDB, querying its built-in `sys.summits` table.
40+
"""
41+
connection = crate.client.connect(CRATEDB_URL)
42+
cursor = connection.cursor()
43+
cursor.execute("SELECT * FROM sys.summits LIMIT 3;")
44+
results = cursor.fetchall()
45+
cursor.close()
46+
connection.close()
47+
48+
pprint(results)
49+
50+
51+
def insert_datetime():
3552
"""
3653
Demonstrate a basic conversation with CrateDB, inserting a record including a timestamp.
3754
"""
38-
connection = crate.client.connect("http://localhost:4200")
55+
connection = crate.client.connect(CRATEDB_URL)
3956
cursor = connection.cursor()
4057
cursor.execute("DROP TABLE IF EXISTS testdrive.foo;")
4158
cursor.execute("CREATE TABLE testdrive.foo (id INT, timestamp TIMESTAMP WITH TIME ZONE);")
@@ -52,4 +69,5 @@ def insert_basic():
5269

5370

5471
if __name__ == "__main__":
55-
insert_basic()
72+
select_summits()
73+
insert_datetime()

by-language/python-dbapi/select_basic.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

by-language/python-dbapi/test.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ def run(command: str):
77
subprocess.check_call(shlex.split(command))
88

99

10-
def test_select_basic():
11-
cmd = "time python select_basic.py"
10+
def test_basic():
11+
cmd = "time python basic.py"
1212
run(cmd)
1313

1414

15-
def test_insert_basic():
16-
cmd = "time python insert_basic.py"
15+
def test_vector():
16+
cmd = "time python vector.py"
1717
run(cmd)

by-language/python-dbapi/vector.py

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""
2+
About
3+
=====
4+
5+
Example program to demonstrate connecting to CrateDB using
6+
its Python DB API driver, and the HTTP protocol.
7+
8+
9+
Setup
10+
=====
11+
::
12+
13+
pip install --upgrade crate
14+
15+
16+
Synopsis
17+
========
18+
::
19+
20+
# Run CrateDB
21+
docker run --rm -it --publish=4200:4200 crate
22+
23+
# Invoke example program.
24+
time python select_basic.py
25+
26+
"""
27+
import sys
28+
from pprint import pprint
29+
30+
import crate.client
31+
32+
33+
CRATEDB_URL = "http://localhost:4200"
34+
35+
36+
def vector_io():
37+
"""
38+
Demonstrate a basic conversation with CrateDB, inserting and querying vector embeddings.
39+
"""
40+
connection = crate.client.connect(CRATEDB_URL)
41+
42+
# Insert.
43+
cursor = connection.cursor()
44+
cursor.execute("DROP TABLE IF EXISTS testdrive.foo;")
45+
cursor.execute("CREATE TABLE testdrive.foo (id INT, embedding FLOAT_VECTOR(3));")
46+
cursor.execute("INSERT INTO testdrive.foo (id, embedding) VALUES (42, [42.42, 43.43, 44.44]);")
47+
cursor.execute("INSERT INTO testdrive.foo (id, embedding) VALUES (43, ?);", parameters=[[84.84, 85.85, 86.86]])
48+
cursor.execute("REFRESH TABLE testdrive.foo;")
49+
cursor.close()
50+
51+
# Select.
52+
cursor = connection.cursor()
53+
54+
# Literal `knn_match`.
55+
print("knn_match: literal")
56+
cursor.execute(
57+
"SELECT * FROM testdrive.foo WHERE knn_match(embedding, [1.1, 2.2, 3.3], 1);")
58+
results = cursor.fetchall()
59+
pprint(results)
60+
61+
# `knn_match` with parameters.
62+
print("knn_match: with parameters")
63+
cursor.execute(
64+
"SELECT * FROM testdrive.foo WHERE knn_match(embedding, ?, 1);", parameters=[[1.1, 2.2, 3.3]])
65+
results = cursor.fetchall()
66+
pprint(results)
67+
68+
# Literal `vector_similarity`.
69+
print("vector_similarity: literal")
70+
cursor.execute(
71+
"SELECT *, vector_similarity(embedding, [1.1, 2.2, 3.3]) AS _score "
72+
"FROM testdrive.foo ORDER BY _score DESC;")
73+
results = cursor.fetchall()
74+
pprint(results)
75+
76+
# `vector_similarity` with parameters.
77+
print("vector_similarity: with parameters")
78+
cursor.execute(
79+
"SELECT *, vector_similarity(embedding, ?) AS _score "
80+
"FROM testdrive.foo ORDER BY _score DESC;", parameters=[[1.1, 2.2, 3.3]])
81+
results = cursor.fetchall()
82+
pprint(results)
83+
84+
# All together now.
85+
print("knn_match and vector_similarity")
86+
cursor.execute(
87+
"SELECT id, embedding, vector_similarity(embedding, ?) AS _score FROM testdrive.foo "
88+
"WHERE knn_match(embedding, ?, ?) ORDER BY _score DESC LIMIT ?;",
89+
parameters=[[1.1, 2.2, 3.3], [1.1, 2.2, 3.3], 1, 1])
90+
results = cursor.fetchall()
91+
pprint(results)
92+
93+
cursor.close()
94+
connection.close()
95+
96+
97+
if __name__ == "__main__":
98+
vector_io()

0 commit comments

Comments
 (0)