내부 문서에서 질문에 답하는 LLM 기반 시스템(RAG)을 만들려면 문서 로딩, 청킹, 임베딩, 검색, LLM으로의 전달을 매번 연결해야 한다. LlamaIndex는 이 연결을 제공하는 프레임워크다. 이를 설치한 뒤 네트워크를 차단하고 LLM 계층을 비활성화한 상태에서 문서 인덱싱과 검색이 작동하는지 테스트했다.
LlamaIndex는 문서 로딩부터 임베딩 인덱스 구축, 검색, LLM으로의 전달까지 다루는 RAG 프레임워크다. LlamaIndex의 소스에는 MIT License가 적용된다(*1).
1. 설치하기
FROM docker.io/library/python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'llama-index-core==0.14.23' 'llama-index-embeddings-huggingface==0.7.0'
COPY warmup_model.py /tmp/warmup_model.py
RUN python /tmp/warmup_model.py
WORKDIR /work
podman build -t llama-index-sandbox .
2. LLM 없이 인덱싱과 검색 테스트하기
Settings.llm = None으로 설정하면 LLM 부분이 비활성화되므로, 임베딩 모델만 사용해 인덱싱과 검색을 테스트했다. 짧은 문서 네 개를 등록하고, 어느 문서와도 단어가 거의 겹치지 않는 질의로 검색했다. 네트워크 접속은 차단한 상태에서 테스트를 실행했다.
from llama_index.core import Document, VectorStoreIndex, Settings
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
Settings.embed_model = HuggingFaceEmbedding(model_name="sentence-transformers/all-MiniLM-L6-v2")
Settings.llm = None
documents = [
Document(text="Restarting the router fixes the wifi connection issue in most cases."),
Document(text="The invoice was rejected because the tax identifier field was empty."),
Document(text="Water the tomato plants every morning before the sun gets strong."),
Document(text="A missing tax ID caused the payment validation to fail."),
]
index = VectorStoreIndex.from_documents(documents)
retriever = index.as_retriever(similarity_top_k=2)
query = "billing rejected, tax number missing"
nodes = retriever.retrieve(query)
for node in nodes:
print(f"score={node.score:.4f} {node.text}")
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
-e HF_HUB_OFFLINE=1 -e TRANSFORMERS_OFFLINE=1 \
-v "$(pwd)":/work -w /work llama-index-sandbox \
python index_demo.py
-v를 사용하면 지정한 디렉터리를 컨테이너에서 읽고 쓸 수 있다. 실험용으로 따로 마련한 빈 디렉터리에서 실행한다. 캐시된 모델이 있어도 임베딩 모델의 기반 라이브러리가 온라인 접속을 시도하는 것을 막으려면 HF_HUB_OFFLINE과 TRANSFORMERS_OFFLINE이 필요했다. 헤드룸 기사를 쓴 뒤로도 이 알려진 증상을 여러 번 겪었다.
출력은 다음과 같았다.
QUERY: billing rejected, tax number missing
score=0.6907 A missing tax ID caused the payment validation to fail.
score=0.6777 The invoice was rejected because the tax identifier field was empty.
두 세금·청구 문서는 질의와 겹치는 단어가 거의 없었지만 상위 두 자리를 차지했다. 검색 결과에서는 Wi-Fi 문서와 토마토에 물을 주는 문서가 제외됐다. LLM이 없어도 문서 로딩, 인덱싱, 검색 파이프라인은 독립적으로 작동했다.
검색부터 연결 상태를 검증하기
RAG 시스템을 만들 때 LLM 호출까지 전체 흐름을 한 번에 테스트하면, 실패 원인이 인덱싱, 검색, 생성 중 어디에서 발생했는지 분리하기 어려워진다. LlamaIndex를 사용하면 LLM 계층을 제거하고 검색만 따로 테스트할 수 있으므로, 생성 테스트에 앞서 기본 연결이 제대로 구성됐는지 확인할 수 있다.
출처
*1 LlamaIndex (GitHub), https://github.com/run-llama/llama_index, MIT License