r"""Returns successive n-sized chunks from provided text. Split a text into smaller chunks of size n". Args: text (str): The text to be split. n (int): The max length of a single chunk. Returns: List[str]: A list of split texts.
(text: str, n: int)
| 487 | |
| 488 | |
| 489 | def create_chunks(text: str, n: int) -> List[str]: |
| 490 | r"""Returns successive n-sized chunks from provided text. Split a text |
| 491 | into smaller chunks of size n". |
| 492 | |
| 493 | Args: |
| 494 | text (str): The text to be split. |
| 495 | n (int): The max length of a single chunk. |
| 496 | |
| 497 | Returns: |
| 498 | List[str]: A list of split texts. |
| 499 | """ |
| 500 | |
| 501 | chunks = [] |
| 502 | i = 0 |
| 503 | while i < len(text): |
| 504 | # Find the nearest end of sentence within a range of 0.5 * n |
| 505 | # and 1.5 * n tokens |
| 506 | j = min(i + int(1.2 * n), len(text)) |
| 507 | while j > i + int(0.8 * n): |
| 508 | # Decode the tokens and check for full stop or newline |
| 509 | chunk = text[i:j] |
| 510 | if chunk.endswith(".") or chunk.endswith("\n"): |
| 511 | break |
| 512 | j -= 1 |
| 513 | # If no end of sentence found, use n tokens as the chunk size |
| 514 | if j == i + int(0.8 * n): |
| 515 | j = min(i + n, len(text)) |
| 516 | chunks.append(text[i:j]) |
| 517 | i = j |
| 518 | return chunks |
| 519 | |
| 520 | |
| 521 | def is_docker_running() -> bool: |