노드 파서 사용 패턴

노드 파서 사용 패턴

노드 파서는 문서 목록을 받아 Node 객체로 청킹하는 간단한 추상화로, 각 노드가 부모 문서의 특정 청크가 되게 해요. 문서가 노드로 분해되면 그 모든 속성(metadata, 텍스트·메타데이터 템플릿 등)이 자식 노드에 상속돼요. Node와 Document 프로퍼티에 대해 더 알아보려면 여기를 읽어보세요.

출처: 문서

본문

시작하기

단독 사용 (Standalone Usage)

노드 파서는 단독으로 사용할 수 있어요:

from llama_index.core import Document
from llama_index.core.node_parser import SentenceSplitter


node_parser = SentenceSplitter(chunk_size=1024, chunk_overlap=20)


nodes = node_parser.get_nodes_from_documents(
    [Document(text="long text")], show_progress=False
)

변환 사용 (Transformation Usage)

노드 파서는 ingestion pipeline과 함께 어떤 변환 세트에도 포함될 수 있어요.

from llama_index.core import SimpleDirectoryReader
from llama_index.core.ingestion import IngestionPipeline
from llama_index.core.node_parser import TokenTextSplitter


documents = SimpleDirectoryReader("./data").load_data()


pipeline = IngestionPipeline(transformations=[TokenTextSplitter(), ...])


nodes = pipeline.run(documents=documents)

인덱스 사용 (Index Usage)

또는 transformations 안이나 전역 설정에 넣어 .from_documents()로 인덱스를 만들 때 자동으로 사용되게 할 수 있어요:

from llama_index.core import SimpleDirectoryReader, VectorStoreIndex
from llama_index.core.node_parser import SentenceSplitter


documents = SimpleDirectoryReader("./data").load_data()


# global
from llama_index.core import Settings


Settings.text_splitter = SentenceSplitter(chunk_size=1024, chunk_overlap=20)


# per-index
index = VectorStoreIndex.from_documents(
    documents,
    transformations=[SentenceSplitter(chunk_size=1024, chunk_overlap=20)],
)

모듈 (Modules)

전체 모듈 가이드를 확인하세요.

더 알아보기 (Learn more)