Skip to main content

๐ŸŽง DJ-Rag-Pipeline

FastAPI Python Pinecone Ragas Arize Phoenix UV

Production RAG Pipeline โ€” PDFs โ†’ Pinecone โ†’ LLM โ†’ Ragas (Sub-10s E2E)

One-command production RAG API for any PDF documents. Built on Clean Layered Architecture (SOLID Principles) with built-in Arize Phoenix Observability & OpenInference tracing.


๐Ÿš€ Key Features

Feature Status Description
Clean SOLID Architecture โœ… Layered Protocol-driven domain, adapters, and services with 100% dependency injection testability.
Phoenix Observability ๐Ÿ”ญ Built-in UI Live tracing dashboard at http://localhost:6006 with OpenInference conventions.
PDF Processing โœ… Docling High-accuracy incremental PDF-to-Markdown conversion.
Smart Chunking โœ… 2-Stage Markdown header splitting + recursive token character splitting.
Pinecone Records API โœ… Server-side Integrated embedding schema, hybrid search, and batch upserting.
Grounded LLM Generation โœ… Strict Context Zero hallucination prompt design with mandatory source citations.
Ragas Quality Evaluation ๐ŸŽฏ Automated 5 metrics (context_precision, recall, relevance, faithfulness, relevancy) with nested span tracing.
FastAPI + UV โšก High Performance Clean modular endpoints with UV package management on Python 3.13.

๐Ÿ“ฆ Install & Run (60 seconds)

# 1. Install
pip install dj-rag

# 2. Create project
dj-rag init my_rag_project

# 3. Setup & run
cd my_rag_project
# Edit .env with your credentials
uv sync
uv run uvicorn main:app --reload --port 8000
  • API Swagger Docs: http://localhost:8000/docs ๐ŸŽ‰
  • Phoenix Tracing UI: http://localhost:6006 ๐Ÿ”ฅ

๐Ÿ—๏ธ Project Structure (Clean Layered Architecture)

dj-rag init sets up the following SOLID architecture:

my_rag_project/
โ”œโ”€โ”€ main.py                         # ๐Ÿš€ FastAPI entrypoint & lifespan management (~75 lines)
โ”œโ”€โ”€ pyproject.toml
โ”œโ”€โ”€ env_example.txt
โ””โ”€โ”€ src/
    โ”œโ”€โ”€ domain/                     # ๐Ÿ›๏ธ Layer 1: Contracts & Models (Zero external deps)
    โ”‚   โ”œโ”€โ”€ contracts.py            # Protocols: BaseRetriever, BaseChatbot, BaseEvaluator, BaseDocConverter
    โ”‚   โ””โ”€โ”€ base_models.py          # Unified DTOs: DocumentChunk, RetrievalResult, PipelineResult
    โ”œโ”€โ”€ infrastructure/             # ๐Ÿ”Œ Layer 2: Vendor Adapters
    โ”‚   โ”œโ”€โ”€ vector_stores/          # PineconeRetriever (implements BaseRetriever)
    โ”‚   โ”œโ”€โ”€ llm/                    # RAGChatbot (implements BaseChatbot)
    โ”‚   โ”œโ”€โ”€ evaluators/             # RagasEvaluator with OTel spans (implements BaseEvaluator)
    โ”‚   โ””โ”€โ”€ doc_parsers/            # DoclingPDFConverter (implements BaseDocConverter)
    โ”œโ”€โ”€ services/                   # โš™๏ธ Layer 3: Application Workflows
    โ”‚   โ”œโ”€โ”€ rag_service.py          # Coordinates Query -> Retrieve -> Generate
    โ”‚   โ”œโ”€โ”€ ingestion_service.py    # Coordinates PDF Convert -> Chunk -> Batch Upsert
    โ”‚   โ””โ”€โ”€ check_pincone_index.py  # Pinecone index inspection & chunk statistics
    โ”œโ”€โ”€ api/                        # ๐ŸŒ Layer 4: HTTP Transport
    โ”‚   โ”œโ”€โ”€ routes/v1.py            # Route handlers: /chat, /evaluate-ragas, /full-pipeline
    โ”‚   โ””โ”€โ”€ dependencies.py         # FastAPI dependency injection providers (Depends())
    โ”œโ”€โ”€ core/                       # ๐Ÿงฉ Cross-cutting concerns (config.py, tracing.py)
    โ”œโ”€โ”€ embeddings/                 # โšก Cached global embedding manager
    โ””โ”€โ”€ data/                       # ๐Ÿ“‚ PDF storage (data_source/) and Markdown (markdown_data_sources/)

