# kazkozdev/production_rag_pipeline architecture

> Architecture diagram of the GitHub repository kazkozdev/production_rag_pipeline, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-05-26). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kazkozdev/production_rag_pipeline
- Repository: https://github.com/kazkozdev/production_rag_pipeline

## Overview

1. **What this repository is**
   This is a Python 3.8+ library for retrieval-augmented generation, with a small CLI on top. Its main job is to turn a user query into a grounded, citation-ready prompt for a local or self-hosted LLM. The architecture is library-first: the CLI and macOS launcher are thin entry points around the same pipeline.

2. **End-to-end data flow**
   The core flow is: query in → search engines return candidate URLs → irrelevant results are filtered before download → pages are fetched in parallel → main content is extracted and chunked → chunks are reranked → a final LLM prompt/context bundle is assembled. The output is not an answer; it is a structured prompt plus source references that the downstream LLM can use.

3. **Search subsystem**
   `production_rag_pipeline.search` handles web discovery via Bing and DuckDuckGo, merges results from both engines, and applies language-aware filtering. The design uses multiple engines to balance coverage and resilience, and it can switch DuckDuckGo into news mode for news-like queries. This is the first major boundary where external data enters the system.

4. **Fetch subsystem**
   `production_rag_pipeline.fetch` is responsible for HTTP retrieval, retries, timeouts, and parallel page downloading. The readme’s default behavior indicates a bounded worker pool and timeout-based fetch stage, which is important for diagramming because it separates “candidate selection” from “expensive network I/O.”

5. **Extraction and normalization**
   `production_rag_pipeline.extract` converts raw HTML into usable content, parses publish dates, filters boilerplate/noise, and chunks text into smaller units. It prefers richer extraction when optional dependencies are present, but can fall back to BeautifulSoup-based parsing. This stage is where raw page content becomes structured, LLM-ready evidence.

6. **Relevance scoring and reranking**
   `production_rag_pipeline.rerank` is the ranking brain of the repo. It combines lexical ranking (BM25), semantic similarity, answer-span detection, diversity control via MMR, and optional cross-encoder reranking. The readme also describes freshness penalties for news-like queries, so ranking is not purely semantic; it blends relevance, diversity, and recency signals.

7. **Confidence and quality signals**
   `production_rag_pipeline.confidence` likely centralizes the scoring/quality signals used to adjust source confidence and freshness. In the architecture, this is a supporting subsystem rather than a standalone stage: it feeds ranking, source selection, and prompt warnings, especially for time-sensitive or low-quality results.

8. **Prompt and context assembly**
   `production_rag_pipeline.pipeline` orchestrates the full sequence and produces the final context bundle, while `production_rag_pipeline.prompts` turns that bundle into the actual LLM prompt. The prompt includes instructions, current date/time, source numbering, citations, and quality/freshness notes. A key boundary here is that source renumbering and source-to-chunk mapping are preserved so citations in the prompt match real inputs.

9. **Configuration and runtime state**
   `production_rag_pipeline.config` exposes `RAGConfig`, with configuration coming from dataclasses, YAML, or `RAG_`-prefixed environment variables. `production_rag_pipeline.core` holds runtime settings and optional dependency state, which lets the pipeline adapt based on what is installed at runtime. This makes configuration a first-class architectural boundary rather than a hardcoded constant set.

10. **Graceful degradation**
   The package is intentionally designed to work in tiers. Without semantic models, it still runs with lexical methods; without `trafilatura`, it falls back to simpler extraction; without extra installs, the base library still produces usable prompts. That means the architecture should be drawn as optional capability layers around a stable core pipeline.

11. **Interfaces and delivery**
   `production_rag_pipeline.cli` exposes the user-facing command-line interface, including search/read modes and config loading. `run_llm_query.command` is a convenience launcher for macOS that bootstraps a local virtual environment. CI and release workflows live under `.github/workflows`, and tests cover config, CLI, pipeline, and integration behavior, which suggests the repo is shipped as a standard Python package with automated verification and release automation.

Main entry points: [`src/production_rag_pipeline/pipeline.py`](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/pipeline.py) (Pipeline), [`src/production_rag_pipeline/rerank.py`](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/rerank.py) (Rerank), [`src/production_rag_pipeline/config.py`](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/config.py) (Config), [`src/production_rag_pipeline/core.py`](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/core.py) (Runtime)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entrypoints["Entry points"]
  node_src_prod_rag_pkg["Package<br/>[__init__.py]"]
  node_src_prod_rag_cli["CLI<br/>command line<br/>[cli.py]"]
  node_run_llm_query["Launcher<br/>macOS command"]
  node_src_prod_rag_readme["Readme<br/>usage guide<br/>[README.md]"]
end

