■ HTMLSectionSplitter 클래스에서 생성한 문서 리스트를 RecursiveCharacterTextSplitter 객체를 사용해 청크 크기를 제한하는 방법을 보여준다.
▶ main.py
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 |
from langchain_text_splitters import HTMLSectionSplitter from langchain_text_splitters import RecursiveCharacterTextSplitter htmlString = """ <!DOCTYPE html> <html> <body> <div> <h1>Foo</h1> <p>Some intro text about Foo.</p> <div> <h2>Bar main section</h2> <p>Some intro text about Bar.</p> <h3>Bar subsection 1</h3> <p>Some text about the first subtopic of Bar.</p> <h3>Bar subsection 2</h3> <p>Some text about the second subtopic of Bar.</p> </div> <div> <h2>Baz</h2> <p>Some text about Baz</p> </div> <br> <p>Some concluding text about Foo</p> </div> </body> </html> """ headerTupeListToSplitOn = [ ("h1", "Header 1"), ("h2", "Header 2"), ("h3", "Header 3"), ] htmlSectionSplitter = HTMLSectionSplitter(headerTupeListToSplitOn) documentList1 = htmlSectionSplitter.split_text(htmlString) recursiveCharacterTextSplitter = RecursiveCharacterTextSplitter(chunk_size = 100, chunk_overlap = 10) documentList2 = recursiveCharacterTextSplitter.split_documents(documentList1) print(documentList2) """ [ Document(page_content='Foo \n Some intro text about Foo.', metadata={'Header 1': 'Foo'}), Document(page_content='Bar main section \n Some intro text about Bar.', metadata={'Header 2': 'Bar main section'}), Document(page_content='Bar subsection 1 \n Some text about the first subtopic of Bar.', metadata={'Header 3': 'Bar subsection 1'}), Document(page_content='Bar subsection 2 \n Some text about the second subtopic of Bar.', metadata={'Header 3': 'Bar subsection 2'}), Document(page_content='Baz \n Some text about Baz \n \n \n Some concluding text about Foo', metadata={'Header 2': 'Baz'}) ] """ |
▶ requirements.txt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
annotated-types==0.7.0 beautifulsoup4==4.12.3 bs4==0.0.2 certifi==2024.6.2 charset-normalizer==3.3.2 idna==3.7 jsonpatch==1.33 jsonpointer==3.0.0 langchain-core==0.2.10 langchain-text-splitters==0.2.2 langsmith==0.1.82 lxml==5.2.2 orjson==3.10.5 packaging==24.1 pydantic==2.7.4 pydantic_core==2.18.4 PyYAML==6.0.1 requests==2.32.3 soupsieve==2.5 tenacity==8.4.2 typing_extensions==4.12.2 urllib3==2.2.2 |
※ psp install langchain-text-splitters lxml bs4 명령을 실행했다.