Estimated time: 2–3 hours
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.
# 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
═══════════════════════════════════════
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
═══════════════════════════════════════
.txt files with standard open().pdf files with pypdf.PdfReaderDocumentSummary with these fields:
title: str — inferred from content if not explicitdocument_type: str — e.g. “legal contract”, “technical report”, “email thread”estimated_word_count: intsummary: str — 2–4 sentences, length depends on detail levelkey_points: list[str] — 3–6 bullet pointsaction_items: list[str] — may be empty0.3 for consistency--json flag prints the raw Pydantic model as JSON (model.model_dump_json(indent=2))gpt-4oread_document(path: str) -> str that handles both .txt and .pdf.DocumentSummary Pydantic model.summarize(text: str, detail: str) -> DocumentSummary function with the API call.argparse for --detail and --json..txt and a .pdf file.test.txt file — Wikipedia articles are well-structured and representative of real document summarisation tasks.
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
"""
.txt file.pdf file--detail brief produces a noticeably shorter summary than --detail detailed--json prints valid JSON outputgpt-4o with a warning--output summary.md to write a formatted Markdown fileFinished this project? Share your GitHub repo link in the discussion below. Browse what others built to get inspiration and give feedback.