문서 전처리기

문서 전처리기 (DocumentPreprocessor)

DocumentPreprocessor는 텍스트 문서 리스트를 더 짧은 텍스트 문서 리스트로 나눈 뒤, 정리(cleaning)를 거쳐 읽기 좋게 만들고 하나의 컴포넌트로 묶어 주는 SuperComponent예요. DocumentSplitterDocumentCleaner를 결합한 것이므로, 인덱싱 파이프라인에서 컨버터(Converter) 뒤에 놓으세요.

출처: 공식문서

주요 정보

항목
파이프라인에서 가장 흔한 위치 인덱싱 파이프라인의 컨버터
필수 실행 변수 documents: 문서 리스트
출력 변수 documents: 분할·정리된 문서 리스트
API 레퍼런스 PreProcessors
GitHub 링크 https://github.com/deepset-ai/haystack/blob/main/haystack/components/preprocessors/document_preprocessor.py
패키지 이름 haystack-ai

개요

DocumentPreprocessor는 문서를 먼저 분할(split)하고 그다음 정리(clean)해요.

이것은 DocumentSplitterDocumentCleaner를 하나의 컴포넌트로 결합한 SuperComponent예요.

파라미터

DocumentPreprocessor는 내부의 DocumentSplitterDocumentCleaner의 모든 초기화 파라미터를 노출하며, 모두 선택 사항이에요. 그 파라미터들의 상세 설명은 각각의 문서 페이지에 있어요.

사용법

단독으로 쓰기

from haystack import Document
from haystack.components.preprocessors import DocumentPreprocessor

doc = Document(content="I love pizza!")
preprocessor = DocumentPreprocessor()

result = preprocessor.run(documents=[doc])
print(result["documents"])

파이프라인 안에서 쓰기

인덱싱 파이프라인에서 DocumentPreprocessor를 사용할 수 있어요. 아래 예시는 MultiFileConverter의 추가 의존성 설치가 필요해요.

pip install pypdf markdown-it-py  mdit_plain trafilatura python-pptx python-docx jq openpyxl tabulate pandas
from haystack import Pipeline
from haystack.components.converters import MultiFileConverter
from haystack.components.preprocessors import DocumentPreprocessor
from haystack.components.writers import DocumentWriter
from haystack.document_stores.in_memory import InMemoryDocumentStore

document_store = InMemoryDocumentStore()

pipeline = Pipeline()
pipeline.add_component("converter", MultiFileConverter())
pipeline.add_component("preprocessor", DocumentPreprocessor())
pipeline.add_component("writer", DocumentWriter(document_store=document_store))
pipeline.connect("converter", "preprocessor")
pipeline.connect("preprocessor", "writer")

result = pipeline.run(data={"sources": ["test.txt", "test.pdf"]})
print(result)
# {'writer': {'documents_written': 3}}

더 알아보기