</>
Skip to content
Gen AI lessons (29/39)

Gen AI — Hallucinations

What are hallucinations?

When AI generates plausible but incorrect or fabricated information.

Types

  1. Factual errors: Wrong information
  2. Fabrication: Invented facts or sources
  3. Logical errors: Incorrect reasoning
  4. Citation errors: Fake references

Detection

def check_hallucination(response, sources):
    prompt = f"""
Check if this response is supported by the sources:

Response: {response}
Sources: {sources}

List any unsupported claims.
"""
    return client.chat.completions.create(
        model="gpt-4",
        messages=[{"role": "user", "content": prompt}]
    )

Prevention strategies

  1. RAG: Ground responses in retrieved documents
  2. Prompting: Ask for confidence levels
  3. Verification: Cross-check with reliable sources
  4. Citation: Require sources in responses
# Prompt to reduce hallucination
prompt = """
Answer the question based ONLY on the provided context.
If the context doesn't contain the answer, say "I don't have enough information."
Do not make up information.
"""

Confidence scoring

prompt = f"""
Answer the question and rate your confidence (1-5):

Question: {question}

Format:
Answer: [your answer]
Confidence: [1-5]
Reasoning: [why this confidence level]
"""

Human oversight

  • Review AI outputs before publishing
  • Verify critical information
  • Use AI as assistant, not authority

Mini Practice

  1. Identify hallucinations in responses
  2. Implement RAG to reduce them
  3. Build a confidence scoring system
  4. Test prevention strategies

Up Next

Continue with Safety - Responsible AI development.

Related Topics

Frequently Asked Questions about Hallucinations

What is Hallucinations in Gen AI?

Hallucinations is a fundamental concept in Gen AI. This lesson explains it step by step with clear examples, making it easy for beginners to understand.

How do I learn Hallucinations?

Start by reading the explanation above, then try the code examples. Practice by modifying the examples and experimenting with different values. Hands-on practice is the best way to learn Hallucinations.

Why is Hallucinations important in Gen AI?

Hallucinations is essential for Gen AI development. Understanding this concept will help you write better code and solve real-world problems more effectively.