subgraph group_orchestration["Orchestration"]
  node_src_prod_rag_pipeline["Pipeline<br/>orchestrator<br/>[pipeline.py]"]
  node_src_prod_rag_prompts["Prompts<br/>prompt builder<br/>[prompts.py]"]
end

subgraph group_retrieval["Retrieval"]
  node_src_prod_rag_search["Search<br/>discovery<br/>[search.py]"]
  node_src_prod_rag_fetch["Fetch<br/>downloader<br/>[fetch.py]"]
  node_src_prod_rag_extract["Extract<br/>[extract.py]"]
end

subgraph group_ranking["Ranking"]
  node_src_prod_rag_rerank["Rerank<br/>ranking engine<br/>[rerank.py]"]
  node_src_prod_rag_confidence(("Confidence<br/>quality signals<br/>[confidence.py]"))
end

subgraph group_support["Support"]
  node_src_prod_rag_config["Config<br/>[config.py]"]
  node_src_prod_rag_core(("Runtime<br/>[core.py]"))
  node_requirements["Deps<br/>python dependencies<br/>[requirements.txt]"]
  node_pyproject["Build<br/>package metadata<br/>[pyproject.toml]"]
end

node_src_prod_rag_cli -->|"invokes"| node_src_prod_rag_pipeline
node_run_llm_query -->|"launches"| node_src_prod_rag_cli
node_src_prod_rag_pkg -->|"exposes"| node_src_prod_rag_cli
node_src_prod_rag_pkg -->|"exposes"| node_src_prod_rag_pipeline
node_src_prod_rag_pipeline -->|"searches"| node_src_prod_rag_search
node_src_prod_rag_search -->|"selects"| node_src_prod_rag_fetch
node_src_prod_rag_fetch -->|"downloads"| node_src_prod_rag_extract
node_src_prod_rag_extract -->|"chunks"| node_src_prod_rag_rerank
node_src_prod_rag_rerank -->|"weights"| node_src_prod_rag_confidence
node_src_prod_rag_rerank -->|"returns"| node_src_prod_rag_pipeline
node_src_prod_rag_pipeline -->|"assembles"| node_src_prod_rag_prompts
node_src_prod_rag_confidence -->|"annotates"| node_src_prod_rag_prompts
node_src_prod_rag_config -->|"configures"| node_src_prod_rag_pipeline
node_src_prod_rag_config -->|"tunes"| node_src_prod_rag_search
node_src_prod_rag_config -->|"tunes"| node_src_prod_rag_fetch
node_src_prod_rag_config -->|"tunes"| node_src_prod_rag_rerank
node_src_prod_rag_core -->|"adapts"| node_src_prod_rag_search
node_src_prod_rag_core -->|"adapts"| node_src_prod_rag_extract
node_src_prod_rag_core -->|"adapts"| node_src_prod_rag_rerank
node_src_prod_rag_readme -.->|"documents"| node_src_prod_rag_cli
node_src_prod_rag_readme -.->|"documents"| node_src_prod_rag_pipeline
node_requirements -.->|"declares"| node_src_prod_rag_core
node_pyproject -.->|"packages"| node_src_prod_rag_pkg

