Skip to content

prachitjain/Email-Triage-Assistant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Email Triage Assistant

An intelligent email summarization tool that uses Scaledown for context compression and AI to generate concise email summaries.

Overview

This project demonstrates how to build an Email Triage Assistant that:

  1. Fetches emails from your inbox (Gmail, Outlook, etc.)
  2. Uses Scaledown to compress email content by up to 80% while preserving semantic meaning
  3. Generates AI-powered summaries to help you quickly triage your inbox
  4. Identifies priority emails and action items

What is Scaledown?

Scaledown is an open-source Python package for prompt optimization and context engineering. It transforms bloated prompts into precision-engineered context, reducing tokens by 80% while preserving semantic integrity. This makes LLM calls faster and cheaper.

Features

  • 📧 Email Fetching: Connect to Gmail via IMAP
  • 🔄 Context Compression: Uses Scaledown to reduce email content tokens
  • 🤖 AI Summarization: Generates concise summaries with key points
  • 🎯 Priority Detection: Identifies urgent/important emails
  • 📊 Batch Processing: Process multiple emails efficiently
  • 💰 Cost Efficient: Reduces API costs by 80% through compression

Architecture

┌─────────────┐      ┌──────────────┐      ┌─────────────┐
│   Email     │─────▶│  Scaledown   │─────▶│    LLM      │
│   Inbox     │      │  Compressor  │      │ Summarizer  │
└─────────────┘      └──────────────┘      └─────────────┘
                            │
                            ▼
                     Token Reduction
                        (60-80%)

Installation

Prerequisites

  • Python 3.8 or higher
  • Gmail account (or other email provider)
  • Scaledown API key from https://scaledown.ai
  • OpenAI API key (or Anthropic Claude API key)

Step 1: Clone the Repository

git clone <your-repo-url>
cd email-triage-assistant

Step 2: Create Virtual Environment

python -m venv venv

# On macOS/Linux:
source venv/bin/activate

# On Windows:
venv\Scripts\activate

Step 3: Install Dependencies

pip install scaledown
pip install openai anthropic  # Choose your preferred LLM provider
pip install python-dotenv

Step 4: Configure Environment Variables

Create a .env file in the project root:

# Scaledown API Configuration
SCALEDOWN_API_KEY=sk-your-scaledown-api-key
SCALEDOWN_API_URL=https://api.scaledown.xyz

# LLM Provider (choose one)
OPENAI_API_KEY=your-openai-api-key
# OR
ANTHROPIC_API_KEY=your-anthropic-api-key

# Email Configuration (for Gmail)
EMAIL_ADDRESS=your-email@gmail.com
EMAIL_PASSWORD=your-app-specific-password
IMAP_SERVER=imap.gmail.com
IMAP_PORT=993

Step 5: Get Scaledown API Key

  1. Visit https://scaledown.ai
  2. Sign up for an account
  3. Generate your API key from the dashboard
  4. Add it to your .env file

Step 6: Set Up Gmail App Password

For Gmail:

  1. Go to Google Account settings
  2. Enable 2-Factor Authentication
  3. Generate an App Password for "Mail"
  4. Use this password in your .env file

Usage

Basic Example

from email_triage import EmailTriageAssistant

# Initialize the assistant
assistant = EmailTriageAssistant()

# Process recent emails
summaries = assistant.process_recent_emails(count=10)

# View summaries
for summary in summaries:
    print(f"From: {summary['from']}")
    print(f"Subject: {summary['subject']}")
    print(f"Priority: {summary['priority']}")
    print(f"Summary: {summary['summary']}")
    print(f"Action Items: {summary['action_items']}")
    print("-" * 80)

Advanced Usage

# Process emails with custom compression rate
assistant = EmailTriageAssistant(compression_rate=0.7)

# Filter by date range
from datetime import datetime, timedelta
since_date = datetime.now() - timedelta(days=7)
summaries = assistant.process_emails_since(since_date)

# Batch process with progress tracking
summaries = assistant.batch_process(
    count=50,
    show_progress=True,
    save_to_file="email_summaries.json"
)

Project Structure

email-triage-assistant/
├── README.md                   # This file
├── requirements.txt            # Python dependencies
├── .env.example               # Environment variables template
├── email_triage.py            # Main application code
├── email_fetcher.py           # Email fetching utilities
├── compressor.py              # Scaledown compression wrapper
├── summarizer.py              # LLM summarization logic
├── examples/
│   ├── basic_usage.py         # Simple example
│   ├── advanced_usage.py      # Advanced features demo
│   └── batch_processing.py    # Batch processing example
└── tests/
    ├── test_compressor.py     # Unit tests
    ├── test_summarizer.py
    └── test_integration.py

How It Works

1. Email Fetching

The system connects to your email inbox via IMAP and retrieves unread or recent emails.

