PDF Layout Parser 및 사내 비전 전처리 pipeline
RagService의 ragcenter.layout_parser는 PDF를 사내에서만 처리하는 high-end 전처리 경로다. pdfplumber가 각 page의 text word, table bounding box, embedded image bounding box를 읽고, y/x reading order로 text → table → figure region을 구성한다. table region은 원본 cell order를 유지한 Markdown과 HTML로 만들며, text stream 뒤에 일괄 부착하지 않고 bounding box 순서에 맞춰 병합한다.
| 구성 요소 | 역할 | 외부 전송 여부 |
|---|---|---|
parse_pdf_layout | PDF word/table/image layout 감지와 local page crop rendering | 없음 |
LayoutRegion | page, bounding box, kind, reading order, Markdown/HTML representation 보존 | 없음 |
PrivateVisionClient | OpenAI-compatible internal Qwen2-VL 계열 endpoint에 figure crop 전달 | internal allow-list endpoint만 |
VisionCaption | 사실 기반 Korean caption과 보이는 numeric data JSON | RAG Markdown에 provenance와 함께 병합 |
parse_pdf_with_layout | layout result를 기존 StructuredDocument로 adapt하여 batch ingestion·Semantic Chunking에 연결 | 없음 |
활성화
기본 PDF parser는 text/table 중심의 경량 경로를 사용한다. 이미지 crop·VLM 호출은 비용과 GPU memory를 고려해 명시적으로 활성화한다.
RAG_SERVICE_ENABLE_PDF_LAYOUT_PARSER=true
PRIVATE_VISION_URL=http://vision-api:8001
PRIVATE_VISION_MODEL=qwen2-vl
PRIVATE_VISION_ALLOWED_HOSTS=vision-api
docker compose -f docker-compose.offline.yml --profile vision up -d vision-api ragcenter-apivision-api profile은 local HuggingFace cache volume에 사전 적재한 Qwen2-VL-compatible model을 vLLM OpenAI endpoint로 serve한다. PRIVATE_VISION_URL은 loopback, RFC1918 IP, .local, .internal, 또는 PRIVATE_VISION_ALLOWED_HOSTS의 명시 host만 허용한다. public endpoint와 외부 image hosting URL은 reject한다.
Figure caption과 수치 JSON 계약
사내 VLM에는 figure crop과 다음 제한 prompt만 전달한다.
보이는 이미지의 정보만 설명하고, 추측·외부 지식·개인정보 복원을 금지한다.
caption과numeric_data외 형식은 허용하지 않는다. 이미지에서 식별할 수 없는 수치는 빈 배열로 반환한다.
응답은 {"caption": "...", "numeric_data": [{"label": "...", "value": "...", "unit": "..."}]} 형태의 JSON object여야 한다. JSON parse/schema validation에 실패하면 raw model text를 저장하거나 stream에 노출하지 않고 안전한 failure caption과 warning을 남긴다.
표 구조 보존
clean_pdf_table은 완전히 비어 있는 row·column만 제거하고 ragged row는 padding한다. 열 삭제 또는 left-shift로 금액·날짜가 잘못된 header 아래로 옮겨지는 것을 막는다. 각 표는 아래 두 형식으로 제공된다.
| 등급 | 인상률 |
| --- | --- |
| A | 7% |<table data-rag-layout="table"><tbody><tr><td>등급</td><td>인상률</td></tr><tr><td>A</td><td>7%</td></tr></tbody></table>운영 검증 범위
tests/test_layout_parser.py는 reading order, table Markdown/HTML, internal endpoint allow-list, VLM JSON extraction을 검증한다. 실제 Qwen2-VL weights와 GPU throughput은 사내 cache/model에서 별도 smoke test해야 하며, 실제 sample PDF에서는 figure crop의 bounding box·caption·numeric data를 human review하는 승인 단계를 두어야 한다.