High-Performance RAG Solution using pgvectorscale with Docker and Python, leveraging OpenAI's powerful text-embedding-3-small model for embeddings. Perfect for AI engineers looking to enhance their projects with state-of-the-art vector search and generation capabilities with the power of PostgreSQL.
Using PostgreSQL with pgvectorscale as your vector database offers several key advantages over dedicated vector databases:
-
PostgreSQL is a robust, open-source database with a rich ecosystem of tools, drivers, and connectors. This ensures transparency, community support, and continuous improvements.
-
By using PostgreSQL, you can manage both your relational and vector data within a single database. This reduces operational complexity, as there's no need to maintain and synchronize multiple databases.
-
Pgvectorscale enhances pgvector with faster search capabilities, higher recall, and efficient time-based filtering. It leverages advanced indexing techniques, such as the DiskANN-inspired index, to significantly speed up Approximate Nearest Neighbor (ANN) searches.
Pgvectorscale Vector builds on top of pgvector, offering improved performance and additional features, making PostgreSQL a powerful and versatile choice for AI applications.
- Docker
- Python 3.7+
- OpenAI API key
- PostgreSQL GUI client
- Set up Docker environment
- Connect to the database using a PostgreSQL GUI client (I use TablePlus)
- Create a Python script to insert document chunks as vectors using OpenAI embeddings
- Create a Python function to perform similarity search
- Create a copy of
example.envand rename it to.env - Open
.envand fill in your OpenAI API key. Leave the database settings as is - Run the Docker container
- Install the required Python packages using
pip install -r requirements.txt - Execute
insert_vectors.pyto populate the database - Play with
similarity_search.pyto perform similarity searches
Cosine similarity measures the cosine of the angle between two vectors in a multi-dimensional space. It's a measure of orientation rather than magnitude.
- Range: -1 to 1 (for normalized vectors, which is typical in text embeddings)
- 1: Vectors point in the same direction (most similar)
- 0: Vectors are orthogonal (unrelated)
- -1: Vectors point in opposite directions (most dissimilar)
In pgvector, the <=> operator computes cosine distance, which is 1 - cosine similarity.
- Range: 0 to 2
- 0: Identical vectors (most similar)
- 1: Orthogonal vectors
- 2: Opposite vectors (most dissimilar)
When you get results from similarity_search:
- Lower distance values indicate higher similarity.
- A distance of 0 would mean exact match (rarely happens with embeddings).
- Distances closer to 0 indicate high similarity.
- Distances around 1 suggest little to no similarity.
- Distances approaching 2 indicate opposite meanings (rare in practice).