Skip to content

Every day, professionals wade through hundreds of emails, from urgent client requests to promotional offers. It's like trying to find important messages in a digital ocean. But AI can help you stay afloat by automatically sorting emails to highlight what matters most.

You've been asked to build an intelligent email assistant using Llama, to help users automatically classify their incoming emails. Your system will identify which emails need immediate attention, which are regular updates, and which are promotions that can wait or be archived.

The Data

You'll work with a dataset of various email examples, ranging from urgent business communications to promotional offers. Here's a peek at what you'll be working with:

email_categories_data.csv

ColumnDescription
email_idA unique identifier for each email in the dataset.
email_contentThe full email text including subject line and body. Each email follows a format of "Subject" followed by the message content on a new line.
expected_categoryThe correct classification of the email: Priority, Updates, or Promotions. This will be used to validate your model's performance.
# Run the following cells first
# Install necessary packages, then import the model running the cell below
!pip install llama-cpp-python==0.2.82 -q -q -q
Spinner
DataFrameas
df
variable
SELECT *
FROM 'models.csv'
LIMIT 5
import pandas as pd
# Load the email dataset
emails_df = pd.read_csv('data/email_categories_data.csv')
# Display the first few rows of our dataset
print("Preview of our email dataset:")
emails_df.head(20)
# Set the model path
model_path = "/files-integrations/files/c9696c24-44f3-45f7-8ccd-4b9b046e7e53/tinyllama-1.1b-chat-v0.3.Q4_K_M.gguf"
from llama_cpp import Llama
import json

llm = Llama(model_path=model_path)

email1_text = emails_df["email_content"][0]
email2_text = emails_df["email_content"][1]

print(email1_text)
print(email2_text)

prompt = """
You classify emails into Priority, Updates, or Promotions.

Email 1: Critical: Client Presentation Due
The client presentation for Project X is due in 2 hours. Requires immediate review.
Category: Priority

Email 2: Monthly Department Updates
Review this month's KPIs and upcoming projects. New policies attached for review.
Category: Updates

Email 3: Flash Sale - 24 Hours Only!
Everything must go! Massive discounts on all items. Shop now before it's too late!
Category: Promotions

Email 4: {}
Category:"""

def process_message(llm, prompt, email_text):
    
    output = llm(
        prompt.format(email_text),
        max_tokens=10,
        temperature=0,
        stop=["\n"]  # stops at end of category line
    )
    
    return output['choices'][0]["text"].strip()


# Run classification
result1 = process_message(llm, prompt, email1_text)
result2 = process_message(llm, prompt, email2_text)

print("Email 1:", result1)
print("Email 2:", result2)