Skip to main content

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

import asyncio
from docs2db_api.rag.engine import UniversalRAGEngine, RAGConfig

async def main():
    # 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"Score: {doc['similarity_score']:.3f}")
        print(f"Source: {doc['document_path']}")
        print(f"Text: {doc['text'][:200]}...\n")

asyncio.run(main())

MCP Integration (LlamaStack, Claude, Cursor, etc.)

For tool-calling RAG integration with LLM frameworks, use docs2db-mcp-server — an MCP server that exposes search_documents over the standard Model Context Protocol.

Any framework that supports MCP can use it directly:

pip install docs2db-mcp-server
docs2db-mcp --db-host localhost --db-port 5432 --db-name ragdb

LlamaStack example (Responses API):

response = client.responses.create(
    model="ollama/qwen2.5:7b-instruct",
    input="How do I configure SSH key-based authentication?",
    tools=[{
        "type": "mcp",
        "server_url": "http://localhost:8000",  # docs2db-mcp-server
    }],
)

See the docs2db-mcp-server README for full setup and configuration.

Configuration

Database Configuration

Configuration precedence (highest to lowest):

  1. CLI arguments: --host, --port, --db, --user, --password
  2. Environment variables: DOCS2DB_DB_HOST, DOCS2DB_DB_PORT, DOCS2DB_DB_DATABASE, DOCS2DB_DB_USER, DOCS2DB_DB_PASSWORD
  3. DOCS2DB_DB_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 DOCS2DB_DB_HOST=prod.example.com
export DOCS2DB_DB_DATABASE=mydb
uv run docs2db-api db-status

# DOCS2DB_DB_URL (cloud providers)
export DOCS2DB_DB_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 DOCS2DB_DB_URL with individual DOCS2DB_DB_* variables.

LLM Configuration (Query Refinement)

Configure the LLM used for query refinement:

export DOCS2DB_LLM_BASE_URL=http://localhost:11434      # OpenAI-compatible API (e.g., Ollama)
export DOCS2DB_LLM_MODEL=qwen2.5:7b-instruct            # Model name
export DOCS2DB_LLM_TIMEOUT=30.0                         # HTTP timeout (seconds)
export DOCS2DB_LLM_TEMPERATURE=0.7                      # Generation temperature
export DOCS2DB_LLM_MAX_TOKENS=500                       # Max tokens per response

Embedding Configuration

export DOCS2DB_OFFLINE=true    # Only use locally cached embedding model (no downloads)

By default, the embedding model is downloaded automatically on first use. Set DOCS2DB_OFFLINE=true for airgapped/offline environments where the model must already be cached.

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.

Metadata

Release files for docs2db-api 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for docs2db-api 0.3.2
File Size Uploaded
docs2db_api-0.3.2.tar.gz 31.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for docs2db-api 0.3.2
File Interpreter ABI Platform
docs2db_api-0.3.2-py3-none-any.whl Python 3 none any Details

Total release size: 66.7 kB

Release files / docs2db_api-0.3.2.tar.gz

Download URL docs2db_api-0.3.2.tar.gz
Size 31.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a379f9de0de78370d138ede36fb5cc19a9e9ca14e0b819aa6a96a78f5d7889c9
BLAKE2b-256 checksum
How to use checksums
bc815c008e02d5f006c89ea19290bffd2a0a386513f1e804cb9766ca0e02c6cd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.9 {"installer":{"name":"uv","version":"0.11.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / docs2db_api-0.3.2-py3-none-any.whl

Download URL docs2db_api-0.3.2-py3-none-any.whl
Size 35.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
25195195954841500c8d4ef2840a77d9a8fa51e1ce63e10dd7195fa3396cf21c
BLAKE2b-256 checksum
How to use checksums
e5ab3949f4a5e2527f9c7f83c8013fdfeae281743c570a1464814fc63321d8c6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.9 {"installer":{"name":"uv","version":"0.11.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.3.2 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page