Skip to main content

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

  1. Install PostgreSQL with PGVector extension
  2. Create your database and enable the vector extension:
CREATE EXTENSION IF NOT EXISTS vector;
  1. 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 creation
  • DatabaseManager: Database connection and session management
  • DocumentDatabaseManager: High-level document operations

Key Methods

  • BaseDocument.from_props(): Create document instances from properties
  • DocumentDatabaseManager.insert_document(): Store documents
  • DocumentDatabaseManager.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

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes with tests
  4. Run the test suite
  5. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pgvector_template-0.3.0.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pgvector_template-0.3.0-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file pgvector_template-0.3.0.tar.gz.

File metadata

  • Download URL: pgvector_template-0.3.0.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.10

File hashes

Hashes for pgvector_template-0.3.0.tar.gz
Algorithm Hash digest
SHA256 f2683924aba64a2bdc4427eb3bafc19372d90d660d8bf3ed7789e89b8bc73908
MD5 bc9c1ab2134e3aa7ed4288544048d561
BLAKE2b-256 845a3c3e5b5e1e210f788028795ff890259c66a45bda57b8f259afb32af568ba

See more details on using hashes here.

File details

Details for the file pgvector_template-0.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pgvector_template-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4f44b53bd605c89155371f7fec29c5f1ef2ec6f727c8d948fdbfba33df8137f1
MD5 a017664628ae9e63a4b75f27cba8ad3e
BLAKE2b-256 c6126bc30e714b38d2cac4c98a6ac4ed8e1ca8ebc764eb250502ce4265741be7

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page