# ridabayi/cccc architecture

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

- Interactive diagram: https://gitdiagram.com/ridabayi/cccc
- Repository: https://github.com/ridabayi/cccc

## Overview

1. This repo is a procurement copilot split into four main planes: a FastAPI backend, a Streamlit frontend, an offline ingestion/indexing pipeline, and GCP infrastructure definitions. The core product scope is twofold: question answering over procurement documents and invoice analysis against structured procurement data.

2. The backend is the main application boundary. `backend/main.py` exposes HTTP endpoints for health, document listing, search, ask, analyze, and invoices. The API layer is thin; most behavior is delegated into services and actor-style orchestration classes.

3. The ask/search subsystem is the central RAG path. A query goes through `QueryClassifierActor`, then retrieval via `RetrievalActor`, then answer synthesis via `AskActor`. Retrieval is hybrid: BM25, vector search, and fusion/reranking are all present, with dedicated actors and repositories for each stage.

4. There is a separate LLM gateway boundary. `LLMGatewayActor` abstracts model access and supports a primary/fallback provider setup, documented as Groq first with Gemini fallback. This keeps model choice and provider resilience outside the business workflow.

5. Retrieval is backed by distinct storage abstractions. `backend/repositories/bm25_store.py` and `vector_store.py` encapsulate the two retrieval modes, while `document_repository.py` handles document metadata. Services such as `retrieval_service.py`, `search_service.py`, `reranking_service.py`, and `metadata_filter_service.py` compose these stores into user-facing search and ask responses.

6. Invoice analysis is a separate workflow, not just another RAG query. The analysis endpoints and services combine uploaded invoice content with structured reference data through business rules, invoice extraction, response-building, and optional BigQuery access. This path is more deterministic and data-centric than the ask flow.

7. The frontend is a lightweight Streamlit client. It provides pages for chat and invoice analysis, and it talks only to the backend via HTTP through `frontend/services/api_client.py`. This keeps the UI stateless and decoupled from retrieval, LLM, and storage concerns.

8. The offline data plane lives in `data/` and `jobs/`. Raw procurement documents are stored as markdown and CSV/JSON datasets, then processed by ingestion jobs that parse, chunk, embed, upload, and build indexes. The pipeline is split into reusable steps for document reading, chunking, embedding generation, and upload/index refresh.

9. The repository is designed for hybrid storage and cloud deployment. The README and Terraform indicate GCS for document/artifact storage, BigQuery for structured analytics data, Vertex AI for vector indexing, and Cloud Run for backend/frontend/job deployment. Redis is optional for ask caching.

10. Observability is a first-class backend concern. Middleware captures request context, services emit structured AI and workflow metrics, and the ask workflow records cache usage, retrieval mode, provider choice, latency, and cost estimates. This supports Cloud Logging/Monitoring and benchmark comparison.

11. Evaluation is built into the codebase as a separate subsystem. `backend/evaluation/` loads datasets, runs benchmark sweeps across retrieval modes and top-k values, and computes retrieval, generation, grounding, latency, and workflow metrics. Versioned outputs are stored under `artifacts/evaluation/`.

12. The repo is organized for CI/CD and reproducible infrastructure. GitLab CI runs lint, test, security, build, and deploy steps; Dockerfiles exist for backend, frontend, and ingestion jobs; Terraform modules define the GCP resources. This makes the runtime architecture explicit: local dev, containerized deployment, and managed cloud services.

