File size: 668 Bytes
c4233b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

from typing import List
import tiktoken
from src.config import TOKEN_ENCODING, CHUNK_TOKENS, CHUNK_OVERLAP

_enc = tiktoken.get_encoding(TOKEN_ENCODING)

def chunk_text(text: str, chunk_tokens: int = CHUNK_TOKENS, overlap_tokens: int = CHUNK_OVERLAP) -> List[str]:
    tokens = _enc.encode(text)
    chunks = []
    start = 0

    while start < len(tokens):
        end = min(start + chunk_tokens, len(tokens))
        chunk = _enc.decode(tokens[start:end]).strip()
        if chunk:
            chunks.append(chunk)

        start = end - overlap_tokens
        if start < 0:
            start = 0
        if end == len(tokens):
            break

    return chunks