Template library for flexible PGVector RAG implementations
Project description
PGVector-Template
A flexible, production-ready template library for building Retrieval-Augmented Generation (RAG) applications using PostgreSQL with PGVector extensions.
Overview
PGVector-Template provides a robust foundation for implementing vector-based document storage and retrieval systems. It offers a clean abstraction layer over PostgreSQL's PGVector extension, making it easy to build scalable RAG applications with proper document management, metadata handling, and efficient vector search capabilities.
Key Features
- Flexible Document Model: Abstract base classes for customizable document schemas
- Vector Search: Optimized HNSW indexing for fast similarity search
- Metadata Management: JSON-based flexible metadata with GIN indexing
- Collection Support: Organize documents into logical collections
- Chunk Management: Handle long content (refer to as corpus) by chunking it into smaller documents. Handle recovering the original corpus given its id
- Database Abstraction: Clean SQLAlchemy-based database layer, with an API to create schemas
- Type Safety: Full Pydantic validation and type hints
- Production Ready: Comprehensive testing and error handling
Architecture
The library is organized into several key components:
- Core: Document models, embedders, search functionality
- Database: Connection management and document database operations
- Service: High-level document service layer
- Types: Shared type definitions and schemas
Installation
pip install pgvector-template
Or add pgvector-template to your dependencies
Prerequisites
- Python 3.11+
- To execute tests: PostgreSQL with PGVector extension
Configuration
Database Setup
- Install PostgreSQL with PGVector extension
- Create your database and enable the vector extension:
CREATE EXTENSION IF NOT EXISTS vector;
- Set up your connection string in environment variables or pass directly to
DatabaseManager
Environment Variables
For integration tests, create a .env file
cp integ-tests/.env.example integ-tests/.env
Specify envvars directly in the .env file. It is loaded automatically for integ tests.
DATABASE_URL=postgresql://user:password@localhost:5432/test_db
API Reference
Core Classes
BaseDocument: Abstract document model with vector embedding support- refer to table schema for explanation of the fields
BaseDocumentOptionalProps: Optional properties for document creationDatabaseManager: Database connection and session managementDocumentDatabaseManager: High-level document operations
Key Methods
BaseDocument.from_props(): Create document instances from propertiesDocumentDatabaseManager.insert_document(): Store documentsDocumentDatabaseManager.search_similar(): Vector similarity search
Testing
Install dependencies (preferably in a virtualenv) before running tests:
pip install -e .[test]
Unit Tests
python -m unittest
Integration Tests
Integration tests require a PostgreSQL database with PGVector extension. Set up your test database and configure the connection in integ-tests/.env:
python -m unittest discover -s integ-tests
Contributing
- Fork the repository
- Create a feature branch
- Make your changes with tests
- Run the test suite
- Submit a pull request
Development Setup
pip install -e .[dev,test]
black . # Format code
License
MIT License - see LICENSE file for details.
Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pgvector_template-0.3.2.tar.gz.
File metadata
- Download URL: pgvector_template-0.3.2.tar.gz
- Upload date:
- Size: 18.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91bdc5110b3a7371d011a1628e0744016d83b8759602b754823e9fe5541fa884
|
|
| MD5 |
1bb278fe923be162bdaf7da286d56a51
|
|
| BLAKE2b-256 |
ea3148d06ed5176e92c488a53b16f8f5e10736a1aa469d32dedaaa729fe3a1c2
|
File details
Details for the file pgvector_template-0.3.2-py3-none-any.whl.
File metadata
- Download URL: pgvector_template-0.3.2-py3-none-any.whl
- Upload date:
- Size: 21.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.8.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e3f289374404627f86a0e1fbb3dd74dc35af03d6168684f12eebd3764c3f72f
|
|
| MD5 |
d67143ae2fd2115153e5e73d3616b9d3
|
|
| BLAKE2b-256 |
9bfd651a0e607c3c07edffa0187a653b836274b35b5f475c0e07a99ca7f8a9d6
|