Main entry points: [`backend/api/router.py`](https://github.com/ridabayi/cccc/blob/main/backend/api/router.py) (HTTP routes), [`backend/services/retrieval_workflow_service.py`](https://github.com/ridabayi/cccc/blob/main/backend/services/retrieval_workflow_service.py) (Search workflow), [`infra/terraform/main.tf`](https://github.com/ridabayi/cccc/blob/main/infra/terraform/main.tf) (Terraform), [`backend/services/ask_workflow_service.py`](https://github.com/ridabayi/cccc/blob/main/backend/services/ask_workflow_service.py) (Ask workflow)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend"]
  node_backend_main["API app<br/>FastAPI entrypoint<br/>[main.py]"]
  node_backend_routes["HTTP routes<br/>API layer<br/>[router.py]"]
  node_ask_workflow["Ask workflow<br/>RAG orchestration"]
  node_retrieval_workflow["Search workflow<br/>retrieval orchestration"]
  node_invoice_workflow["Invoice analysis<br/>analysis workflow"]
  node_llm_gateway["LLM gateway<br/>model access"]
  node_retrieval_actors["Retrieval actors<br/>[retrieval_actor.py]"]
  node_answer_svc["Answer builder<br/>response service"]
  node_search_svc["Search service<br/>[search_service.py]"]
  node_bm25_store[("BM25 store<br/>lexical index<br/>[bm25_store.py]")]
  node_vector_store[("Vector store<br/>embedding index<br/>[vector_store.py]")]
  node_doc_repo[("Document repo<br/>metadata store")]
  node_observability["Observability<br/>metrics & logging"]
  node_evaluation["Evaluation<br/>benchmark suite<br/>[benchmark.py]"]
end

subgraph group_frontend["Frontend"]
  node_frontend_app["Streamlit app<br/>UI shell<br/>[app.py]"]
  node_frontend_api["API client<br/>HTTP client<br/>[api_client.py]"]
end

subgraph group_data_jobs["Data & Jobs"]
  node_data_corpus["Procurement data<br/>raw datasets<br/>[README.md]"]
  node_ingestion_pipeline["Ingestion pipeline<br/>offline pipeline<br/>[run_pipeline.py]"]
  node_indexing_steps["Indexing steps<br/>ingestion stages<br/>[pipeline.py]"]
end

subgraph group_infra["Infra"]
  node_infra_terraform["Terraform<br/>GCP infra<br/>[main.tf]"]
end

node_backend_main -->|"serves"| node_backend_routes
node_backend_main -->|"configures"| node_observability
node_backend_routes -->|"ask"| node_ask_workflow
node_backend_routes -->|"search"| node_retrieval_workflow
node_backend_routes -->|"analyze"| node_invoice_workflow
node_ask_workflow -->|"retrieves"| node_retrieval_actors
node_ask_workflow -->|"generates"| node_llm_gateway
node_ask_workflow -->|"builds"| node_answer_svc
node_retrieval_workflow -->|"orchestrates"| node_retrieval_actors
node_retrieval_workflow -->|"formats"| node_search_svc
node_retrieval_actors -->|"bm25"| node_bm25_store
node_retrieval_actors -->|"vector"| node_vector_store
node_retrieval_workflow -->|"filters"| node_doc_repo
node_search_svc -->|"enriches"| node_doc_repo
node_invoice_workflow -->|"references"| node_doc_repo
node_invoice_workflow -->|"reads"| node_data_corpus
node_backend_routes -->|"tracks"| node_observability
node_evaluation -.->|"benchmarks"| node_retrieval_workflow
node_evaluation -.->|"measures"| node_observability
node_frontend_app -->|"calls"| node_frontend_api
node_frontend_api -->|"http"| node_backend_main
node_ingestion_pipeline -->|"runs"| node_indexing_steps
node_indexing_steps -->|"ingests"| node_data_corpus
node_indexing_steps -->|"writes"| node_vector_store
node_indexing_steps -->|"writes"| node_bm25_store
node_infra_terraform -->|"deploys"| node_backend_main
node_infra_terraform -->|"deploys"| node_frontend_app
node_infra_terraform -->|"provisions"| node_vector_store
node_infra_terraform -->|"provisions"| node_doc_repo

click node_backend_main "https://github.com/ridabayi/cccc/blob/main/backend/main.py"
click node_backend_routes "https://github.com/ridabayi/cccc/blob/main/backend/api/router.py"
click node_ask_workflow "https://github.com/ridabayi/cccc/blob/main/backend/services/ask_workflow_service.py"
click node_retrieval_workflow "https://github.com/ridabayi/cccc/blob/main/backend/services/retrieval_workflow_service.py"
click node_invoice_workflow "https://github.com/ridabayi/cccc/blob/main/backend/services/analysis_workflow_service.py"
click node_llm_gateway "https://github.com/ridabayi/cccc/blob/main/backend/actors/llm_gateway_actor.py"
click node_retrieval_actors "https://github.com/ridabayi/cccc/blob/main/backend/actors/retrieval_actor.py"
click node_answer_svc "https://github.com/ridabayi/cccc/blob/main/backend/services/ask_response_service.py"
click node_search_svc "https://github.com/ridabayi/cccc/blob/main/backend/services/search_service.py"
click node_bm25_store "https://github.com/ridabayi/cccc/blob/main/backend/repositories/bm25_store.py"
click node_vector_store "https://github.com/ridabayi/cccc/blob/main/backend/repositories/vector_store.py"
click node_doc_repo "https://github.com/ridabayi/cccc/blob/main/backend/repositories/document_repository.py"
click node_observability "https://github.com/ridabayi/cccc/blob/main/backend/services/ai_observability_service.py"
click node_evaluation "https://github.com/ridabayi/cccc/blob/main/backend/evaluation/benchmark.py"
click node_frontend_app "https://github.com/ridabayi/cccc/blob/main/frontend/app.py"
click node_frontend_api "https://github.com/ridabayi/cccc/blob/main/frontend/services/api_client.py"
click node_data_corpus "https://github.com/ridabayi/cccc/blob/main/data/raw/README.md"
click node_ingestion_pipeline "https://github.com/ridabayi/cccc/blob/main/jobs/pipeline/run_pipeline.py"
click node_indexing_steps "https://github.com/ridabayi/cccc/blob/main/jobs/indexing/ingestion/pipeline.py"
click node_infra_terraform "https://github.com/ridabayi/cccc/blob/main/infra/terraform/main.tf"

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_backend_main,node_backend_routes,node_ask_workflow,node_retrieval_workflow,node_invoice_workflow,node_llm_gateway,node_retrieval_actors,node_answer_svc,node_search_svc,node_bm25_store,node_vector_store,node_doc_repo,node_observability,node_evaluation toneBlue
class node_frontend_app,node_frontend_api toneAmber
class node_data_corpus,node_ingestion_pipeline,node_indexing_steps toneMint
class node_infra_terraform toneRose
```

## Components

### Backend

FastAPI application boundary, orchestration, storage abstractions, and evaluation

- [API app](https://github.com/ridabayi/cccc/blob/main/backend/main.py): FastAPI entrypoint · Wires routes, middleware, settings, and service dependencies · `backend/main.py`
- [HTTP routes](https://github.com/ridabayi/cccc/blob/main/backend/api/router.py): API layer · Health, documents, search, ask, analyze, and invoices endpoints · `backend/api/router.py`
- [Ask workflow](https://github.com/ridabayi/cccc/blob/main/backend/services/ask_workflow_service.py): RAG orchestration · Coordinates query classification, retrieval, and answer synthesis · `backend/services/ask_workflow_service.py`
- [Search workflow](https://github.com/ridabayi/cccc/blob/main/backend/services/retrieval_workflow_service.py): retrieval orchestration · Composes hybrid retrieval, filtering, reranking, and search responses · `backend/services/retrieval_workflow_service.py`
- [Invoice analysis](https://github.com/ridabayi/cccc/blob/main/backend/services/analysis_workflow_service.py): analysis workflow · Combines invoice extraction, business rules, response building, and structured data access · `backend/services/analysis_workflow_service.py`
- [LLM gateway](https://github.com/ridabayi/cccc/blob/main/backend/actors/llm_gateway_actor.py): model access · Abstracts primary/fallback model providers for generation and classification · `backend/actors/llm_gateway_actor.py`
- [Retrieval actors](https://github.com/ridabayi/cccc/blob/main/backend/actors/retrieval_actor.py): hybrid retrieval actors · Query classification, BM25, vector search, fusion, and retrieval coordination · `backend/actors/retrieval_actor.py`
- [Answer builder](https://github.com/ridabayi/cccc/blob/main/backend/services/ask_response_service.py): response service · Builds final ask responses and manages prompt/answer assembly · `backend/services/ask_response_service.py`
- [Search service](https://github.com/ridabayi/cccc/blob/main/backend/services/search_service.py): search service · User-facing search composition over retrieval and metadata filters · `backend/services/search_service.py`
- [BM25 store](https://github.com/ridabayi/cccc/blob/main/backend/repositories/bm25_store.py): lexical index · Lexical retrieval storage abstraction · `backend/repositories/bm25_store.py`
- [Vector store](https://github.com/ridabayi/cccc/blob/main/backend/repositories/vector_store.py): embedding index · Vector retrieval storage abstraction · `backend/repositories/vector_store.py`
- [Document repo](https://github.com/ridabayi/cccc/blob/main/backend/repositories/document_repository.py): metadata store · Document metadata and listing repository · `backend/repositories/document_repository.py`
- [Observability](https://github.com/ridabayi/cccc/blob/main/backend/services/ai_observability_service.py): metrics & logging · Request context, AI metrics, workflow telemetry, and cost/latency tracking · `backend/services/ai_observability_service.py`
- [Evaluation](https://github.com/ridabayi/cccc/blob/main/backend/evaluation/benchmark.py): benchmark suite · Dataset loading, sweep execution, metric computation, and report generation · `backend/evaluation/benchmark.py`

### Frontend

Streamlit client for chat and invoice analysis

- [Streamlit app](https://github.com/ridabayi/cccc/blob/main/frontend/app.py): UI shell · Chat and invoice analysis pages · `frontend/app.py`
- [API client](https://github.com/ridabayi/cccc/blob/main/frontend/services/api_client.py): HTTP client · Stateless frontend-to-backend integration · `frontend/services/api_client.py`

### Data & Jobs

Offline corpora, ingestion, embedding, and indexing pipeline

- [Procurement data](https://github.com/ridabayi/cccc/blob/main/data/raw/README.md): raw datasets · Markdown documents, metadata, structured tables, and evaluation samples · `data/raw/README.md`
- [Ingestion pipeline](https://github.com/ridabayi/cccc/blob/main/jobs/pipeline/run_pipeline.py): offline pipeline · Parse, chunk, embed, and upload documents for index refresh · `jobs/pipeline/run_pipeline.py`
- [Indexing steps](https://github.com/ridabayi/cccc/blob/main/jobs/indexing/ingestion/pipeline.py): ingestion stages · Reader, chunker, embedding, and upload stages for document processing · `jobs/indexing/ingestion/pipeline.py`

### Infra

GCP deployment and Terraform definitions

- [Terraform](https://github.com/ridabayi/cccc/blob/main/infra/terraform/main.tf): GCP infra · Cloud Run, storage, BigQuery, Vertex AI, secrets, IAM, and observability modules · `infra/terraform/main.tf`

## Connections

- API app → HTTP routes: serves · Registers API routes and app lifecycle
- API app → Observability: configures · Installs request context and logging/metrics middleware
- HTTP routes → Ask workflow: ask · Ask endpoint delegates to RAG workflow
- HTTP routes → Search workflow: search · Search endpoint delegates to retrieval workflow
- HTTP routes → Invoice analysis: analyze · Analyze and invoice endpoints delegate to invoice workflow
- Ask workflow → Retrieval actors: retrieves · Classifies query and drives hybrid retrieval actors
- Ask workflow → LLM gateway: generates · Uses model gateway for classification and answer synthesis
- Ask workflow → Answer builder: builds · Assembles final answer payload
- Search workflow → Retrieval actors: orchestrates · Coordinates lexical, vector, and fusion retrieval
- Search workflow → Search service: formats · Turns retrieval results into search responses
- Retrieval actors → BM25 store: bm25 · Reads lexical index
- Retrieval actors → Vector store: vector · Reads embedding index
- Search workflow → Document repo: filters · Uses document metadata and document listing
- Search service → Document repo: enriches · Adds document metadata to search results
- Invoice analysis → Document repo: references · Uses structured document and invoice metadata
- Invoice analysis → Procurement data: reads · Consumes uploaded invoice and structured procurement data
- HTTP routes → Observability: tracks · Emits request and workflow telemetry
- Evaluation → Search workflow: benchmarks · Runs retrieval and RAG sweeps
- Evaluation → Observability: measures · Captures latency, cost, and workflow metrics
- Streamlit app → API client: calls · UI talks only through HTTP client
- API client → API app: http · Sends requests to backend API
- Ingestion pipeline → Indexing steps: runs · Executes parsing, chunking, embedding, and upload stages
- Indexing steps → Procurement data: ingests · Processes raw documents and structured inputs
- Indexing steps → Vector store: writes · Publishes embeddings to vector index
- Indexing steps → BM25 store: writes · Refreshes lexical index
- Terraform → API app: deploys · Defines Cloud Run backend runtime
- Terraform → Streamlit app: deploys · Defines Cloud Run frontend runtime
- Terraform → Vector store: provisions · Defines Vertex AI and storage resources
- Terraform → Document repo: provisions · Defines GCS and BigQuery-backed data services
