핵심 요약
대형 언어 모델의 할루시네이션 문제를 해결하기 위해 답변의 불확실성을 스스로 인지하고 보완하는 시스템이 필요하다. 본 튜토리얼은 답변 생성, 자기 평가, 웹 검색 및 합성의 3단계로 구성된 추론 파이프라인을 제안한다. 모델은 각 답변에 대해 수치화된 신뢰도 점수를 부여하며, 점수가 임계값보다 낮을 경우 DuckDuckGo를 통해 실시간 정보를 검색하여 답변의 정확도를 높인다. 이를 통해 개발자는 더 투명하고 신뢰할 수 있는 AI 애플리케이션을 구축할 수 있다.
배경
Python 기초, OpenAI API 사용법, JSON 데이터 구조 이해
대상 독자
LLM의 신뢰성을 높이고자 하는 AI 애플리케이션 개발자
의미 / 영향
이 방식은 AI가 모르는 것을 아는 척하는 문제를 완화하며, 특히 최신 정보가 중요한 뉴스나 금융 분야의 AI 비서 구축에 필수적인 설계 패턴을 제시한다.
섹션별 상세
시스템은 답변 생성 시 신뢰도 점수(0.0~1.0)와 그 근거를 함께 출력하도록 강제하는 구조화된 JSON 형식을 사용한다.
python
SYSTEM_UNCERTAINTY = """
You are an expert AI assistant that is HONEST about what it knows and doesn't know.
For every question you MUST respond with valid JSON only (no markdown, no prose outside JSON):
{
"answer": "",
"confidence": ,
"reasoning": ""
}
Confidence scale:
0.90-1.00 → very high: well-established fact, you are certain
0.75-0.89 → high: strong knowledge, minor uncertainty
0.55-0.74 → medium: plausible but you may be wrong, could be outdated
0.30-0.54 → low: significant uncertainty, answer is a best guess
0.00-0.29 → very low: mostly guessing, minimal reliable knowledge
""".strip()모델이 답변과 함께 수치화된 신뢰도를 출력하도록 유도하는 시스템 프롬프트 정의
자기 평가(Self-Evaluation) 단계에서 모델은 자신의 답변을 비판적으로 검토하고 논리적 일관성과 사실 관계를 확인하여 신뢰도 점수를 수정한다.
python
def self_evaluate(response: LLMResponse) -> LLMResponse:
critique_prompt = f"""
Review this answer and its stated confidence. Check for:
1. Logical consistency
2. Whether the confidence matches the actual quality of the answer
3. Any factual errors you can spot
Question: {response.question}
Proposed answer: {response.answer}
Stated confidence: {response.confidence}
Stated reasoning: {response.reasoning}
Respond in JSON: {{ "revised_confidence": , "critique": "", "revised_answer": "" }}
""".strip()
// ...(중략)
ev = json.loads(completion.choices[0].message.content)
response.confidence = float(ev.get("revised_confidence", response.confidence))
response.answer = ev.get("revised_answer", response.answer)
return response생성된 답변을 모델이 스스로 비판하고 신뢰도 점수를 수정하는 자기 평가 로직
신뢰도 점수가 설정된 임계값(예: 0.55) 이하로 떨어지면 시스템은 자동으로 DuckDuckGo 검색 API를 호출하여 최신 정보를 수집한다.
python
def research_and_synthesize(response: LLMResponse) -> LLMResponse:
console.print(f" [yellow]🔍 Confidence {response.confidence:.0%} is low — triggering auto-research...[/yellow]")
snippets = web_search(response.question)
if not snippets:
return response
synthesis_prompt = f"""
Question: {response.question}
Preliminary answer (low confidence): {response.answer}
Web search snippets: {formatted}
Synthesize an improved answer using the evidence above.
""".strip()
// ...(중략)
syn = json.loads(completion.choices[0].message.content)
response.answer = syn.get("answer", response.answer)
response.confidence = float(syn.get("confidence", response.confidence))
return response신뢰도가 낮을 경우 웹 검색을 수행하고 결과를 종합하여 답변을 개선하는 로직
수집된 검색 결과와 초기 답변을 결합하여 최종 답변을 합성(Synthesis)하며, 이 과정에서 사용된 출처와 수정된 신뢰도를 함께 제공한다.
Rich 라이브러리를 활용하여 콘솔 환경에서 신뢰도 수준을 시각적으로 표시하고 대화형 모드를 통해 실시간으로 파이프라인을 테스트할 수 있다.
실무 Takeaway
- LLM에 신뢰도 척도를 정의한 시스템 프롬프트를 제공하여 모델이 자신의 지식 한계를 수치로 표현하게 유도할 수 있다.
- 자기 비판(Self-Critic) 단계를 추가함으로써 초기 답변의 오류를 사전에 필터링하고 더 객관적인 신뢰도 점수를 확보할 수 있다.
- 실시간 검색 도구를 조건부로 결합하여 지식 컷오프 이후의 최신 정보나 전문적인 수치 데이터에 대한 정확도를 획기적으로 개선할 수 있다.
언급된 리소스
튜토리얼Full Notebook
AI 분석 전체 내용 보기
AI 요약 · 북마크 · 개인 피드 설정 — 무료