|
| 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