Hallucinations are confident but incorrect LLM outputs — a fundamental challenge requiring multiple defences.
# Main causes
# 1. Knowledge gaps (model generates rather than admits ignorance)
# 2. Conflation of facts from different sources
# 3. High temperature settings
# 4. Leading questions that assume false premises
# Defence 1: RAG (ground answers in retrieved facts)
system = 'Answer using ONLY the provided context.'
+ ' Say you do not know if not in context.'
# Defence 2: Uncertainty prompts
prompt = '''Answer the question.
If you are not 100% confident, say: I am not certain -- please verify.
Never guess or make up facts or names.'''
# Defence 3: Require citations
prompt = 'After each fact, cite the source: [Source: doc_name]'
# Defence 4: Chain-of-thought verification
prompt = '''Answer the question.
Then verify: Does my answer match only the provided context?
Are there any claims without evidence? If so, remove them.'''
# Defence 5: Low temperature for factual tasks
temperature = 0.0 # deterministic, no creativity
# Defence 6: Post-processing cross-encoder check
from sentence_transformers import CrossEncoder
nli = CrossEncoder('cross-encoder/nli-deberta-v3-small')
score = nli.predict([[context, answer]]) # entailment score