Skip to main content

Understanding the RAG Algorithm

What Is RAG?

RAG (Retrieval-Augmented Generation) is a technique in which the AI first retrieves relevant documents and then generates an answer based on their content.

Put simply, it is like letting the AI take an "open-book exam." Instead of answering from memorized knowledge alone, it looks up the relevant material first.

사용자 질문


① 검색 (Retrieval)
"질문과 관련된 문서 조각을 찾아라"


② 컨텍스트 구성
검색된 문서 조각들을 LLM에게 전달


③ 생성 (Generation)
LLM이 문서를 참고하여 답변 생성


답변 (출처 포함)

The PlantPulse RAG Pipeline

Step 1: Document Indexing (Preparation)

The process of converting documents into a form the AI can search.

원본 문서 (PDF, DOCX, 이미지)


┌─────────────────────────┐
│ Docling 파서 │
│ 문서 → 텍스트 + 이미지 │
│ + 테이블 구조 추출 │
└────────────┬────────────┘


┌─────────────────────────┐
│ 청킹 (Chunking) │
│ 긴 문서를 512토큰 단위로 │
│ 적절한 크기로 분할 │
│ (50토큰 오버랩) │
└────────────┬────────────┘


┌─────────────────────────┐
│ 임베딩 (Embedding) │
│ 텍스트 → 2560차원 벡터 │
│ 의미를 숫자 배열로 변환 │
└────────┬────────┬───────┘
│ │
▼ ▼
┌────────┐ ┌────────┐
│ Qdrant │ │ Neo4j │
│ 벡터DB │ │ 그래프 │
└────────┘ └────────┘

What Is Chunking?

Searching a 10-page manual as a single unit is inefficient. Splitting it into appropriately sized pieces lets you retrieve only the relevant portion.

[원본 문서: 10페이지 매뉴얼]

↓ 청킹

[청크 1] 1장 개요 부분 (512토큰)
[청크 2] 1장 뒷부분 ~ 2장 앞부분 (512토큰, 50토큰 오버랩)
[청크 3] 2장 중반부 (512토큰, 50토큰 오버랩)
...

Overlap: To keep context from breaking at chunk boundaries, the last 50 tokens of the previous chunk are included in the next one.

What Is Embedding?

It converts the meaning of text into an array of numbers (a vector). Sentences with similar meanings end up close together in vector space.

"펌프 베어링 교체 방법" → [0.12, -0.34, 0.56, ..., 0.78] (2560차원)
"펌프 축수 정비 절차" → [0.11, -0.33, 0.55, ..., 0.77] ← 비슷한 벡터!
"오늘 날씨가 좋다" → [0.89, 0.23, -0.67, ..., 0.12] ← 다른 벡터

Step 2: Retrieval

When a user question arrives, the system finds the relevant document chunks.

Dense Retrieval (Meaning-Based)

The question is vectorized with the same embedding model, and the nearest document chunks in vector space are retrieved.

질문: "주입기 베어링 교체 주기는?"

▼ 임베딩
질문 벡터: [0.13, -0.35, 0.54, ...]

▼ 코사인 유사도 계산

문서 청크 A (점수 0.92): "주입기 베어링은 6개월마다 교체..." ← 선택!
문서 청크 B (점수 0.85): "펌프 축수 정비 시 베어링 점검..." ← 선택!
문서 청크 C (점수 0.31): "작업장 안전 수칙..." ← 제외

Advantage: It understands semantic similarity, such as "replacement cycle" = "maintenance interval."

Sparse Retrieval (Keyword-Based)

Retrieval by exact word matching, based on the BM25 algorithm.

질문: "TAG_DJ_M_01_1_1093 허용 온도"

▼ 키워드 추출
["TAG_DJ_M_01_1_1093", "허용", "온도"]

▼ 키워드 매칭

문서 청크 A: "TAG_DJ_M_01_1_1093의 허용 온도 범위는..." ← 정확히 매칭!

Advantage: It precisely finds unique identifiers such as equipment IDs and model names.

Hybrid Retrieval (PlantPulse Default Mode)

Combining Dense and Sparse retrieval delivers both the comprehension of semantic search and the precision of keyword search.

최종 점수 = Dense 점수 × α + Sparse 점수 × (1-α)
info

PlantPulse RAG uses hybrid mode by default. Technical documents contain many specialized terms and equipment IDs, so keyword matching matters.


Step 3: Reranking

A reranker model re-evaluates the initial retrieval results (Top 10) in detail and selects the final Top 5.

초기 검색 결과 (Top 10)


┌─────────────────────────────┐
│ Reranker (rerank-multilingual-v3.0) │
│ │
│ 질문과 각 문서 청크를 │
│ 쌍으로 비교하여 │
│ 관련도를 정밀 평가 │
└──────────────┬──────────────┘


최종 결과 (Top 5) — 가장 관련도 높은 문서만 전달

The reranker uses a more sophisticated model than the retrieval stage to score question-document relevance. It catches subtle relevance that the retrieval stage missed.


Step 4: Generation

The retrieved document chunks are passed to the LLM as context to generate the answer.

[시스템 프롬프트]
아래 문서를 참고하여 질문에 답하세요.

[검색된 문서]
문서 1: "주입기 베어링은 6개월마다 교체하며..."
문서 2: "교체 시 SKF 6205-2RS 규격을 사용..."

[사용자 질문]
주입기 베어링 교체 주기와 규격을 알려줘.

↓ LLM 생성

[답변]
주입기 베어링 교체 주기는 6개월이며, SKF 6205-2RS 규격을 사용합니다.
(출처: 정비 매뉴얼 3장)

Comparison of the Four Retrieval Modes

ModeHow It WorksBest Suited For
hybridCombines Dense + SparseGeneral document search (default, recommended)
naiveDense only (semantic)Conceptual questions ("What is OEE?")
localDetailed search within a documentChecking specifics in a particular document
globalExplores relationships across documents (KG)Consolidated information spanning multiple documents

Knowledge Graph RAG (Graph RAG)

In addition to vector search, PlantPulse RAG supports retrieval using a Neo4j knowledge graph.

문서 인덱싱 시:
텍스트 → 엔티티 추출 → 관계 추출 → Neo4j 그래프 저장

검색 시 (global 모드):
질문 → 관련 엔티티 → 그래프 탐색 → 연결된 문서 청크

Advantage: It enables relationship-based retrieval, such as "all maintenance manuals related to the injector."