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.
Quick Start
from pgvector_template import DatabaseManager, DocumentDatabaseManager
from pgvector_template.core.document import BaseDocument
from sqlalchemy import create_engine
from uuid import uuid4
# 1. Define your document model
class MyDocument(BaseDocument):
__tablename__ = "my_documents"
# 2. Set up database connection
engine = create_engine("postgresql://user:pass@localhost/mydb")
db_manager = DatabaseManager(engine)
db_manager.create_tables([MyDocument])
# 3. Create document manager
doc_manager = DocumentDatabaseManager(db_manager)
# 4. Insert a document
with db_manager.get_session() as session:
doc = MyDocument.from_props(
corpus_id=uuid4(),
chunk_index=0,
content="Your document content here",
embedding=[0.1, 0.2, 0.3, ...], # Your embedding vector
)
doc_manager.insert_document(session, doc)
# 5. Search similar documents
with db_manager.get_session() as session:
results = doc_manager.search_similar(
session, MyDocument, query_embedding=[0.1, 0.2, 0.3, ...], limit=5
)
Key Concepts
Corpus vs Document: Understanding the hierarchy is essential:
- Corpus: A complete source document (e.g., a full PDF, article, or book)
- Document: A chunk or segment of a corpus that fits within embedding limits
- Collection: A logical grouping of related corpora (e.g., "legal_docs", "user_manuals")
Example: A 50-page PDF (corpus) might be split into 200 documents (chunks), all sharing the same corpus_id but with different chunk_index values.
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 by chunking into retrievable documents
- Database Abstraction: Clean SQLAlchemy-based database layer with schema creation API
- 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
Basic Installation
pip install pgvector-template
With Database Driver
For production use, you'll also need a PostgreSQL driver:
# For binary driver (recommended)
pip install pgvector-template psycopg[binary]
# Or for source driver
pip install pgvector-template psycopg
Prerequisites
- Python 3.11+
- PostgreSQL 12+ with PGVector extension
- For development: Additional test dependencies
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:
from sqlalchemy import create_engine
from pgvector_template import DatabaseManager
# Option 1: Direct connection string
engine = create_engine("postgresql://user:password@localhost:5432/mydb")
db_manager = DatabaseManager(engine)
# Option 2: From environment variable
import os
engine = create_engine(os.getenv("DATABASE_URL"))
db_manager = DatabaseManager(engine)
Production Configuration
from sqlalchemy import create_engine
from sqlalchemy.pool import QueuePool
engine = create_engine(
"postgresql://user:password@localhost:5432/mydb",
poolclass=QueuePool,
pool_size=10,
max_overflow=20,
pool_pre_ping=True,
)
Environment Variables
# Development
DATABASE_URL=postgresql://user:password@localhost:5432/dev_db
Usage Examples
1. Define Your Document Model
from pgvector_template.core.document import BaseDocument, BaseDocumentMetadata
from pydantic import Field
class MyDocumentMetadata(BaseDocumentMetadata):
source_type: str = Field(..., description="Type of source document")
author: str = Field(default="unknown", description="Document author")
class MyDocument(BaseDocument):
__tablename__ = "my_documents"
2. Insert Documents
from pgvector_template.core.document import BaseDocumentOptionalProps
from uuid import uuid4
# Create document with metadata
metadata = MyDocumentMetadata(source_type="pdf", author="John Doe")
optional_props = BaseDocumentOptionalProps(
title="Chapter 1: Introduction",
collection="textbooks",
language="en",
tags=["education", "intro"]
)
doc = MyDocument.from_props(
corpus_id=uuid4(),
chunk_index=0,
content="This is the document content...",
embedding=your_embedding_vector, # list[float] with 1024 dimensions
metadata=metadata.to_dict(),
optional_props=optional_props
)
with db_manager.get_session() as session:
doc_manager.insert_document(session, doc)
3. Search Documents
# Basic similarity search
with db_manager.get_session() as session:
results = doc_manager.search_similar(
session=session,
document_cls=MyDocument,
query_embedding=query_vector,
limit=10
)
# Search with filters
from pgvector_template.models.search import MetadataFilter
filters = [
MetadataFilter(key="source_type", value="pdf"),
MetadataFilter(key="author", value="John Doe")
]
results = doc_manager.search_similar(
session=session,
document_cls=MyDocument,
query_embedding=query_vector,
limit=10,
metadata_filters=filters,
collection="textbooks"
)
4. Manage Collections
# Get all documents in a collection
with db_manager.get_session() as session:
docs = doc_manager.get_documents_by_collection(
session, MyDocument, "textbooks"
)
# Get all chunks from a corpus
corpus_docs = doc_manager.get_documents_by_corpus_id(
session, MyDocument, corpus_id
)
Concept Reference
Core Classes
BaseDocument: Abstract SQLAlchemy model for documents with vector embeddingsBaseDocumentOptionalProps: Pydantic model for optional document propertiesBaseDocumentMetadata: Base schema for structured document metadataDatabaseManager: Database connection and session managementDocumentDatabaseManager: High-level document CRUD operations
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.3.tar.gz.
File metadata
- Download URL: pgvector_template-0.3.3.tar.gz
- Upload date:
- Size: 20.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ba595c92c80b6c905b0905cddce40053d79b6d8de0ff11754513b57e4cf2bdd
|
|
| MD5 |
546a35b1eec485b99093b0a3a227eb61
|
|
| BLAKE2b-256 |
e4734a23b0820b7aeb19506c9756c8d4816a75954900928bee61cd1fa7477c5d
|
File details
Details for the file pgvector_template-0.3.3-py3-none-any.whl.
File metadata
- Download URL: pgvector_template-0.3.3-py3-none-any.whl
- Upload date:
- Size: 22.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4251feebb55fc4885b7f75bbb1f22a320c4b21c993c42d6dd66a6ea5d0fd2a63
|
|
| MD5 |
620d347d0f91f422aa2d078fbb56d2a2
|
|
| BLAKE2b-256 |
eaa55ecc689b52b29c271c40936f57bcfd51931c815d9f6ea170965052728f57
|