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

Feedback 루프와 self-correcting retrieval

~22 min · rag, advanced

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

single-pass RAG 천장

한 retrieve → 한 답은 crisp 질문에 fine. 모호한 query 또는 한 검색이 다 못 가져오는 복합 질문에서 깨짐. 두 패턴이 이 천장 넘게 push.

Self-RAG: 모델이 자기 답 판단

생성 후 모델한테 답의 retrieved 청크에 대한 groundedness score 요청. score 낮으면 rewritten query 또는 expanded k 로 retry. 비용은 LLM 호출 한 번 추가; win 은 built-in 품질 게이트.

Agentic retrieval

LLM 에 'search' 도구 주고 언제 호출할지 결정하게 함. 모델이 multi-hop query plan 가능: 먼저 일반 context retrieve, 그 다음 발견한 거에 informed 된 follow-up 검색으로 좁힘. Anthropic 의 tool-use, OpenAI 의 function calling, 모던 agent framework 가 다 이렇게 retrieval expose.

Code

Self-RAG critique 루프·python
def rag_with_self_critique(question: str, max_retries: int = 2):
    for attempt in range(max_retries + 1):
        result = rag(question)
        critique_prompt = (
            f'Question: {question}\n\n'
            f'Answer: {result["answer"]}\n\n'
            f'Context used: {[c["text"][:200] for c in result["citations"]]}\n\n'
            f'Score the answer 1-5 on how well the context supports it. '
            f'Output: just the number, then a one-line reason.'
        )
        score_line = llm.complete(critique_prompt)
        score = int(score_line.strip()[0])
        if score >= 4 or attempt == max_retries:
            return result
        question = expand_query(question, n=1)[0]   # rewrite + 다시 시도
도구로서의 검색 (agentic)·python
tools = [
    {
        'name': 'search_knowledge_base',
        'description': 'Search the company knowledge base for the given query. Returns top-5 chunks with sources.',
        'input_schema': {
            'type': 'object',
            'properties': {'query': {'type': 'string'}},
            'required': ['query'],
        },
    }
]

response = anthropic_client.messages.create(
    model='claude-opus-4-7',
    max_tokens=2000,
    tools=tools,
    messages=[{'role': 'user', 'content': 'What is our refund policy for annual plans?'}],
)

# 루프: tool_use 블록 읽고, retrieve() 호출, tool_result 로 feed back, 모델이 텍스트로 응답할 때까지.

External links

Exercise

RAG 를 LLM 에 expose 한 search 도구 뒤에 wire. multi-hop 질문 10개 ('legacy 2023 retention rule 을 update 하는 가장 최근 정책은 언제?') 에 single-pass RAG vs agentic retrieval 비교. 답 품질 + 총 tool-call 횟수 노트.

Progress

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

댓글 0

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

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