🤖 AI/ML

Hallucination

Hallucination

A phenomenon where an LLM generates content that is not true. Mitigated with RAG.

📖 Detailed Explanation

Hallucination is a phenomenon in which an LLM confidently generates information that is not factual as if it were true. Typical examples include citing papers that do not exist, describing events that did not actually happen, or presenting incorrect information as if it were accurate.

The root cause of Hallucination lies in how LLMs work. Because an LLM generates text by predicting the "most likely next token," it can produce content that looks statistically plausible but is not factual.

Hallucination is broadly classified into two types. Intrinsic Hallucination is generating content that contradicts the input information, and Extrinsic Hallucination is adding unverifiable information that is not in the input.

Strategies for mitigating Hallucination include RAG (Retrieval-Augmented Generation), Grounding, Chain-of-Thought prompting, setting a low temperature, and building a post-hoc verification system for outputs. Complete elimination is impossible, but it can be reduced considerably.

💻 Code Example

from openai import OpenAI

# Hallucination 감소를 위한 프롬프트 전략
def reduce_hallucination_query(question, context=None):
    client = OpenAI()

    system_prompt = """당신은 정확한 정보만 제공하는 AI 어시스턴트입니다.
    다음 규칙을 반드시 따르세요:
    1. 확실하지 않은 정보는 "확실하지 않습니다"라고 말하세요
    2. 제공된 컨텍스트에 없는 정보는 추측하지 마세요
    3. 출처를 알 수 없는 통계나 수치는 사용하지 마세요
    4. 모르는 것은 모른다고 솔직하게 답하세요"""

    messages = [{"role": "system", "content": system_prompt}]

    if context:
        messages.append({
            "role": "user",
            "content": f"참고 문서:\n{context}\n\n질문: {question}\n\n위 문서만을 기반으로 답변하세요."
        })
    else:
        messages.append({"role": "user", "content": question})

    response = client.chat.completions.create(
        model="gpt-4",
        messages=messages,
        temperature=0.1,  # 낮은 temperature로 창의성 제한
        max_tokens=500
    )

    return response.choices[0].message.content

# 사용 예시 - Grounding으로 Hallucination 방지
context = """
회사 휴가 정책:
- 연차: 입사 1년차 15일, 2년차 이상 20일
- 병가: 연간 10일 (진단서 필요)
- 경조사: 결혼 5일, 출산 10일
"""

answer = reduce_hallucination_query(
    "회사 연차가 며칠인가요?",
    context=context
)
print(answer)

🗣️ Say It Like This in Practice

In an AI service quality meeting

"Our customer inquiry chatbot experienced Hallucination and gave out an incorrect refund policy. If we have it reference the official documents via RAG and lower the temperature to 0.2, this kind of problem is greatly reduced."

In a technical interview

"Completely eliminating Hallucination is impossible. This is because the essence of an LLM is probabilistic text generation. Instead, we need to ground the model with RAG and build a Human-in-the-Loop process in which people always review important information."

In a product review

"In a legal advisory AI, Hallucination is fatal. We need to train the model to be able to answer 'I'm not certain,' and add a step that verifies in the DB whether a cited precedent actually exists."

Take a mock interview on this topic → WIA SOOM AI Interview · Start for free

⚠️ Common Mistakes & Cautions

1
Confident wrong answers

LLMs answer with great confidence even when the information is wrong. For important decisions, always cross-verify with other sources.

2
Caution with citations and references

A large number of the papers, books, and URLs that LLMs cite do not exist. Always check them yourself and verify the actual sources.

3
Caution with high-risk domains

In the medical, legal, and financial fields, Hallucination can lead to serious consequences. In these fields, expert review is essential.

🔗 Related Terms

📚 Learn More