from typing import Optional


def _truncate_doc(
    doc: str,
    max_len: Optional[int] = None,
) -> str:
    """
    Truncate a document to max_len by the rough number of tokens
    """
    if not max_len:
        return doc
    if len(doc.split()) > max_len:
        doc = " ".join(doc.split()[:max_len])
        return doc
    return doc