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

피드백 순환과 스스로 고치는 검색

~22 min · rag, advanced

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

한 번만 검색하는 RAG의 한계

한 번 검색하고 한 번 답하는 방식은 명확한 질문에는 잘 맞아. 하지만 쿼리가 모호하거나 한 번의 검색으로 필요한 자료를 모두 찾기 어려운 복합 질문에서는 깨져. 다음 두 방식으로 그 한계를 넘을 수 있어.

Self-RAG: 모델이 자기 답을 평가해

답을 만든 뒤 검색한 청크가 그 답을 얼마나 뒷받침하는지 모델에 점수로 평가하게 해. 점수가 낮으면 쿼리를 다시 쓰거나 k를 늘려 재시도해. LLM 호출이 한 번 더 들지만 품질 검문소가 시스템 안에 생겨.

도구를 사용하는 검색

LLM에 검색 도구를 주고 언제 호출할지 스스로 결정하게 해. 모델은 여러 단계를 거치는 쿼리도 계획할 수 있어. 먼저 넓은 문맥을 찾고, 거기서 발견한 정보로 다음 검색을 더 좁히는 식이지. Anthropic의 tool-use, OpenAI의 function calling, 현대적인 에이전트 프레임워크가 모두 이런 방식으로 검색을 제공해.

Code

Self-RAG 자기평가 순환·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 + 다시 시도
도구로 제공하는 검색·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이 호출할 수 있는 검색 도구로 연결해. '2023년의 기존 보존 규칙을 갱신한 가장 최근 정책은 언제 나왔나?' 같은 다단계 질문 열 개에 한 번만 검색하는 RAG와 도구 기반 검색을 각각 적용해. 답변 품질과 전체 도구 호출 횟수를 기록해.

Progress

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

댓글 0

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

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