How shivammitra/gen-ai-email-suggested-response-system works
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: evaluate.py Reply evaluator · run_pipeline.py Leave-one-out pipeline · generator.py TF-IDF retrieval · dataset.jsonl Inbox examples JSONL
Components 18
Dataset
- Synthetic dataset builder · Reproducibly creates PII-free inbox examples.
data/generate_dataset.py - Inbox examples JSONL · Incoming email, historical reply, category, and required key points.
data/dataset.jsonl
Generation
- Excluded retrieval corpus · All dataset examples except the target record.
- Reply generator · Owns retrieval, prompting, and runtime fallback.
src/generator.py - TF-IDF retrieval · Ranks historical emails by cosine similarity.
src/generator.py - Few-shot prompt · Combines retrieved email/reply pairs with the target email.
src/generator.py - Groq chat completion · Uses the OpenAI-compatible Groq SDK when available.
- Closest-reply fallback · Returns a reply derived from the highest-ranked retrieval.
src/generator.py - Suggested reply
Evaluation
- Reply evaluator · Returns per-item details, composite scores, and category aggregates.
eval/evaluate.py - Key-point coverage · Regex/pattern checks against the required checklist.
eval/evaluate.py - Reply similarity · TF-IDF cosine similarity to the historical reply.
eval/evaluate.py - Optional LLM judge · Scores correctness, tone, and completeness when Groq is available.
eval/evaluate.py - Groq judge API
- Scores and aggregates · Includes covered and missed requirements, explanations, and category results.
Experiment Run
- Leave-one-out pipeline · Generates, evaluates, summarizes, and emits the report.
run_pipeline.py - Held-out record · Its reply is excluded from retrieval but retained as evaluation ground truth.
- Detailed report · Persists per-response and aggregate results without rerunning APIs.
report.json
Connections 24
- 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