click node_src_prod_rag_pkg "https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/__init__.py"
click node_src_prod_rag_cli "https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/cli.py"
click node_run_llm_query "https://github.com/kazkozdev/production_rag_pipeline/blob/main/run_llm_query.command"
click node_src_prod_rag_pipeline "https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/pipeline.py"
click node_src_prod_rag_prompts "https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/prompts.py"
click node_src_prod_rag_search "https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/search.py"
click node_src_prod_rag_fetch "https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/fetch.py"
click node_src_prod_rag_extract "https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/extract.py"
click node_src_prod_rag_rerank "https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/rerank.py"
click node_src_prod_rag_confidence "https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/confidence.py"
click node_src_prod_rag_config "https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/config.py"
click node_src_prod_rag_core "https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/core.py"
click node_src_prod_rag_readme "https://github.com/kazkozdev/production_rag_pipeline/blob/main/README.md"
click node_requirements "https://github.com/kazkozdev/production_rag_pipeline/blob/main/requirements.txt"
click node_pyproject "https://github.com/kazkozdev/production_rag_pipeline/blob/main/pyproject.toml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_prod_rag_pkg,node_src_prod_rag_cli,node_run_llm_query,node_src_prod_rag_readme toneBlue
class node_src_prod_rag_pipeline,node_src_prod_rag_prompts toneAmber
class node_src_prod_rag_search,node_src_prod_rag_fetch,node_src_prod_rag_extract toneMint
class node_src_prod_rag_rerank,node_src_prod_rag_confidence toneRose
class node_src_prod_rag_config,node_src_prod_rag_core,node_requirements,node_pyproject toneIndigo
```

## Components

### Entry points

User-facing launchers and package interface.

- [Package](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/__init__.py): python package · Library root for the retrieval-augmented generation toolkit. · `src/production_rag_pipeline/__init__.py`
- [CLI](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/cli.py): command line · User-facing command-line entry point for search and read workflows. · `src/production_rag_pipeline/cli.py`
- [Launcher](https://github.com/kazkozdev/production_rag_pipeline/blob/main/run_llm_query.command): macOS command · Convenience launcher that bootstraps a local environment on macOS. · `run_llm_query.command`
- [Readme](https://github.com/kazkozdev/production_rag_pipeline/blob/main/README.md): usage guide · Documents the pipeline behavior, modes, and operational defaults. · `README.md`

### Orchestration

End-to-end pipeline composition and prompt assembly.

- [Pipeline](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/pipeline.py): orchestrator · Coordinates the full query-to-prompt flow and preserves source mapping. · `src/production_rag_pipeline/pipeline.py`
- [Prompts](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/prompts.py): prompt builder · Converts the assembled context bundle into the final citation-ready prompt. · `src/production_rag_pipeline/prompts.py`

### Retrieval

Discovery, network fetch, and content normalization stages.

- [Search](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/search.py): discovery · Runs web search across multiple engines and applies language-aware filtering. · `src/production_rag_pipeline/search.py`
- [Fetch](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/fetch.py): downloader · Retrieves candidate pages with retries, timeouts, and bounded parallelism. · `src/production_rag_pipeline/fetch.py`
- [Extract](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/extract.py): content extraction · Normalizes HTML into main content, dates, and text chunks. · `src/production_rag_pipeline/extract.py`

### Ranking

Scoring, reranking, and confidence signals.

- [Rerank](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/rerank.py): ranking engine · Combines lexical, semantic, diversity, and freshness signals. · `src/production_rag_pipeline/rerank.py`
- [Confidence](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/confidence.py): quality signals · Derives source quality and freshness signals that inform ranking and warnings. · `src/production_rag_pipeline/confidence.py`

### Support

Configuration and runtime capability state.

- [Config](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/config.py): configuration · Loads RAGConfig from dataclasses, YAML, and environment variables. · `src/production_rag_pipeline/config.py`
- [Runtime](https://github.com/kazkozdev/production_rag_pipeline/blob/main/src/production_rag_pipeline/core.py): runtime state · Holds optional dependency state and runtime capabilities. · `src/production_rag_pipeline/core.py`
- [Deps](https://github.com/kazkozdev/production_rag_pipeline/blob/main/requirements.txt): python dependencies · Declared runtime dependencies and optional capability packages. · `requirements.txt`
- [Build](https://github.com/kazkozdev/production_rag_pipeline/blob/main/pyproject.toml): package metadata · Project metadata, build configuration, and packaging settings. · `pyproject.toml`

## Connections

- CLI → Pipeline: invokes · CLI hands user requests to the orchestrated pipeline.
- Launcher → CLI: launches · macOS launcher bootstraps and then calls the CLI entry point.
- Package → CLI: exposes · Package surface exposes the command-line interface.
- Package → Pipeline: exposes · Package surface exposes the main orchestration API.
- Pipeline → Search: searches · Pipeline begins by discovering candidate URLs.
- Search → Fetch: selects · Search results become the set of pages to retrieve.
- Fetch → Extract: downloads · Fetched HTML is passed into content extraction and normalization.
- Extract → Rerank: chunks · Extracted evidence is chunked and handed to the ranking stage.
- Rerank → Confidence: weights · Ranking incorporates confidence and freshness signals.
- Rerank → Pipeline: returns · Reranked evidence flows back into orchestration.
- Pipeline → Prompts: assembles · Pipeline converts selected evidence into a prompt bundle.
- Confidence → Prompts: annotates · Quality signals are reflected in prompt notes and warnings.
- Config → Pipeline: configures · Configuration drives pipeline behavior and defaults.
- Config → Search: tunes · Search behavior is controlled by configuration.
- Config → Fetch: tunes · Fetch timeouts and concurrency are configuration-driven.
- Config → Rerank: tunes · Ranking thresholds and model choices are configuration-driven.
- Runtime → Search: adapts · Available capabilities influence search behavior.
- Runtime → Extract: adapts · Optional extraction dependencies change normalization strategy.
- Runtime → Rerank: adapts · Optional semantic models and rerankers change ranking depth.
- Readme → CLI: documents · README describes the user-facing commands and modes.
- Readme → Pipeline: documents · README explains the end-to-end query-to-prompt flow.
- Deps → Runtime: declares · Dependencies define which optional capabilities are available.
- Build → Package: packages · Project metadata and packaging settings define the distributable library.
