Logo

dev-resources.site

for different kinds of informations.

Implementing a RAG system inside an RDBMS: Sqlite and Postgres with Sqlite-vec, PGVector.

Published at
9/13/2024
Categories
llm
rag
rdbms
embedding
Author
jonbiz
Categories
4 categories in total
llm
open
rag
open
rdbms
open
embedding
open
Author
6 person written this
jonbiz
open
Implementing a RAG system inside an RDBMS: Sqlite and Postgres with Sqlite-vec, PGVector.

tl;dr: A discussion of the pros and cons of storing embedding data with the data it represents vs an external vector database. I'll be following this post up shortly with a walkthrough the boilerplate required to create and search embeddings in SQlite and PostGres.

"Vector Databases: They are single-purpose DBMSs with indexes to accelerate nearest-neighbor search. RM DBMSs should soon provide native support for these data structures and search methods using their extendable type system that will *render such specialized databases unnecessary" - Stonebraker & Pavlo, 2024.*

I've been exploring RAG techniques and embeddings and as part of that, I've been checking out effective embedding generation and storage/retrieval options. As long as I'm able to perform vector searches, I don't see the value in storing embedding data separately from the relational data it represents.

Do dedicated vector databases make sense?

Vector storage and search is at this point, essentially commoditized: going forward it's not clear to me (or others) how dedicated vector databases can differentiate themselves from bog-standard relational databases with Vector search enabled.

Storing vector embeddings with the data that they represent is convenient and allows for succinct access to the results of vector-based searches. Some people are finding that keeping a separate vector DB in synch can be "painful at best, even for prototype applications." That said, vector database providers are understandably keen to provide value. But, given that both storing vectors and searching them are solved problems, there doesn't appear to be much room in which they could make any improvements.

So, even if adding vector storage to your existing database won't work for you, making adding a secondary database your the least-worst option, there's no obvious reason not to consider Postgres with PGVec, (or PGVec-scale) for that role.

Bonus: LLMS can speak SQL.

Many LLMs compose SQL well, which brings up an interesting possibility: LLM agents that can compose their own vector-based search queries. The use cases where this would make sense might be minimal at the moment: but interesting avenue nonetheless. I want to play around with that.

The contenders

Currently, I am creating embeddings for a dataset, storing them in Sqlite and Postgres, and performing connecting them to a local LLM, via a couple of extensions. Given that Sqlite and Postgres aren't really competing databases, this isn't going to be much of a comparison as much as a walkthrough of encoding and retrieving vector embeddings from SQL compatible databases. There's Sqlite-vec, and PG-Vector.

No love for mySql

I couldn't find an equivalent for mySQL, so if you are using mySQL, adding an secondary vector database appears to be your only straightforward option.

Of course, this isn't likely to be the case for long: nearest-neighbour search is a solved problem, it just needs to be implemented for mySQL. There's also been at least one stab at building one: MySQLvss. At the moment, this doesn't seem to be a maintained: the last commit was six months ago: perhaps it can provide a starting off point, should you decide to build your own mySQL nearest-neighbour search.

Fwiw, many cloud services, such as both Oracle and Google are offering vector search functionality as part of their managed mySQL services.

Sqlite: Sqlite-vec, -lembed & -rembed

Sqlite-vec is a new database extension learned about during the AI Engineer World's Fair keynote. It performs vector search allows the storage and retrieval of vector embeddings It seemed like something nice and shiny, and yet practical program to add to my RAG toolset.

Enabling extensions in sqlite can be less than straightforward, if you don't have easy access to the sqlite C api. Enabling extensions via python sqlite library requires recompiling python with sqlite feature flags enabled, or just using the package from Homebrew, which comes with the feature enabled.

SqLite-Vec comes peer extensions that allow the execution of embedding models locally, or from a model running on a server or 3rd party service. PGVector is just provides yjr search function, so we'll have to create the embeddings before we can use it. It's a small convenience, but I can see how it would be useful for programmatic generation of embeddings (such as providing an agent semantic search over their interactions with the user and other behaviours).

Additionally, SqLite-Vec is built with WASM, so this can power AI running in the browser, or on embedded devices.

Postgres: PG-Vector, PG-Vector-scale

PGVector provides a nearest-neighbour vector search PostGres. As such, I expect it the one I'll be reaching for more often. Additionally, it provide more sophisticated search algorithms than Sqlite: while Sqlite only implements cosine similarity, PG-Vector also search algorithms such as HNSW (Hierarchical Navigable Small World), IVFFlat (Inverted File Flat) and Euclidean distance.

PG-Vector-scale is a super fast iteration on PGVector, intended for really large deployments where the PGVector might hit performance or scale limitations. It is also addresses scalability for large datasets, distributed indexing and querying and handling billions of vectors with efficiency. As I write this, it's not clear whether there are circumstances where it makes sense to use PGVector at all, hopefully as I dig into it, it will become clear.

Ok, so, these are the tools I'm currently playing with. Next post, I'm going to get into implementing them and working with them.

rdbms Article's
30 articles in total
Favicon
Enabling Database Backup and Restore to S3 for SQL Server in AWS RDS: A Step-by-Step Guide
Favicon
Identifying and Resolving Blocking Sessions in Oracle Database
Favicon
How to Set Custom Status Bar Colors in SSMS to Differentiate Environments
Favicon
Auditing SQL Server Database Users, Logins, and Activity: A Comprehensive Guide
Favicon
Resumo de conceitos de bancos de dados relacionais
Favicon
Why Is MySQL the Best Database?
Favicon
SQL Server TempDB Common Issues and How to Handle Them Effectively
Favicon
Identifying Heavy Usage of TempDB In SQLSERVER and Monitoring It
Favicon
In-Memory Databases vs. Relational Databases: Key Advantages and Use Cases
Favicon
Advantages of Using a Relational Database Management System (RDBMS) Over Hierarchical and Network Models
Favicon
Understanding Database Relationships: A Deep Dive into Data Integrity
Favicon
SQL window functions with examples
Favicon
When a Traditional Relational Database Is More Suitable Than Blockchain: Key Scenarios
Favicon
Understanding Database Management Systems (DBMS): Definition and Functioning
Favicon
Why Choose a Relational Database Over NoSQL? A Detailed Analysis
Favicon
Implementing a RAG system inside an RDBMS: Sqlite and Postgres with Sqlite-vec, PGVector.
Favicon
PostgreSQL INTERVAL data type or ORM?
Favicon
Relational vs Non-Relational Databases: Key Differences You Need to Know
Favicon
Top 10 Affordable Options To Host Your PostgreSQL Database
Favicon
Exploring the Dynamics of Relational and Non-Relational Databases
Favicon
>1 RDBMS in Spring Data JPA
Favicon
Understanding Scalar Functions in SQL
Favicon
Level Up Your SQL Queries with Group By: Tips and Tricks
Favicon
Implementing nested loop joins in MySQL for performance
Favicon
Views, Materialized Views, and Spring Data JPA
Favicon
Level UP your RDBMS Productivity in GO
Favicon
Importance of Databases in Applications
Favicon
Importance of Databases in Applications
Favicon
How to Learn RDBMS Part 2 - Mastering SQL (Learning by doing)
Favicon
Redis: A Comparison with Other Databases (Bite-size Article)

Featured ones: