# About Email Categorization Using AI

Email categorization allows automatically determining the category of incoming emails using artificial intelligence and forwarding them to operators for quick processing. This reduces the need for manual processing and improves response speed to client requests.

---

## Context and Problem

In many cases, you need to automatically process incoming emails:
- Large number of emails requires quick processing
- Different types of requests require different operators or topics
- Manual categorization takes a lot of time
- Quick response to client requests is needed

AI categorization solves this task by allowing automatically determining email category and forwarding it to the appropriate operator.

---

## Key Concepts

### Email Processing Stages

**Stage 1: Receiving Email**
- `receive_email_message` block automatically reads new emails
- Email text is saved in `email_message`
- Email subject is saved in `email_subject`
- Sender address is saved in `email_to`

**Stage 2: Categorization Using AI**
- `fastline_completion` block uses AI to analyze text
- AI determines the best request category
- Result is saved in `completion` (or specified variable)

**Stage 3: Forwarding to Operator Panel**
- Email with category is forwarded to operator panel
- Category corresponds to topic (subject) in operator panel
- Topic must have an alias that matches AI value

---

## Approach Options

### AI Categorization vs Manual Categorization

**AI Categorization:**
- ✅ Pros: Speed, automation, scalability
- ❌ Cons: Possible errors, prompt configuration needed

**Manual Categorization:**
- ✅ Pros: Accuracy, control
- ❌ Cons: Slowness, does not scale

**Why we use AI:**
AI allows automatically processing large numbers of emails and quickly forwarding them to operators, which improves response speed to client requests.

---

## Adopted Solutions

### Using fastline_completion

Action `fastline_completion` is used to analyze email text and determine category. The prompt should:
- Describe categorization task
- Specify available categories
- Return only one category

### Topic Alias Matching

The value returned by AI must exactly match the topic alias in the operator panel. This ensures correct assignment of email to the corresponding topic.

---

## Implications for Users and Implementation

### For Integrators

When configuring categorization, it's important to:

1. **Configure receive_email_message** — for receiving emails
2. **Create AI prompt** — with category descriptions and instructions
3. **Configure topics in operator panel** — with aliases that match AI categories
4. **Check categorization accuracy** — configure prompt for better results

### Common Errors

**Error:** AI returns incorrect category  
**Problem:** Insufficiently detailed prompt or incorrect instructions  
**Solution:** Improve prompt, add examples and clear instructions

**Error:** Email is not assigned to topic  
**Problem:** Topic alias does not match AI value  
**Solution:** Check topic alias matching with values returned by AI

**Error:** Categorization works slowly  
**Problem:** Slow model used or large prompt  
**Solution:** Use faster model or optimize prompt

---

## Usage Examples

### fastline_completion Configuration

```json
{
  "model": "gpt-4o-mini",
  "temperature": 0.3,
  "prompt": "You need to categorize client emails. Based on the provided message '{{email_message}}' and subject '{{email_subject}}', determine the best matching topic from the following: consultation, complaint, clarification. Return only one topic without additional explanations.\nImportant: choose only one category. If none of them fit, return 'Other' (other)."
}
```

### operator_panel__connect_to_operator_with_msg Configuration

```json
{
  "subject_alias": "{{completion}}"
}
```

---

## Related Documents

- **Explanation:** [Connecting Chat with Operator](/en/operatorline/explanation/connect-chat-to-operator.md) — forwarding to operator panel

