# shivammitra/gen-ai-email-suggested-response-system architecture

> Architecture diagram of the GitHub repository shivammitra/gen-ai-email-suggested-response-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/shivammitra/gen-ai-email-suggested-response-system
- Repository: https://github.com/shivammitra/gen-ai-email-suggested-response-system

## Overview

#### Purpose and Entry Points
This repository evaluates a retrieval-grounded GenAI email reply system. It generates suggested responses for a synthetic inbox dataset, then scores reply quality per item, category, and overall.

Primary entry points:
- `run_pipeline.py` — end-to-end leave-one-out generation, evaluation, console summary, and report emission.
- `eval/evaluate.py` — metric implementation plus standalone human-rating validation harness.
- `data/generate_dataset.py` — reproducibly creates the checked-in dataset.

#### Dataset and Evaluation Contract
The central data contract is JSONL examples containing an incoming email, historical reply, category, and required `key_points`. The latter is the authoritative correctness checklist rather than exact-reply text.

- `data/dataset.jsonl`
- `data/generate_dataset.py`

This schema makes generation grounding and evaluation share a stable unit of work. Dataset rows cover six inbox categories and are intentionally synthetic to avoid PII exposure.

#### Retrieval and Prompted Generation
`src/generator.py` owns the generation path:
1. Load examples and represent subject plus incoming-email text using TF-IDF.
2. Retrieve the most similar historical emails by cosine similarity.
3. Construct a few-shot prompt from retrieved email/reply pairs and the target email.
4. Request a suggested reply from Groq chat completions.

- `src/generator.py`
- `data/dataset.jsonl`

The retrieval boundary is local and deterministic; the LLM boundary is external and nondeterministic.

#### Generation Runtime Modes
With `GROQ_API_KEY`, generation calls Groq’s OpenAI-compatible SDK using `llama-3.3-70b-versatile` by default. Without a key or network availability, the generator uses a deterministic offline fallback based on the closest retrieved reply.

- `src/generator.py`
- `run_pipeline.py`

This fallback keeps pipeline execution available for local/offline grading, but it is explicitly lower-quality than the LLM path.

#### Quality Measurement Pipeline
`eval/evaluate.py` evaluates generated replies against the held-out source record through three signals:
- Key-point coverage: regex/pattern checks over the annotated checklist (45%).
- Similarity: TF-IDF cosine similarity to the historical reply (25%).
- Optional LLM judge: Groq scores correctness, tone, and completeness on 1–5 (30%).

- `eval/evaluate.py`
- `src/generator.py`

When the judge is unavailable, its weight is redistributed across the deterministic metrics. Evaluation returns metric details, covered/missed requirements, explanations, composite scores, and category aggregates.

#### End-to-End Control Flow and Isolation
`run_pipeline.py` performs leave-one-out evaluation: each dataset item is generated using retrieval over the other examples, preventing its known historical reply from becoming direct few-shot context. It then evaluates the generated reply against the held-out item’s reference and checklist.

- `run_pipeline.py`
- `src/generator.py`
- `eval/evaluate.py`

This is the key experimental boundary: retrieval context excludes the target record, while evaluation retains its ground-truth annotations.

#### Outputs, Infrastructure, and Constraints
The full run serializes detailed per-response and aggregate results to `report.json`, enabling downstream analysis without rerunning API calls.

- `run_pipeline.py`
- `report.json`

Runtime dependencies include Python, TF-IDF/cosine similarity tooling, `python-dotenv` for optional local secrets, and the Groq API/SDK for both generation and optional judging. The system’s known quality limits are regex-based coverage, lexical TF-IDF semantics, small synthetic data, and reliance on an LLM judge that requires external quota/network access.

Main entry points: [`eval/evaluate.py`](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/eval/evaluate.py) (Reply evaluator), [`run_pipeline.py`](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/run_pipeline.py) (Leave-one-out pipeline), [`src/generator.py`](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/src/generator.py) (TF-IDF retrieval), [`data/dataset.jsonl`](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/data/dataset.jsonl) (Inbox examples JSONL)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_data["Dataset"]
  node_dataset_generator["Synthetic dataset builder<br/>dataset generator"]
  node_dataset[("Inbox examples JSONL<br/>data contract<br/>[dataset.jsonl]")]
