Skip to content

Commit d031c9b

Browse files
Add vector example
1 parent c26dd86 commit d031c9b

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

examples/vector/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
local.db-journal

examples/vector/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Local
2+
3+
This example demonstrates how to use libSQL vector search with a local database.
4+
5+
## Install Dependencies
6+
7+
```bash
8+
pip install libsql-experimental
9+
```
10+
11+
## Running
12+
13+
Execute the example:
14+
15+
```bash
16+
python3 main.py
17+
```
18+
19+
This will setup a local SQLite database, insert some data, and then query the results using the vector similarity search function.

examples/vector/main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import libsql_experimental as libsql
2+
3+
conn = libsql.connect("local.db")
4+
5+
conn.execute("DROP TABLE IF EXISTS movies")
6+
conn.execute("CREATE TABLE IF NOT EXISTS movies (title TEXT, year INT, embedding F32_BLOB(3))")
7+
conn.execute("CREATE INDEX movies_idx ON movies (libsql_vector_idx(embedding))")
8+
conn.execute("INSERT INTO movies (title, year, embedding) VALUES ('Napoleon', 2023, vector32('[1,2,3]')), ('Black Hawk Down', 2001, vector32('[10,11,12]')), ('Gladiator', 2000, vector32('[7,8,9]')), ('Blade Runner', 1982, vector32('[4,5,6]'))")
9+
10+
print(conn.execute("SELECT title, year FROM vector_top_k('movies_idx', '[4,5,6]', 3) JOIN movies ON movies.rowid = id").fetchall())

0 commit comments

Comments
 (0)