문자를 기준으로 재귀적으로 분할하기
많은 개발자들이 특정 문자 목록을 기준으로 문서를 분할하기 위해 재귀적 문자 분할기를 사용해요. 기본적으로 이 문자들은 문단, 줄바꿈, 공백, 빈 문자열이며, 다음과 같아요: ["\n\n", "\n", " ", ""].
이 분할기는 먼저 문단 기준으로 나누고, chunk_size와 chunk_overlap 조건이 충족되는지 확인한 뒤, 충족되지 않으면 문장, 단어, 개별 문자 순으로 더 잘게 나눠요.
대부분의 경우, 문서에 잘 맞는 값을 찾기 위해 chunk_size와 chunk_overlap을 여러 가지로 시도해 봐야 해요.
이 연습은 강의의 일부입니다
LangChain으로 LLM 애플리케이션 개발하기
연습 안내
langchain_text_splitters에서RecursiveCharacterTextSplitter클래스를 임포트하세요.separators=["\n", " ", ""],chunk_size=24,chunk_overlap=10으로RecursiveCharacterTextSplitter인스턴스를 생성하세요..split_text()메서드로quote를 분할하고, 청크와 각 청크의 길이를 출력하세요.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
# Import the recursive character splitter
from langchain_text_splitters import ____
quote = 'Words are flowing out like endless rain into a paper cup,\nthey slither while they pass,\nthey slip away across the universe.'
chunk_size = 24
chunk_overlap = 10
# Create an instance of the splitter class
splitter = RecursiveCharacterTextSplitter(
separators=____,
chunk_size=____,
chunk_overlap=____)
# Split the document and print the chunks
docs = splitter.____(quote)
print(docs)
print([len(doc) for doc in docs])