시작하기무료로 시작하기

문자 단위로 분할하기

Retrieval Augmented Generation(RAG)을 구현할 때 핵심 과정 중 하나는 문서를 벡터 데이터베이스에 저장하기 위해 여러 청크로 나누는 일입니다.

LangChain에는 여러 가지 분할 전략이 있으며, 일부는 다른 것보다 더 복잡한 절차를 사용합니다. 이번 연습에서는 문자를 기준으로 문서를 분리하고 청크 길이를 문자 수로 측정하는 character text splitter를 구현해 보겠습니다.

완벽한 분할 전략은 없습니다. 사용 사례에 맞는 방법을 찾기 위해 여러 방법을 시도해 볼 필요가 있어요.

이 연습은 강의의 일부입니다

LangChain으로 LLM 애플리케이션 개발하기

강의 보기

연습 안내

  • langchain_text_splitters에서 CharacterTextSplitter 클래스를 임포트하세요.
  • separator="\n", chunk_size=24, chunk_overlap=10으로 CharacterTextSplitter 인스턴스를 생성하세요.
  • .split_text() 메서드로 quote를 분할한 다음, 청크와 각 청크의 길이를 출력하세요.

실습형 인터랙티브 연습

이 예제를 이 샘플 코드를 완성하여 풀어보세요.

# Import the 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 = CharacterTextSplitter(
    separator=____,
    chunk_size=____,
    chunk_overlap=____)

# Split the string and print the chunks
docs = splitter.____(quote)
print(docs)
print([len(doc) for doc in docs])
코드 편집 및 실행