def chunk(text, size=700, overlap=100): """Split on sentence ends so an idea is never cut in half.""" import re sentences = re.split(r'(?<=[.!?])\s+', text.strip()) chunks, current = [], '' for s in sentences: if len(current) + len(s) > size and current: chunks.append(current.strip()) # carry the tail forward so context survives the boundary current = current[-overlap:] + ' ' current += s + ' ' if current.strip(): chunks.append(current.strip()) return chunks