Skip to content

Commit af16341

Browse files
committed
Examples: AI: RAG: Added script to create SQLite vector database
It's run with install.sh.
1 parent 75498d2 commit af16341

13 files changed

Lines changed: 191 additions & 3 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"folders": [
3+
{
4+
"path": "."
5+
}
6+
]
7+
}

‎Examples/AI/RAG/Database/clean.sh‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
# Exit script if a step fails
4+
set -e
5+
# Set working directory to script directory
6+
cd "$(dirname "$0")"
7+
8+
echo "==== Cleaning: Example AI RAG database"
9+
10+
rm -f vector.db

‎Examples/AI/RAG/Database/createSqlite.sql‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
-- In DB Browser, load vec0.dll extension from ./node_modules/sqlite-vec<platform>/vec0*
1+
-- When using DB Browser for SQlite,
2+
-- first load `vec0.dll` extension from `./node_modules/sqlite-vec<platform>/vec0`*
23

34
CREATE VIRTUAL TABLE documents USING vec0(
45
id INTEGER PRIMARY KEY,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as fs from "node:fs";
2+
import { DatabaseSync } from "node:sqlite";
3+
import * as sqliteVec from "sqlite-vec"
4+
5+
// Read SQL script.
6+
let sqlPath = "createSqlite.sql";
7+
console.log( "Reading SQL script: " + sqlPath );
8+
let sql = fs.readFileSync( sqlPath ).toString();
9+
10+
// Create and open new database
11+
let databasePath = "vector.db";
12+
console.log( "Creating SQLite database: " + databasePath );
13+
if ( fs.existsSync( databasePath ) )
14+
fs.unlinkSync( databasePath );
15+
let database = new DatabaseSync( databasePath, { allowExtension: true } );
16+
17+
// Load vector extension and create vector table
18+
sqliteVec.load( database );
19+
database.exec( sql );
20+
21+
// Close
22+
database.close();
23+
24+
console.log( "Done." );
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#!/bin/bash
2+
# This script creates the SQLite example database
3+
4+
# Exit script if a step fails
5+
set -e
6+
# Set working directory to script directory
7+
cd "$(dirname "$0")"
8+
9+
echo "==== SQLite vector database"
10+
11+
npm install
12+
13+
node createSqliteDatabase.js

‎Examples/AI/RAG/Database/package-lock.json‎

Lines changed: 90 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "module",
3+
"dependencies": {
4+
"sqlite-vec": "^0.1.9"
5+
}
6+
}

‎Examples/AI/RAG/Database/update.sh‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
3+
# Exit script if a step fails
4+
set -e
5+
# Set working directory to script directory.
6+
cd "$(dirname "$0")"
7+
8+
# Check confirmation argument '-y'
9+
10+
if
11+
[[ ! "$1" == "-y" ]]
12+
then
13+
echo "Script must be called with argument '-y' to update."
14+
echo "Aborting."
15+
exit 1
16+
fi
17+
18+
# Perform update
19+
20+
echo "==== Updating: Example AI RAG SQLite database"
21+
npx npm-check-updates -u --cooldown 7d
22+
./install.sh

‎Examples/AI/RAG/clean.sh‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@ cd "$(dirname "$0")"
77
echo "==== Cleaning: Example AI RAG"
88

99
rm -fr ./out
10+
11+
# Clean the database
12+
./Database/clean.sh

‎Examples/AI/RAG/doc/Setup.md‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ The SQLite database is included in Node.js, it does not need to be installed sep
2525
An SQLite extension to enable vector storage.\
2626
The binary of the actual extension is included in the npm package,\
2727
in the folder `node_modules/sqlite-vec-<platform>`.\
28-
The app loads in automatically.
28+
The app loads it automatically.
29+
30+
The `install.sh` script for this app creates the SQLite `vector.db` database automatically.\
31+
So the manual steps below are not necessary to run the app.
2932

3033
### DB Brower for SQLite
3134

@@ -41,6 +44,10 @@ Open DB Browser and activate the vector extension through the menu:
4144
Then open the SQL script `Database/createSqlite.sql` .\
4245
Run it to table `documents` also containing embedding vectors.
4346

47+
2026-08-06: MacOS note.\
48+
This step currently fails on MacOS 26.6 on Intel (ARM not tested).\
49+
Use the install script to create the datebase on MacOS.
50+
4451
## Postgres with vectors
4552

4653
An alternative to SQLite, you can use Postgres.

0 commit comments

Comments
 (0)