end

subgraph group_generation["Generation"]
  node_retrieval_corpus[("Excluded retrieval corpus<br/>per-item corpus")]
  node_generator["Reply generator<br/>generation runtime<br/>[generator.py]"]
  node_tfidf_retrieval["TF-IDF retrieval<br/>local retrieval<br/>[generator.py]"]
  node_few_shot_prompt["Few-shot prompt<br/>prompt assembly<br/>[generator.py]"]
  node_groq_generation{{"Groq chat completion<br/>external LLM"}}
  node_offline_fallback["Closest-reply fallback<br/>deterministic fallback<br/>[generator.py]"]
  node_suggested_reply["Suggested reply<br/>generation output"]
end

subgraph group_evaluation["Evaluation"]
  node_evaluator["Reply evaluator<br/>metric runtime<br/>[evaluate.py]"]
  node_keypoint_coverage["Key-point coverage<br/>deterministic metric<br/>[evaluate.py]"]
  node_reply_similarity["Reply similarity<br/>deterministic metric<br/>[evaluate.py]"]
  node_llm_judge["Optional LLM judge<br/>judge metric<br/>[evaluate.py]"]
  node_groq_judging{{"Groq judge API<br/>external LLM"}}
  node_evaluation_results["Scores and aggregates<br/>evaluation output"]
end

subgraph group_orchestration["Experiment Run"]
  node_pipeline["Leave-one-out pipeline<br/>entry point<br/>[run_pipeline.py]"]
  node_held_out_record["Held-out record<br/>evaluation unit"]
  node_report["Detailed report<br/>serialized output<br/>[report.json]"]
end

node_dataset_generator -->|"writes"| node_dataset
node_dataset -->|"loads"| node_pipeline
node_pipeline -->|"selects each item"| node_held_out_record
node_pipeline -->|"excludes target"| node_retrieval_corpus
node_retrieval_corpus -->|"retrieval source"| node_generator
node_held_out_record -->|"target email"| node_generator
node_generator -->|"ranks"| node_tfidf_retrieval
node_tfidf_retrieval -->|"retrieved pairs"| node_few_shot_prompt
node_few_shot_prompt -->|"when API available"| node_groq_generation
node_tfidf_retrieval -.->|"when offline"| node_offline_fallback
node_groq_generation -->|"generated reply"| node_suggested_reply
node_offline_fallback -.->|"fallback reply"| node_suggested_reply
node_pipeline -->|"evaluates each reply"| node_evaluator
node_suggested_reply -->|"candidate"| node_evaluator
node_held_out_record -->|"reference and checklist"| node_evaluator
node_evaluator -->|"45% base weight"| node_keypoint_coverage
node_evaluator -->|"25% base weight"| node_reply_similarity
node_evaluator -.->|"30% when available"| node_llm_judge
node_llm_judge -.->|"requests score"| node_groq_judging
node_keypoint_coverage -->|"metric detail"| node_evaluation_results
node_reply_similarity -->|"metric detail"| node_evaluation_results
node_llm_judge -.->|"optional score"| node_evaluation_results
node_evaluation_results -->|"serializes"| node_report
node_pipeline -->|"emits"| node_report