๐Ÿ”ญ Arize Phoenix Observability (http://localhost:6006)

Whenever your server starts, an in-process Arize Phoenix UI launches automatically:

  • Dashboard URL: http://localhost:6006/
  • Collector Endpoint: http://localhost:6006/v1/traces
  • Tracing Conventions: OpenInference (CHAIN, LLM, EVALUATOR)
  • Self-Tracing Muted: Phoenix's internal /graphql server polling is suppressed to keep traces and console logs clean.
  • Bulk Ingestion Suppression: Bulk embedding calls during /full-pipeline are muted via suppress_tracing() to avoid trace flooding.

๐Ÿ“Š What You Can Check on the Dashboard

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚  ARIZE PHOENIX DASHBOARD (http://localhost:6006)                       โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ ๐Ÿ“ Traces       โ”‚ Chronological list of every /chat & /evaluate requestโ”‚
โ”‚ ๐Ÿ” Spans        โ”‚ Search & filter by kind: LLM, CHAIN, EVALUATOR       โ”‚
โ”‚ ๐ŸŽฏ Evaluations  โ”‚ Quality metrics: Faithfulness, Precision, Recall     โ”‚
โ”‚ โšก Latency & $  โ”‚ Token usage, estimated cost, and latency percentiles โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Trace Waterfall Breakdown:

  • /chat Execution Tree:
    โ–ผ rag_chat_generation (CHAIN) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ [482ms]
        โ”‚  โ”œโ”€ Input: User query + retrieved document contexts
        โ”‚  โ””โ”€ Output: Generated grounded answer
        โ”‚
        โ””โ”€โ–บ ChatOpenAI / Groq (LLM) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ [465ms]
              โ”œโ”€ Model: openai/gpt-oss-120b | Temperature: 0.1
              โ”œโ”€ Tokens: Prompt: 1,240 | Completion: 85 | Total: 1,325
              โ””โ”€ System Prompt: Strict "context-only" instructions
    
  • /evaluate-ragas Execution Tree:
    โ–ผ ragas_evaluation (EVALUATOR) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ [3.1s]
        โ”‚  Attributes: eval.context_precision: 1.0 | eval.faithfulness: 0.95 | ...
        โ”‚
        โ”œโ”€โ–บ ragas_context_precision (EVALUATOR) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ [620ms] -> LLM prompt & score
        โ”œโ”€โ–บ ragas_context_recall (EVALUATOR) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ [580ms] -> LLM prompt & score
        โ”œโ”€โ–บ ragas_context_relevance (EVALUATOR) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ [510ms] -> LLM prompt & score
        โ”œโ”€โ–บ ragas_faithfulness (EVALUATOR) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ [710ms] -> Hallucination detection
        โ””โ”€โ–บ ragas_answer_relevancy (EVALUATOR) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ [680ms] -> Answer relevancy
    

๐Ÿ› ๏ธ Debugging Workflows:

  • Detecting Hallucinations: When faithfulness < 0.90, expand ragas_faithfulness $\rightarrow$ inspect the judge LLM's explanation of ungrounded statements.
  • Verifying Retrieval: Inspect the rag_chat_generation span input to verify chunk rank order.
  • Tracking Costs & Latency: Monitor token usage, cost estimations, and $p50/p90/p99$ latencies.

๐ŸŽฏ Upload & Query PDFs (API-First)

1. Upload + Index PDFs (One Command)

curl -X POST "http://localhost:8000/full-pipeline" \
  -F "files=@yoga-guide.pdf" \
  -F "files=@asana-manual.pdf"

2. Query Instantly!

curl -X POST "http://localhost:8000/chat" \
  -H "Content-Type: application/json" \
  -d '{"query": "What are the primary benefits of pranayama?", "top_k": 5}'

Response:

{
  "success": true,
  "data": {
    "answer": "Based on the provided context: Pranayama improves lung capacity and calms the nervous system... [yoga-guide.md | Chunk: 2]",
    "sources": [
      {
        "id": "yoga-guide_2",
        "text": "Pranayama practices improve respiratory stamina...",
        "source": "yoga-guide.md",
        "score": 0.912
      }
    ],
    "retrieval_metrics": {
      "retrieved_count": 5,
      "latency_seconds": 0.234,
      "avg_score": 0.891
    }
  }
}

3. Evaluate Answer Quality (Ragas)

curl -X POST "http://localhost:8000/evaluate-ragas" \
  -H "Content-Type: application/json" \
  -d '{
    "question": "What are the primary benefits of pranayama?",
    "answer": "Pranayama improves lung capacity and calms the nervous system.",
    "contexts": ["Pranayama practices improve respiratory stamina and regulate stress."]
  }'

๐ŸŒ API Endpoints

Endpoint Method Purpose Latency
/chat POST Core RAG (Retrieve $\rightarrow$ Generate) ~500ms โšก
/full-pipeline POST PDFs $\rightarrow$ Markdown $\rightarrow$ Pinecone ~30s ๐Ÿญ
/evaluate-ragas POST 5 Ragas Quality Metrics ~3s ๐ŸŽฏ
/index-status GET Vector Store Health & Chunk Counts ~50ms ๐Ÿ“Š
/health GET API Status & Version ~5ms โœ…

โš™๏ธ Environment Configuration (.env)

PINECONE_API_KEY=your_pinecone_api_key
PINECONE_INDEX_NAME=your_index_name
PINECONE_INDEX_HOST=
PINECONE_NAMESPACE=__default__
PINECONE_TEXT_FIELD=text
PINECONE_EMBED_MODEL=llama-text-embed-v2

EMBEDDING_MODEL=nomic-ai/nomic-embed-text-v1.5
MAX_CHUNK_SIZE=1500
CHUNK_OVERLAP=400

LLM_MODEL=openai/gpt-oss-120b
LLM_BASE_URL=https://api.groq.com/openai/v1
LLM_PROVIDER=openai
LLM_MAX_TOKENS=16384
LLM_API_KEY=your_llm_api_key

PHONIEX_PORT=6006
PHONIEX_HOST=0.0.0.0
PHOENIX_ENABLE_AUTH=false

๐Ÿ“ˆ Production Targets

Metric Target Description
Retrieval Latency < 500ms Vector search duration in Pinecone
Context Precision > 0.90 Most relevant chunks prioritized first
Faithfulness > 0.90 Zero hallucination score against retrieved docs
Answer Relevancy > 0.85 Response directness to user's question

๐Ÿ“ License

Distributed under the MIT License.
Made with โค๏ธ by DJ ๐ŸŽง

Metadata

Release files for dj-rag 2.0.4

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

Source distribution (sdist)

Source distribution for dj-rag 2.0.4
File Size Uploaded Provenance Actions
dj_rag-2.0.4.tar.gz 33.2 kB Without provenance Details for dj_rag-2.0.4.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dj-rag 2.0.4
File Interpreter ABI Platform Actions
dj_rag-2.0.4-py3-none-any.whl Python 3 none any Details for dj_rag-2.0.4-py3-none-any.whl

Total release size: 82.0 kB

Release files / dj_rag-2.0.4.tar.gz

Download URL dj_rag-2.0.4.tar.gz
Size 33.2 kB
Tags Source
SHA-256 checksum
How to use checksums
d41bd510bd253b6b41e70df45d677cce855cf4bc4cb91224ea09ab92d68a057a
BLAKE2b-256 checksum
How to use checksums
814d566b92c0b39b040c9f8dd53e440468c64281c0a7526458c8372052b7dc77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.8

Release files / dj_rag-2.0.4-py3-none-any.whl

Download URL dj_rag-2.0.4-py3-none-any.whl
Size 48.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
63fec607348d4c36c818e6ec6062462c67b5aa7c768ac34bb030dcc6e4d79dbc
BLAKE2b-256 checksum
How to use checksums
48fda8b2d6e2e97438ffacbd91a881d17c0304578c7c9ba9a14b8376d2c213b9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.7.8

Security for dj-rag 2.0.4

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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