File size: 4,097 Bytes
c00ea7b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import tiktoken
import re

ENCODING = tiktoken.encoding_for_model("gpt-4o-mini")

def chunk_text(text:str, source_file:str, doc_type:str,  chunk_size:int = 500, overlap:int = 50) -> list[dict]:

    segments = split_by_code_blocks(text)

    chunks = []
    current_chunk = ""

    for segment in segments:
        code = segment["text"]

        if segment["type"] == 'code':
            if  count_tokens(code) < 50:
                current_chunk += "\n\n" + code
                continue

            # code block — never split it
            # if current chunk has content, save it first
            if  current_chunk.strip():
                chunks.append({
                    "text":current_chunk.strip(), 
                    "source_file": source_file, 
                    "doc_type":doc_type, 
                    "chunk_index":len(chunks)
                })
                current_chunk = get_last_n_tokens(current_chunk, overlap)

            # code block becomes its own chunk regardless of size
            chunks.append({
                "text":code.strip(),
                "source_file":source_file,
                "doc_type":doc_type,
                "chunk_index":len(chunks)
            })

            # reset — code block breaks the flow
            current_chunk = ""
        else:
            paragraphs = segment["text"].split("\n\n")

            for paragraph in paragraphs:

                if not paragraph.strip():
                    continue

                # If adding this paragraph exceeds chunk_size tokens, save and slide window
                if count_tokens(current_chunk + paragraph) > chunk_size:
                    if current_chunk.strip():
                        chunks.append({
                                "text":current_chunk.strip(), 
                                "source_file": source_file, 
                                "doc_type":doc_type, 
                                "chunk_index":len(chunks)
                            })
                    current_chunk = get_last_n_tokens(current_chunk, overlap)
                    
                    if count_tokens(paragraph) > chunk_size:
                        chunks.append({
                            "text": paragraph.strip(),
                            "source_file": source_file,
                            "doc_type": doc_type,
                            "chunk_index": len(chunks)
                        })
                        current_chunk = get_last_n_tokens(paragraph, overlap)
                        continue

                current_chunk += "\n\n" + paragraph

    
    if current_chunk.strip():
        chunks.append({
            "text":current_chunk.strip(), 
            "source_file": source_file, 
            "doc_type":doc_type, 
            "chunk_index":len(chunks)
        })

    return chunks


def split_by_code_blocks(text:str)-> list[dict]:
    parts = re.split(r"```[\s\S]*?```",text)

    segments = []
    for part in parts:
        if part.startswith("```"):
            segments.append({"type":"code", "text":part})
        else:
            part.strip()
            segments.append({"type":"prose", "text":part})
    return segments


def count_tokens(text:str)-> int:
    num_tokens = len(ENCODING.encode(text))
    return num_tokens

def get_last_n_tokens(text:str, overlap:int) -> str:
    tokens = ENCODING.encode(text)
    last_tokens = tokens[-overlap:]    
    return ENCODING.decode(last_tokens)  


if __name__ == "__main__":
    from pathlib import Path

    source_file = "knowledge_base/async_book/02_execution/02_future.md"
    text = Path(source_file).read_text()
    chunks = chunk_text( text=text, source_file=source_file, doc_type="tokio_tutorial")

    print(f"Total chunks: {len(chunks)}")

    for i, chunk in enumerate(chunks[:3]):
        print(f"\n---- chunk {i} ---")
        print(chunk["text"])
        print(count_tokens(chunk["text"]))

    # check for oversized chunks
    oversized = [i for i, c in enumerate(chunks) if count_tokens(c["text"]) > 500]
    print(f"\nOversized chunks: {oversized}")