Francisco Zanartu commited on
Commit ·
2d7f9db
1
Parent(s): 8c422df
refactor: simplify context enrichment by removing window context and integrating text splitter. Window context already in text_spliter's chunk_overlap parameter.
Browse files- src/utils/chunking.py +13 -31
src/utils/chunking.py
CHANGED
|
@@ -10,16 +10,9 @@ def get_context_enriched_chunks(
|
|
| 10 |
llm,
|
| 11 |
chunk_size: int = 1000,
|
| 12 |
chunk_overlap: int = 200,
|
| 13 |
-
window_size: int = 2,
|
| 14 |
):
|
| 15 |
|
| 16 |
-
|
| 17 |
-
separators=["\n\n", "\n", ".", " "], # Paragraph → Line → Sentence → Word
|
| 18 |
-
chunk_size=chunk_size,
|
| 19 |
-
chunk_overlap=chunk_overlap,
|
| 20 |
-
)
|
| 21 |
-
|
| 22 |
-
base_chunks = text_splitter.split_text(document_text)
|
| 23 |
|
| 24 |
document_overview = document_summary(document_text, llm)
|
| 25 |
|
|
@@ -27,18 +20,11 @@ def get_context_enriched_chunks(
|
|
| 27 |
for i, chunk in enumerate(base_chunks):
|
| 28 |
print(f"Processing chunk {i+1}/{len(base_chunks)}")
|
| 29 |
|
| 30 |
-
context_chunks, window_start, window_end = get_context_window(
|
| 31 |
-
base_chunks, i, window_size
|
| 32 |
-
)
|
| 33 |
-
|
| 34 |
doc = create_enriched_document(
|
| 35 |
document_overview,
|
| 36 |
chunk,
|
| 37 |
-
context_chunks,
|
| 38 |
i,
|
| 39 |
len(base_chunks),
|
| 40 |
-
window_start,
|
| 41 |
-
window_end,
|
| 42 |
llm,
|
| 43 |
)
|
| 44 |
|
|
@@ -50,27 +36,20 @@ def get_context_enriched_chunks(
|
|
| 50 |
def create_enriched_document(
|
| 51 |
document_overview,
|
| 52 |
chunk,
|
| 53 |
-
context_chunks,
|
| 54 |
chunk_id,
|
| 55 |
total_chunks,
|
| 56 |
-
window_start,
|
| 57 |
-
window_end,
|
| 58 |
llm,
|
| 59 |
):
|
| 60 |
-
context_text = " ".join(context_chunks)
|
| 61 |
|
| 62 |
metadata = {
|
| 63 |
"chunk_id": chunk_id,
|
| 64 |
"total_chunks": total_chunks,
|
| 65 |
"chunk_size": len(chunk),
|
| 66 |
-
"window_start_idx": window_start,
|
| 67 |
-
"window_end_idx": window_end - 1,
|
| 68 |
-
"has_context": len(context_chunks) > 0,
|
| 69 |
"chunk": chunk,
|
| 70 |
"document_summary": document_overview,
|
| 71 |
}
|
| 72 |
|
| 73 |
-
chunk_summary = summarize_context(document_overview,
|
| 74 |
|
| 75 |
metadata["chunk_summary"] = chunk_summary
|
| 76 |
|
|
@@ -111,13 +90,16 @@ def summarize_context(document_overview, context_text, llm):
|
|
| 111 |
return response.content
|
| 112 |
|
| 113 |
|
| 114 |
-
def
|
| 115 |
-
|
| 116 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
|
| 118 |
-
|
| 119 |
-
context_chunks = [
|
| 120 |
-
chunk for j, chunk in enumerate(window) if j != current_index - window_start
|
| 121 |
-
]
|
| 122 |
|
| 123 |
-
return
|
|
|
|
| 10 |
llm,
|
| 11 |
chunk_size: int = 1000,
|
| 12 |
chunk_overlap: int = 200,
|
|
|
|
| 13 |
):
|
| 14 |
|
| 15 |
+
base_chunks = get_base_chunks(document_text, chunk_size, chunk_overlap)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
document_overview = document_summary(document_text, llm)
|
| 18 |
|
|
|
|
| 20 |
for i, chunk in enumerate(base_chunks):
|
| 21 |
print(f"Processing chunk {i+1}/{len(base_chunks)}")
|
| 22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
doc = create_enriched_document(
|
| 24 |
document_overview,
|
| 25 |
chunk,
|
|
|
|
| 26 |
i,
|
| 27 |
len(base_chunks),
|
|
|
|
|
|
|
| 28 |
llm,
|
| 29 |
)
|
| 30 |
|
|
|
|
| 36 |
def create_enriched_document(
|
| 37 |
document_overview,
|
| 38 |
chunk,
|
|
|
|
| 39 |
chunk_id,
|
| 40 |
total_chunks,
|
|
|
|
|
|
|
| 41 |
llm,
|
| 42 |
):
|
|
|
|
| 43 |
|
| 44 |
metadata = {
|
| 45 |
"chunk_id": chunk_id,
|
| 46 |
"total_chunks": total_chunks,
|
| 47 |
"chunk_size": len(chunk),
|
|
|
|
|
|
|
|
|
|
| 48 |
"chunk": chunk,
|
| 49 |
"document_summary": document_overview,
|
| 50 |
}
|
| 51 |
|
| 52 |
+
chunk_summary = summarize_context(document_overview, chunk, llm)
|
| 53 |
|
| 54 |
metadata["chunk_summary"] = chunk_summary
|
| 55 |
|
|
|
|
| 90 |
return response.content
|
| 91 |
|
| 92 |
|
| 93 |
+
def get_base_chunks(document_text, chunk_size, chunk_overlap):
|
| 94 |
+
|
| 95 |
+
text_splitter = RecursiveCharacterTextSplitter(
|
| 96 |
+
separators=["\n\n", "\n", ".", " "], # Paragraph → Line → Sentence → Word
|
| 97 |
+
chunk_size=chunk_size,
|
| 98 |
+
chunk_overlap=chunk_overlap,
|
| 99 |
+
add_start_index=True,
|
| 100 |
+
strip_whitespace=False,
|
| 101 |
+
)
|
| 102 |
|
| 103 |
+
base_chunks = text_splitter.split_text(document_text)
|
|
|
|
|
|
|
|
|
|
| 104 |
|
| 105 |
+
return base_chunks
|