2. Context Compression with Scaledown

Before sending to the LLM, email content is compressed:

from scaledown import ScaleDownCompressor

compressor = ScaleDownCompressor(
    target_model="gpt-4o",
    rate="auto"  # Automatically determines optimal compression
)

# Original email: 2000 tokens
result = compressor.compress(
    context=email_body,
    prompt="Summarize this email with key points and action items"
)

# Compressed email: ~400 tokens (80% reduction!)
print(f"Token reduction: {result.metrics.original_prompt_tokens} → "
      f"{result.metrics.compressed_prompt_tokens}")

3. AI Summarization

The compressed context is sent to an LLM for summarization:

# Using OpenAI
import openai

response = openai.ChatCompletion.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "system", "content": "You are an email triage assistant."},
        {"role": "user", "content": result.compressed_prompt}
    ]
)

4. Priority Detection

The system analyzes summaries to detect:

  • Urgency indicators (deadlines, "ASAP", "urgent")
  • Importance markers (from VIPs, meeting requests)
  • Action requirements (tasks, approvals needed)

Benefits of Using Scaledown

Cost Savings

Without Scaledown:
- 100 emails × 2000 tokens each = 200,000 tokens
- At $0.003/1K tokens = $0.60 per batch

With Scaledown (80% reduction):
- 100 emails × 400 tokens each = 40,000 tokens
- At $0.003/1K tokens = $0.12 per batch
- Savings: $0.48 per batch (80% cost reduction)

Speed Improvement

  • Reduced tokens mean faster LLM responses
  • Typical response time improvement: 50-70%
  • Better for real-time applications

Accuracy

Scaledown preserves semantic meaning while removing redundancy:

  • Email signatures and footers
  • Quoted previous messages
  • Verbose greetings
  • Marketing fluff

Configuration Options

Compression Settings

# Automatic compression (recommended)
compressor = ScaleDownCompressor(rate="auto")

# Fixed compression rate
compressor = ScaleDownCompressor(rate=0.5)  # 50% reduction

# Target token count
compressor = ScaleDownCompressor(
    target_tokens=500,
    target_model="gpt-4o"
)

Email Filtering

# Filter by sender
assistant.process_emails(sender_filter="boss@company.com")

# Filter by subject keywords
assistant.process_emails(subject_keywords=["invoice", "urgent"])

# Filter by date
from datetime import datetime, timedelta
since = datetime.now() - timedelta(days=3)
assistant.process_emails(since_date=since)

API Reference

EmailTriageAssistant

class EmailTriageAssistant:
    def __init__(
        self,
        compression_rate: str = "auto",
        llm_provider: str = "openai",
        llm_model: str = "gpt-4o-mini"
    )
    
    def process_recent_emails(self, count: int = 10) -> List[Dict]
    
    def process_emails_since(self, since_date: datetime) -> List[Dict]
    
    def batch_process(
        self,
        count: int,
        show_progress: bool = True,
        save_to_file: str = None
    ) -> List[Dict]

Testing

Run the test suite:

# Install test dependencies
pip install pytest pytest-cov

# Run all tests
pytest

# Run with coverage
pytest --cov=. --cov-report=html

Troubleshooting

Common Issues

  1. Authentication Failed

    • Ensure you're using an App Password for Gmail
    • Check that 2FA is enabled on your Google account
  2. Scaledown API Error

    • Verify your API key is correct
    • Check your account has available credits
    • Ensure SCALEDOWN_API_URL is set correctly
  3. Rate Limiting

    • Add delays between batch processing
    • Consider caching results
    • Use compression to reduce token usage

Performance Tips

  1. Optimize Compression Rate: Start with "auto" and adjust based on results
  2. Batch Processing: Process emails in batches of 10-20 for best performance
  3. Cache Results: Store summaries to avoid reprocessing
  4. Filter Aggressively: Only process emails you need to triage

Example Output

From: john.doe@company.com
Subject: Q4 Budget Review Meeting - Action Required
Priority: ⚠️ HIGH

Summary:
John requests your review of the Q4 budget proposal before Friday's meeting.
The finance team identified a 15% variance in marketing spend that needs
explanation. Your input on resource allocation for Project Phoenix is needed.

Action Items:
- Review attached Q4 budget spreadsheet
- Prepare variance explanation for marketing department
- Submit Project Phoenix resource requirements by Thursday 5 PM

Token Savings: 1,847 → 312 tokens (83% reduction)
Processing Time: 1.2s

Contributing

Contributions are welcome! Please:

  1. Fork the repository
  2. Create a feature branch
  3. Add tests for new functionality
  4. Submit a pull request

Acknowledgments

  • Scaledown Team for the excellent context compression library
  • Built with OpenAI GPT and Anthropic Claude APIs
  • Inspired by modern context engineering practices

Resources

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors