C.W.K.
Stream
Lesson 03 of 05 · published

버티는 인용과 provenance

~22 min · rag, citations

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

인용이 cosmetics 너머 중요한 이유

인용 없는 답은 verify 불가. 유저 (그리고 디버깅하는 본인) 가 source 청크 클릭해서 모델이 디테일 invent 안 했는지 확인해야. 인용은 forcing function 역할도 — 모델이 인용 prompted 되면 hallucinate 덜 함.

두 가지 인용 스타일

  • Inline numeric (Wikipedia 스타일). 모델이 생산 쉽고 render 쉽고. '... was released in 2024 [3].'
  • Inline link. source title 또는 URL 을 inline render. 시각적으로 무거운데 클릭 가능.

인용 contract

모델한테 정확히 어떤 인용 포맷 기대하는지 말하고, 그걸 깨는 답은 render 거부. [\d+] 토큰 찾아서 각각 매칭 candidate 있는지 확인하는 regex 가 hallucinate 인용 대부분 잡기에 충분.

Code

인용이 candidate set 에 매칭되는지 validate·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}')
클릭 가능한 source 와 함께 답 render·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 에 위 인용 validator 추가. 질문 30개 실행. 모든 hallucinate 인용 로깅. 제공된 source 만 인용하도록 system prompt 더 strict 하게 조정. 재실행 후 hallucination rate 떨어지는지 확인.

Progress

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

댓글 0

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

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