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

검증 가능한 인용과 출처 추적

~22 min · rag, citations

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

인용은 꾸밈보다 훨씬 중요해

인용이 없는 답은 검증할 수 없어. 사용자와 개발자가 출처 청크를 열어 모델이 세부 내용을 지어내지 않았는지 확인할 수 있어야 해. 인용을 요구하는 것 자체도 모델이 근거 없는 내용을 덜 만들게 하는 제약으로 작동해.

두 가지 인용 방식

  • 본문 속 번호 — 위키백과처럼 '... was released in 2024 [3].'로 표시해. 모델이 만들기 쉽고 화면에 그리기도 쉬워.
  • 본문 속 링크 — 출처 제목이나 URL을 바로 링크로 보여 줘. 화면은 조금 무거워지지만 클릭할 수 있어.

인용 형식의 계약

모델에 기대하는 인용 형식을 정확히 알려 주고, 그 형식을 어긴 답은 화면에 내보내지 마. 정규식으로 [\d+] 토큰을 찾아 실제 후보와 하나씩 대조하는 것만으로도 지어낸 인용 대부분을 잡을 수 있어.

Code

인용이 후보 집합과 일치하는지 검증하기·python
import re

def validate_citations(answer: str, candidates: list[dict]) -> tuple[bool, list[int]]:
    cited = sorted({int(m.group(1)) for m in re.finditer(r'\[(\d+)\]', answer)})
    valid_range = set(range(1, len(candidates) + 1))
    bad = [c for c in cited if c not in valid_range]
    return (len(bad) == 0, bad)

result = rag(question)
ok, bad = validate_citations(result['answer'], result['citations'])
if not ok:
    print(f'WARN: hallucinated citations {bad}')
클릭 가능한 출처와 함께 답변 표시하기·python
def render(answer: str, candidates: list[dict]) -> str:
    sources_md = '\n'.join(
        f'[{i+1}] [{c["source"]}]({c["source"]})  — chunk {c["chunk_index"]}'
        for i, c in enumerate(candidates)
    )
    return f'{answer}\n\n---\n\n**Sources**\n{sources_md}'

External links

Exercise

RAG에 위의 인용 검증기를 추가하고 질문 30개를 실행해. 지어낸 인용을 모두 기록한 뒤, 제공한 출처만 인용하도록 시스템 프롬프트를 더 엄격하게 바꿔. 다시 실행해 잘못된 인용 비율이 줄었는지 확인해.

Progress

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

댓글 0

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

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