본문 바로가기
C.W.K.
Stream
Lesson 01 of 05 · published

검색 결과의 계약

~20 min · rag, design

Level 0Scout
0 XP0/41 lessons0/10 achievements
0/120 XP to next level120 XP to go0% complete

검색 단계가 생성 단계에 넘겨야 할 것

RAG는 검색과 생성 사이에 명확한 계약이 있을 때 작동해. 검색한 청크, 각 청크의 출처, 결과를 얼마나 믿을지 판단할 신호가 필요해. 하나라도 빠지면 LLM이 근거 없이 답하거나 인용을 만들지 못해.

모든 검색 결과에 필요한 세 가지

  1. 텍스트 — 임베딩할 때 사용한 청크 내용 그대로야.
  2. 출처 — 파일 경로, URL, 문서 ID, 청크 순서처럼 생성기와 사용자가 원문을 되짚을 수 있는 정보야.
  3. 점수 — 유사도나 재순위화 점수로, 신뢰도가 낮은 결과가 프롬프트에 들어가기 전에 걸러.

최소한으로 작동하는 RAG 순환

question -> embed -> retrieve top-k -> filter by score -> stuff into prompt -> generate -> render with citations

쿼리 재작성, 다중 쿼리, 재순위화, 요약은 모두 최적화 단계야. 먼저 이 기본 순환을 작동시키고, 평가 집합이 실제로 약하다고 보여 주는 부분만 개선해.

Code

30줄 MVP RAG 루프·python
def rag(question: str, k: int = 5, min_score: float = 0.3) -> dict:
    candidates = retrieve(question, k=k * 4)
    candidates = [c for c in candidates if c['score'] >= min_score]
    candidates = candidates[:k]

    if not candidates:
        return {'answer': "I don't have enough information to answer that.",
                'citations': []}

    context = '\n\n'.join(
        f'[{i+1}] (source: {c["source"]})\n{c["text"]}'
        for i, c in enumerate(candidates)
    )
    system = (
        'Answer using ONLY the provided context. Cite sources as [1], [2], etc. '
        'If the context is insufficient, say so explicitly.'
    )
    prompt = f'Context:\n{context}\n\nQuestion: {question}'
    answer = llm.complete(prompt, system=system)
    return {'answer': answer, 'citations': candidates}

External links

Exercise

직접 가진 코퍼스에 위의 최소 RAG 순환을 구현하고 질문 열 개를 실행해. 실제 출처를 인용한 횟수, 근거 없는 내용을 만든 횟수, 정확히 '모르겠다'고 답한 횟수를 측정해. 가장 심한 실패 유형 하나를 골라 다음에 고쳐.

Progress

Progress is local-only — sign in to sync across devices.
이 페이지에서 버그를 발견하셨거나 피드백이 있으세요?문제 신고

댓글 0

🔔 답글 알림 (로그인 필요)
로그인댓글을 남기려면 로그인해 주세요.

아직 댓글이 없어요. 첫 댓글을 남겨보세요.