click node_dataset_generator "https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/data/generate_dataset.py"
click node_dataset "https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/data/dataset.jsonl"
click node_pipeline "https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/run_pipeline.py"
click node_generator "https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/src/generator.py"
click node_tfidf_retrieval "https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/src/generator.py"
click node_few_shot_prompt "https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/src/generator.py"
click node_offline_fallback "https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/src/generator.py"
click node_evaluator "https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/eval/evaluate.py"
click node_keypoint_coverage "https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/eval/evaluate.py"
click node_reply_similarity "https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/eval/evaluate.py"
click node_llm_judge "https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/eval/evaluate.py"
click node_report "https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/report.json"

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_dataset_generator,node_dataset toneBlue
class node_retrieval_corpus,node_generator,node_tfidf_retrieval,node_few_shot_prompt,node_groq_generation,node_offline_fallback,node_suggested_reply toneAmber
class node_evaluator,node_keypoint_coverage,node_reply_similarity,node_llm_judge,node_groq_judging,node_evaluation_results toneMint
class node_pipeline,node_held_out_record,node_report toneRose
```

## Components

### Dataset

- [Synthetic dataset builder](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/data/generate_dataset.py): dataset generator · Reproducibly creates PII-free inbox examples. · `data/generate_dataset.py`
- [Inbox examples JSONL](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/data/dataset.jsonl): data contract · Incoming email, historical reply, category, and required key points. · `data/dataset.jsonl`

### Generation

- **Excluded retrieval corpus**: per-item corpus · All dataset examples except the target record.
- [Reply generator](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/src/generator.py): generation runtime · Owns retrieval, prompting, and runtime fallback. · `src/generator.py`
- [TF-IDF retrieval](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/src/generator.py): local retrieval · Ranks historical emails by cosine similarity. · `src/generator.py`
- [Few-shot prompt](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/src/generator.py): prompt assembly · Combines retrieved email/reply pairs with the target email. · `src/generator.py`
- **Groq chat completion**: external LLM · Uses the OpenAI-compatible Groq SDK when available.
- [Closest-reply fallback](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/src/generator.py): deterministic fallback · Returns a reply derived from the highest-ranked retrieval. · `src/generator.py`
- **Suggested reply**: generation output

### Evaluation

- [Reply evaluator](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/eval/evaluate.py): metric runtime · Returns per-item details, composite scores, and category aggregates. · `eval/evaluate.py`
- [Key-point coverage](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/eval/evaluate.py): deterministic metric · Regex/pattern checks against the required checklist. · `eval/evaluate.py`
- [Reply similarity](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/eval/evaluate.py): deterministic metric · TF-IDF cosine similarity to the historical reply. · `eval/evaluate.py`
- [Optional LLM judge](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/eval/evaluate.py): judge metric · Scores correctness, tone, and completeness when Groq is available. · `eval/evaluate.py`
- **Groq judge API**: external LLM
- **Scores and aggregates**: evaluation output · Includes covered and missed requirements, explanations, and category results.

### Experiment Run

- [Leave-one-out pipeline](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/run_pipeline.py): entry point · Generates, evaluates, summarizes, and emits the report. · `run_pipeline.py`
- **Held-out record**: evaluation unit · Its reply is excluded from retrieval but retained as evaluation ground truth.
- [Detailed report](https://github.com/shivammitra/gen-ai-email-suggested-response-system/blob/main/report.json): serialized output · Persists per-response and aggregate results without rerunning APIs. · `report.json`

## Connections

- Synthetic dataset builder → Inbox examples JSONL: writes
- Inbox examples JSONL → Leave-one-out pipeline: loads
- Leave-one-out pipeline → Held-out record: selects each item
- Leave-one-out pipeline → Excluded retrieval corpus: excludes target
- Excluded retrieval corpus → Reply generator: retrieval source
- Held-out record → Reply generator: target email
- Reply generator → TF-IDF retrieval: ranks
- TF-IDF retrieval → Few-shot prompt: retrieved pairs
- Few-shot prompt → Groq chat completion: when API available
- TF-IDF retrieval → Closest-reply fallback: when offline
- Groq chat completion → Suggested reply: generated reply
- Closest-reply fallback → Suggested reply: fallback reply
- Leave-one-out pipeline → Reply evaluator: evaluates each reply
- Suggested reply → Reply evaluator: candidate
- Held-out record → Reply evaluator: reference and checklist
- Reply evaluator → Key-point coverage: 45% base weight
- Reply evaluator → Reply similarity: 25% base weight
- Reply evaluator → Optional LLM judge: 30% when available
- Optional LLM judge → Groq judge API: requests score
- Key-point coverage → Scores and aggregates: metric detail
- Reply similarity → Scores and aggregates: metric detail
- Optional LLM judge → Scores and aggregates: optional score
- Scores and aggregates → Detailed report: serializes
- Leave-one-out pipeline → Detailed report: emits
