在 RAG 系統裡,文檔切分 (Chunking) 是一個看似小細節,卻極大影響檢索與答案準確性的環節。 想像一下:如果你把一本百科全書切成一頁一頁,那檢索會很快,但每頁可能都缺乏上下文;反之,如果整本書作為一個 Chunk,雖然資訊完整,但向量檢索幾乎失效。
因此,如何「切得剛剛好」,就是文檔切分的核心科學。
一個簡單的決策樹:
def choose_chunking_strategy(doc_type, size, structure):
if size < 10000 and structure == "simple":
return "fixed_length"
elif doc_type in ["legal", "technical"]:
return "semantic_split"
elif structure == "hierarchical":
return "recursive_split"
elif doc_type == "conversation":
return "sliding_window"
else:
return "hybrid"
工程實務上,常見做法是:
如何知道「Chunking 切得好不好」?
小技巧:可以針對同一份文件,嘗試不同切分策略,再用小樣本測試問答效果,找出最適合的配置。