# auracodeskm/brdagentbackend architecture

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

- Interactive diagram: https://gitdiagram.com/auracodeskm/brdagentbackend
- Repository: https://github.com/auracodeskm/brdagentbackend

## Overview

#### Purpose and Entry Points
BRD Agent is a Python backend that ingests corporate communications and generates source-grounded, multi-section Business Requirements Documents (BRDs). It exposes FastAPI HTTP/WebSocket interfaces and can also be run as a standalone pipeline.
- Application entry points: `main.py`, `app/main.py`
- API routes: `app/api/routes/ingest.py`, `app/api/routes/generate.py`
- Batch/background ingestion: `background_ingest.py`

#### API and Request Boundary
FastAPI receives uploads/ingestion requests and BRD-generation requests. Request/response contracts are centralized in schemas; configuration is environment-driven. Generation is intended to stream section progress/results to the external Next.js frontend over WebSockets.
- API composition: `app/main.py`
- Route handlers: `app/api/routes/ingest.py`, `app/api/routes/generate.py`
- Contracts/configuration: `app/models/schemas.py`, `app/core/config.py`

#### Ingestion and Index Construction
The ingestion pipeline converts raw communications into cleaned text and semantic chunks, then builds two retrieval representations: dense embeddings and a relationship-oriented knowledge graph. It supports the repository’s offline/local processing posture and has a standalone voice/transcript component.
- Pipeline: `pipeline/ingestion.py`
- Embedding model integration: `models/embeddings.py`
- Speech processing: `whisper_engine.py`

#### Dual Retrieval and Reranking
At query time, retrieval combines semantic vector search with graph-aware retrieval. The merged evidence is reranked before context is supplied to generation, separating evidence selection from document writing.
- Retrieval orchestration: `pipeline/retrieval.py`, `pipeline/rag_engine.py`
- Dense/vector and graph dependencies are represented through the pipeline; Redis-backed BM25/cache adapters live in `cache/redis_bm25.py`, `cache/redis_cache.py`
- Cross-encoder reranking: `models/reranker.py`

#### BRD Generation Pipeline
The generator produces structured BRD sections from retrieved evidence. The design supports a two-pass model strategy: a smaller local Qwen model extracts facts, then a larger Qwen model writes professional prose. Functional requirements can use multiple candidate generations and a merge/intersection pass to reduce unsupported requirements.
- Generation orchestration: `pipeline/generator.py`
- Local LLM adapter: `models/llm.py`
- Generation API boundary: `app/api/routes/generate.py`

#### Governance: Conflicts and Traceability
Post-retrieval/document-analysis stages identify contradictory stakeholder statements and create requirement-to-source mappings. These outputs are first-class BRD evidence, not merely UI metadata.
- Contradiction analysis: `pipeline/conflict.py`
- Requirement-source matrix: `pipeline/traceability.py`
- Operational verification artifacts: `test_graph.py`, `audit_brd.md`

#### Runtime and Infrastructure
The service runs on Python/FastAPI/Uvicorn and is designed for local Ollama inference, LightRAG graph storage, FAISS dense retrieval, Hugging Face BGE embedding/reranker models, and optional Redis/Upstash caching. It targets an NVIDIA RTX 4090-class 24 GB VRAM environment; model setup is automated separately.
- Dependencies: `requirements.txt`
- Model bootstrap: `download_models.py`
- Environment template: `.env.example`

