Skip to main content

Query Docs2DB RAG databases with hybrid search and reranking

Project description

Docs2DB-API

Query a Docs2DB RAG database with modern retrieval techniques. Docs2DB-API provides a Python library for hybrid search (vector + BM25) with reranking.

What it does:

  • Queries RAG databases created by docs2db
  • Hybrid search: combines vector similarity with BM25 full-text search
  • Reciprocal Rank Fusion (RRF) for result combination
  • Cross-encoder reranking for improved result quality
  • Question refinement for query expansion
  • Universal RAG engine adaptable to multiple API frameworks

What it's for:

  • Building RAG applications and agents
  • Adding document search to LLM systems
  • Serving RAG APIs (FastAPI, LlamaStack, custom frameworks)

Installation

uv add docs2db-api

Quickstart

Step 1: Create a database with docs2db

uv tool install docs2db
docs2db pipeline /path/to/documents

This creates ragdb_dump.sql.

Step 2: Restore and query

# Start database
uv run docs2db-api db-start

# Restore dump
uv run docs2db-api db-restore ragdb_dump.sql

# Check status
uv run docs2db-api db-status

Step 3: Use in your application

from docs2db_api.rag.engine import UniversalRAGEngine, RAGConfig

# Initialize engine with defaults (auto-detects database from environment)
engine = UniversalRAGEngine()
await engine.start()

# Or with specific settings
config = RAGConfig(
    model_name="granite-30m-english",
    max_chunks=5,
    similarity_threshold=0.7
)
db_config = {
    "host": "localhost",
    "port": "5432",
    "database": "ragdb",
    "user": "postgres",
    "password": "postgres"
}
engine = UniversalRAGEngine(config=config, db_config=db_config)
await engine.start()

# Search
result = await engine.search_documents("How do I configure authentication?")
for doc in result.documents:
    print(f"{doc['title']}: {doc['content'][:100]}...")

LlamaStack Integration

Docs2DB-API includes a native LlamaStack tool provider for agent-based RAG. See the complete demo with setup scripts and examples:

📁 demos/llama-stack/ - LlamaStack RAG tool provider with agent demos

(this needs to be adjusted to work from pypi)

Database Configuration

** Database Configuration precedence (highest to lowest):**

  1. CLI arguments: --host, --port, --db, --user, --password
  2. Environment variables: POSTGRES_HOST, POSTGRES_PORT, POSTGRES_DB, POSTGRES_USER, POSTGRES_PASSWORD
  3. DATABASE_URL: postgresql://user:pass@host:port/database
  4. postgres-compose.yml in current directory
  5. Defaults: localhost:5432, user=postgres, password=postgres, db=ragdb

Examples:

# Use defaults
uv run docs2db-api db-status

# Environment variables
export POSTGRES_HOST=prod.example.com
export POSTGRES_DB=mydb
uv run docs2db-api db-status

# DATABASE_URL (cloud providers)
export DATABASE_URL="postgresql://user:pass@host:5432/db"
uv run docs2db-api db-status

# CLI arguments
uv run docs2db-api db-status --host localhost --db mydb

Note: Don't mix DATABASE_URL with individual POSTGRES_* variables.

RAG Configuration

RAG settings control retrieval behavior (similarity thresholds, reranking, refinement, etc.) and can be stored in the database or provided at query time.

Available Settings

  • refinement_prompt - Custom prompt for query refinement
  • enable_refinement (refinement) - Enable question refinement (true/false)
  • enable_reranking (reranking) - Enable cross-encoder reranking (true/false)
  • similarity_threshold - Similarity threshold 0.0-1.0
  • max_chunks - Maximum chunks to return
  • max_tokens_in_context - Maximum tokens in context window
  • refinement_questions_count - Number of refined questions to generate

Configuration Precedence (highest to lowest)

  1. Query parameters - Passed directly to engine.search_documents() or CLI --threshold, --limit, etc.
  2. RAGConfig object - Provided when initializing UniversalRAGEngine
  3. Database settings - Stored in database via docs2db config command (see docs2db)
  4. Code defaults - Built-in fallback values

Commands

Database Management

docs2db-api db-start               # Start PostgreSQL with Podman/Docker
docs2db-api db-stop                # Stop PostgreSQL (data preserved)
docs2db-api db-destroy             # Stop and delete all data
docs2db-api db-status              # Check connection and stats
docs2db-api db-restore <file>      # Restore database from dump
docs2db-api manifest               # Generate list of documents

Querying

# Basic search
docs2db-api query "How do I configure authentication?"

# Advanced options
docs2db-api query "deployment guide" \
  --model granite-30m-english \
  --limit 20 \
  --threshold 0.8 \
  --no-refine                     # Disable question refinement

RAG Features

Docs2DB-API implements modern retrieval techniques:

  • Contextual chunks - LLM-generated context situating each chunk within its document (Anthropic's approach)
  • Hybrid search - Combines BM25 (lexical) and vector embeddings (semantic)
  • Reciprocal Rank Fusion (RRF) - Intelligent result combination
  • Cross-encoder reranking - Improved result quality
  • Question refinement - Query expansion for better matches
  • PostgreSQL full-text search - tsvector with GIN indexing for BM25
  • pgvector similarity - Fast vector search with HNSW indexes
  • Universal RAG engine - Adaptable to multiple API frameworks

License

See LICENSE for details.

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

docs2db_api-0.1.0.tar.gz (30.1 kB view details)

Uploaded Source

Built Distribution

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

docs2db_api-0.1.0-py3-none-any.whl (34.5 kB view details)

Uploaded Python 3

File details

Details for the file docs2db_api-0.1.0.tar.gz.

File metadata

  • Download URL: docs2db_api-0.1.0.tar.gz
  • Upload date:
  • Size: 30.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.11

File hashes

Hashes for docs2db_api-0.1.0.tar.gz
Algorithm Hash digest
SHA256 569ae04c62c38182bba422c9015d29e55979e77031f8707ab99e60d270b551d0
MD5 6136d8f2d22a90d15c9b724d843e033f
BLAKE2b-256 8d48384796c955a39d35d7629434ae41512b6b17441f1f8be10803e4fa5f2ac1

See more details on using hashes here.

File details

Details for the file docs2db_api-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for docs2db_api-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8d650cf7c419d54713ae89b9084214dde608a57872f2fa8e4cbabcbc68ab8875
MD5 859f6e1729fc8e9d205b7def2053696a
BLAKE2b-256 d89cb7c3a94e168b49ca449b15de9b8454da9b5c9797f51d94344613e3460070

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