Home

Chapter 1 · Project

Build a document summarizer

Estimated time: 2–3 hours

Overview

Document summarisation is one of the most common real AI engineering tasks — legal teams summarise contracts, operations teams summarise incident reports, sales teams summarise call transcripts. You are building a CLI tool that takes a .txt or .pdf file and produces a structured summary, using everything from Lessons 1–6: API calls, structured outputs, XML-tagged prompts, and cost tracking.

Prerequisites

  • Lesson 4: Pydantic models, .parse()
  • Lesson 5: XML tags, system prompts
  • Lesson 6: temperature, cost tracking, retry logic
  • Install: uv add pypdf (for PDF reading — pypdf)

Specification

Usage

# Summarise a .txt file
uv run summarize.py report.txt

# Summarise a .pdf file
uv run summarize.py contract.pdf

# Choose detail level
uv run summarize.py report.txt --detail brief
uv run summarize.py report.txt --detail standard   # default
uv run summarize.py report.txt --detail detailed

# Output as JSON (structured fields only)
uv run summarize.py report.txt --json

Output (standard mode)

═══════════════════════════════════════
  DOCUMENT SUMMARY
═══════════════════════════════════════
Title:       Q3 2024 Incident Report
Type:        technical report
Word count:  ~2,400
─────────────────────────────────────
SUMMARY
The Q3 incident report covers three major outages between July and September...

KEY POINTS
• The longest outage (4h 22m) was caused by a misconfigured load balancer
• Two incidents were traced to a single library dependency
• Remediation involved rolling back to version 2.1.4

ACTION ITEMS
• Audit all third-party library upgrade PRs before merge
• Add circuit breaker to the payment service
─────────────────────────────────────
Cost: $0.000089 | Tokens: 1,243
═══════════════════════════════════════

Requirements

Suggested approach

  1. Build the file reader: a function read_document(path: str) -> str that handles both .txt and .pdf.
  2. Define the DocumentSummary Pydantic model.
  3. Write the summarize(text: str, detail: str) -> DocumentSummary function with the API call.
  4. Add the formatted print output.
  5. Wire up argparse for --detail and --json.
  6. Add the word count check and model switching.
  7. Test with both a .txt and a .pdf file.
Need a test document? Paste any Wikipedia article into a test.txt file — Wikipedia articles are well-structured and representative of real document summarisation tasks.

Prompt structure guidance

Structure your user message like this:

user_message = f"""
<document>
{document_text}
</document>

Summarise the document above. Extract all required fields accurately.
The detail level for this summary is: {detail}
- brief: 1–2 sentence summary, 3 key points
- standard: 2–4 sentence summary, 3–5 key points
- detailed: 4–6 sentence summary, 5–6 key points with sub-points
"""

Completion checklist

Extension challenges

Community Showcase

Finished this project? Share your GitHub repo link in the discussion below. Browse what others built to get inspiration and give feedback.