Main entry points: [`pipeline/generator.py`](https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/generator.py) (BRD generator), [`pipeline/ingestion.py`](https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/ingestion.py) (Ingestion pipeline), [`pipeline/retrieval.py`](https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/retrieval.py) (Dual retrieval), [`app/api/routes/generate.py`](https://github.com/auracodeskm/brdagentbackend/blob/main/app/api/routes/generate.py) (Generate routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["Service API"]
  node_frontend["Next.js frontend<br/>external client"]
  node_app["FastAPI application<br/>API composition<br/>[main.py]"]
  node_ingest_route["Ingest routes<br/>route handlers<br/>[ingest.py]"]
  node_generate_route["Generate routes<br/>route handlers<br/>[generate.py]"]
  node_contracts["Schemas &amp; configuration<br/>contracts/config<br/>[schemas.py]"]
end

subgraph group_ingestion["Ingestion &amp; Indexing"]
  node_background_ingest["Background ingestion<br/>batch entry point"]
  node_ingestion_pipeline["Ingestion pipeline<br/>text processing<br/>[ingestion.py]"]
  node_speech["Speech processing<br/>transcription<br/>[whisper_engine.py]"]
end

subgraph group_retrieval["Evidence Retrieval"]
  node_retrieval["Dual retrieval<br/>retrieval orchestration<br/>[retrieval.py]"]
  node_rag_engine["RAG engine<br/>retrieval integration<br/>[rag_engine.py]"]
  node_evidence_store[("FAISS + LightRAG indexes<br/>retrieval stores")]
  node_redis[("Redis BM25/cache<br/>optional cache<br/>[redis_bm25.py]")]
  node_reranker{{"Cross-encoder reranker<br/>BGE reranker<br/>[reranker.py]"}}
end

subgraph group_generation["BRD Generation &amp; Governance"]
  node_generator["BRD generator<br/>generation orchestration<br/>[generator.py]"]
  node_conflicts["Conflict analysis<br/>contradiction detection<br/>[conflict.py]"]
  node_traceability["Traceability matrix<br/>requirement-source mapping<br/>[traceability.py]"]
end

subgraph group_runtime["Models &amp; Runtime"]
  node_embedding_model{{"Embedding model<br/>BGE embeddings<br/>[embeddings.py]"}}
  node_llm{{"Local Qwen adapter<br/>Ollama LLM adapter<br/>[llm.py]"}}
  node_model_bootstrap["Model bootstrap<br/>model setup<br/>[download_models.py]"]
end

node_frontend -->|"HTTP / WebSocket"| node_app
node_app -->|"mounts"| node_ingest_route
node_app -->|"mounts"| node_generate_route
node_contracts -->|"request contracts"| node_ingest_route
node_contracts -->|"request contracts"| node_generate_route
node_ingest_route -->|"ingests uploads"| node_ingestion_pipeline
node_background_ingest -->|"runs batch ingestion"| node_ingestion_pipeline
node_speech -->|"transcripts"| node_ingestion_pipeline
node_ingestion_pipeline -->|"embeds chunks"| node_embedding_model
node_embedding_model -->|"dense vectors"| node_evidence_store
node_ingestion_pipeline -->|"entities and relationships"| node_evidence_store
node_ingestion_pipeline -.->|"BM25/cache data"| node_redis
node_generate_route -->|"starts BRD generation"| node_generator
node_generator -->|"queries evidence"| node_retrieval
node_retrieval -->|"orchestrates"| node_rag_engine
node_rag_engine -->|"vector + graph search"| node_evidence_store
node_retrieval -.->|"BM25/cache lookup"| node_redis
node_retrieval -->|"reranks merged evidence"| node_reranker
node_reranker -->|"grounded context"| node_generator
node_generator -->|"fact extraction and prose"| node_llm
node_generator -->|"analyzes statements"| node_conflicts
node_generator -->|"maps requirements to sources"| node_traceability
node_generator -->|"sections and progress"| node_generate_route
node_model_bootstrap -.->|"installs"| node_embedding_model
node_model_bootstrap -.->|"installs"| node_reranker
node_model_bootstrap -.->|"installs"| node_llm

click node_app "https://github.com/auracodeskm/brdagentbackend/blob/main/app/main.py"
click node_ingest_route "https://github.com/auracodeskm/brdagentbackend/blob/main/app/api/routes/ingest.py"
click node_generate_route "https://github.com/auracodeskm/brdagentbackend/blob/main/app/api/routes/generate.py"
click node_contracts "https://github.com/auracodeskm/brdagentbackend/blob/main/app/models/schemas.py"
click node_background_ingest "https://github.com/auracodeskm/brdagentbackend/blob/main/background_ingest.py"
click node_ingestion_pipeline "https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/ingestion.py"
click node_speech "https://github.com/auracodeskm/brdagentbackend/blob/main/whisper_engine.py"
click node_embedding_model "https://github.com/auracodeskm/brdagentbackend/blob/main/models/embeddings.py"
click node_retrieval "https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/retrieval.py"
click node_rag_engine "https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/rag_engine.py"
click node_redis "https://github.com/auracodeskm/brdagentbackend/blob/main/cache/redis_bm25.py"
click node_reranker "https://github.com/auracodeskm/brdagentbackend/blob/main/models/reranker.py"
click node_generator "https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/generator.py"
click node_llm "https://github.com/auracodeskm/brdagentbackend/blob/main/models/llm.py"
click node_conflicts "https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/conflict.py"
click node_traceability "https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/traceability.py"
click node_model_bootstrap "https://github.com/auracodeskm/brdagentbackend/blob/main/download_models.py"

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_frontend,node_app,node_ingest_route,node_generate_route,node_contracts toneBlue
class node_background_ingest,node_ingestion_pipeline,node_speech toneAmber
class node_retrieval,node_rag_engine,node_evidence_store,node_redis,node_reranker toneMint
class node_generator,node_conflicts,node_traceability toneRose
class node_embedding_model,node_llm,node_model_bootstrap toneIndigo
```

## Components

### Service API

- **Next.js frontend**: external client · Receives streamed BRD progress and results.
- [FastAPI application](https://github.com/auracodeskm/brdagentbackend/blob/main/app/main.py): API composition · `app/main.py`
- [Ingest routes](https://github.com/auracodeskm/brdagentbackend/blob/main/app/api/routes/ingest.py): route handlers · `app/api/routes/ingest.py`
- [Generate routes](https://github.com/auracodeskm/brdagentbackend/blob/main/app/api/routes/generate.py): route handlers · Streams generation updates over WebSockets. · `app/api/routes/generate.py`
- [Schemas & configuration](https://github.com/auracodeskm/brdagentbackend/blob/main/app/models/schemas.py): contracts/config · `app/models/schemas.py`

### Ingestion & Indexing

- [Background ingestion](https://github.com/auracodeskm/brdagentbackend/blob/main/background_ingest.py): batch entry point · `background_ingest.py`
- [Ingestion pipeline](https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/ingestion.py): text processing · Cleans communications and creates semantic chunks. · `pipeline/ingestion.py`
- [Speech processing](https://github.com/auracodeskm/brdagentbackend/blob/main/whisper_engine.py): transcription · `whisper_engine.py`

### Evidence Retrieval

- [Dual retrieval](https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/retrieval.py): retrieval orchestration · Combines dense search with graph-aware evidence. · `pipeline/retrieval.py`
- [RAG engine](https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/rag_engine.py): retrieval integration · Connects vector and graph retrieval representations. · `pipeline/rag_engine.py`
- **FAISS + LightRAG indexes**: retrieval stores · Dense vectors and relationship-oriented knowledge graph.
- [Redis BM25/cache](https://github.com/auracodeskm/brdagentbackend/blob/main/cache/redis_bm25.py): optional cache · `cache/redis_bm25.py`
- [Cross-encoder reranker](https://github.com/auracodeskm/brdagentbackend/blob/main/models/reranker.py): BGE reranker · `models/reranker.py`

### BRD Generation & Governance

- [BRD generator](https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/generator.py): generation orchestration · Builds structured sections and validates functional requirements through candidate merging. · `pipeline/generator.py`
- [Conflict analysis](https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/conflict.py): contradiction detection · `pipeline/conflict.py`
- [Traceability matrix](https://github.com/auracodeskm/brdagentbackend/blob/main/pipeline/traceability.py): requirement-source mapping · `pipeline/traceability.py`

### Models & Runtime

- [Embedding model](https://github.com/auracodeskm/brdagentbackend/blob/main/models/embeddings.py): BGE embeddings · `models/embeddings.py`
- [Local Qwen adapter](https://github.com/auracodeskm/brdagentbackend/blob/main/models/llm.py): Ollama LLM adapter · Uses smaller fact extraction and larger prose-writing passes. · `models/llm.py`
- [Model bootstrap](https://github.com/auracodeskm/brdagentbackend/blob/main/download_models.py): model setup · `download_models.py`

## Connections

- Next.js frontend → FastAPI application: HTTP / WebSocket
- FastAPI application → Ingest routes: mounts
- FastAPI application → Generate routes: mounts
- Schemas & configuration → Ingest routes: request contracts
- Schemas & configuration → Generate routes: request contracts
- Ingest routes → Ingestion pipeline: ingests uploads
- Background ingestion → Ingestion pipeline: runs batch ingestion
- Speech processing → Ingestion pipeline: transcripts
- Ingestion pipeline → Embedding model: embeds chunks
- Embedding model → FAISS + LightRAG indexes: dense vectors
- Ingestion pipeline → FAISS + LightRAG indexes: entities and relationships
- Ingestion pipeline → Redis BM25/cache: BM25/cache data
- Generate routes → BRD generator: starts BRD generation
- BRD generator → Dual retrieval: queries evidence
- Dual retrieval → RAG engine: orchestrates
- RAG engine → FAISS + LightRAG indexes: vector + graph search
- Dual retrieval → Redis BM25/cache: BM25/cache lookup
- Dual retrieval → Cross-encoder reranker: reranks merged evidence
- Cross-encoder reranker → BRD generator: grounded context
- BRD generator → Local Qwen adapter: fact extraction and prose
- BRD generator → Conflict analysis: analyzes statements
- BRD generator → Traceability matrix: maps requirements to sources
- BRD generator → Generate routes: sections and progress
- Model bootstrap → Embedding model: installs
- Model bootstrap → Cross-encoder reranker: installs
- Model bootstrap → Local Qwen adapter: installs
