遞迴式分割文件
只用單一字元來分割雖然簡單又可預期,但常會得到不理想的區塊。在這個練習中,你將對先前載入的論文《Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks》套用遞迴式字元分割。
回想一下,遞迴式字元分割會走訪一串字元清單,依序嘗試在每個字元處切割,以產生長度低於 chunk_size 上限的區塊。
本練習屬於課程
使用 LangChain 的 Retrieval Augmented Generation(RAG)
練習說明
- 定義一個 LangChain 的遞迴式字元文字分割器,使用字元清單
['\n', '.', ' ', '']依序遞迴分割,chunk_size設為75,chunk_overlap設為10。 - 使用你定義的
text_splitter與合適的方法來分割document。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
loader = PyPDFLoader("rag_paper.pdf")
document = loader.load()
# Define a text splitter that splits recursively through the character list
text_splitter = ____(
____,
chunk_size=75,
chunk_overlap=10
)
# Split the document using text_splitter
chunks = text_splitter.____
print(chunks)
print([len(chunk.page_content) for chunk in chunks])