Check out my blog post on sqlite vector embeddings that explains this project!
This example tutorial project demonstrates how to utilize TensorFlow.js (specifically Universal Sentence Encoder) and SQLite with the sqlite-vss vector extension to create, and search a chat history stored in SQLite.
The project consists of the following functionalities:
- Embeddings Creation: Using TensorFlow.js and Universal Sentence Encoder, it creates vector embeddings from raw string inputs.
- Database Setup: Connects to SQLite and loads the required extensions for vector operations, along with table creation.
- Chat History Management: It provides functions to add chat messages to the history and perform search queries using vector similarity.
Make sure you have Node.js installed in your environment and the required SQLite extensions:
sqlite3
@tensorflow/tfjs-node
@tensorflow-models/universal-sentence-encoder
-
Install wget (if not already installed):
brew install wget
-
Run the installation script:
sh install.sh
This script will detect your system's architecture and platform (currently supports arm64 and x86_64 on macOS and x86_64 on Linux) and download the appropriate SQLite vector extensions. Check out the in-depth docs for how to build sqlite-vss yourself to add support for other operating systems.
-
Install the necessary dependencies:
npm install
Make sure the SQLite extensions and database paths are configured correctly in the code:
const vectorExtensionPath = "./vector0.dylib"
const vssExtensionPathVSS = "./vss0.dylib"
const DB_PATH = "./chat.sqlite"
Run npm start
and if everything is installed correctly, you should see a list of vectors printed in descending order ranked by similarity to the example input query vector.
createQueryEmbedding(query: string)
: Creates an embedding for the given query.createMessageEmbedding(type: string, command: string, content: string, timestamp: string)
: Creates an embedding for the given chat message.setupEmbeddings()
: Prepares the Universal Sentence Encoder.
setupDatabase(): Promise<Database>
: Opens the database and loads the required extensions, creates tables.searchChatHistory(query: string)
: Searches the chat history based on the query embedding.addToChatHistory(type: "user" | "ai", command: string, content: string): Promise<void>
: Adds a chat message to the history.
- The Universal Sentence Encoder model for creating semantic textual embeddings.
- SQLite vector extension for vector search functionality within the database.