04. Automated Responses
Drafting responses requires more from the model than classification. The response must be factually accurate, match company tone, and actually address the sender's question or concern.
The response generation approach uses few-shot prompting to establish the desired format and tone:
RESPONSE_TEMPLATE = """You are a customer service assistant for a mid-sized company. Write professional, concise email responses.
TONE: Helpful but professional. Do not be overly casual or robotic.
LENGTH: 2-4 sentences for simple questions. Up to 2 paragraphs for complex issues.
ACCURACY: Only state facts you are certain about. Do not make up policies or prices.
FORMAT: Plain text, no emojis, no markdown formatting.
EXAMPLES:
Perfect Example (greeting):
"Thank you for reaching out. Yes, we do offer overnight shipping for all domestic orders over $50. You can select this option at checkout."
Perfect Example (referral):
"Thank you for your patience. I'm escalating your account question to our billing specialists who can access your specific records. Expect a response within 24 hours."
CONTEXT:
Customer's question about: {topic}
{relevant_context}
Customer Email:
{email_body}
Write a response:
"""
def generate_response(email_body, topic_hint="general inquiry", context=None):
"""Generate an email response."""
context_section = ""
if context:
context_section = f"Relevant information: {context}"
prompt = RESPONSE_TEMPLATE.format(
topic=topic_hint,
email_body=email_body,
relevant_context=context_section
)
response = chat(model='llama3.1:8b', messages=[
{'role': 'user', 'content': prompt}
])
return response['message']['content']
Different email types require different approaches. Acknowledgment emails are straightforward—you confirm receipt and provide a timeline. Support responses need careful handling to avoid hallucinating solutions. Sales responses must be accurate about pricing and availability.
A critical production pattern is the review step. Even with high confidence, automated drafts should go to a queue for human review before sending, especially in the early stages of deployment. Capture user feedback on draft quality and use it to refine prompts.
def create_draft(email_data):
"""Create a response draft for human review."""
classification = classify_email(email_data['subject'], email_data['body'])
topic_hints = {
'support_request': 'technical support',
'sales_inquiry': 'product information and pricing',
'customer_complaint': 'customer satisfaction concern'
}
draft = generate_response(
email_data['body'],
topic_hint=topic_hints.get(classification['category'], 'general inquiry'),
context=get_relevant_context(classification['category'])
)
return {
'draft': draft,
'original_email': email_data,
'classification': classification,
'status': 'pending_review'
}
Write three "perfect examples" that match your company's actual voice. Adapt the template and test drafts on ten real emails.