How to reduce AI hallucinations
Quick answer
To reduce AI hallucinations, use precise prompt engineering with clear context and constraints, select models known for factual accuracy like
claude-3-5-sonnet-20241022, and implement output verification with external knowledge sources or retrieval-augmented generation. Combining these approaches minimizes fabricated or incorrect AI responses.PREREQUISITES
Python 3.8+OpenAI API key (free tier works)pip install openai>=1.0
Setup
Install the OpenAI Python SDK and set your API key as an environment variable to securely authenticate requests.
pip install openai>=1.0 Step by step
This example demonstrates reducing hallucinations by providing clear instructions and context to the gpt-4o model, and verifying output with a simple check.
import os
from openai import OpenAI
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
prompt = (
"You are a helpful assistant. Answer factually and cite sources if possible. "
"If you don't know, say 'I don't know'."
"\nQuestion: What is the capital of France?"
)
response = client.chat.completions.create(
model="gpt-4o",
messages=[{"role": "user", "content": prompt}]
)
answer = response.choices[0].message.content
print("AI answer:", answer)
# Simple verification example
if "Paris" in answer:
print("Verified: Correct capital detected.")
else:
print("Warning: Possible hallucination detected.") output
AI answer: The capital of France is Paris. Verified: Correct capital detected.
Common variations
You can reduce hallucinations further by:
- Using retrieval-augmented generation (RAG) to provide up-to-date documents as context.
- Choosing models with stronger factual grounding like
claude-3-5-sonnet-20241022. - Applying temperature=0 or low values to reduce randomness.
- Implementing multi-step prompting to verify or cross-check answers.
import os
import anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
system_prompt = "You are a helpful assistant that only answers with verified facts."
user_prompt = "What is the capital of France?"
message = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=100,
system=system_prompt,
messages=[{"role": "user", "content": user_prompt}]
)
print("Claude answer:", message.content[0].text) output
Claude answer: The capital of France is Paris.
Troubleshooting
If you see inconsistent or fabricated answers, try:
- Lowering the temperature parameter to 0 or 0.2.
- Adding explicit instructions to refuse to answer if unsure.
- Using external knowledge bases or APIs to fact-check outputs.
- Splitting complex queries into smaller, verifiable steps.
Key Takeaways
- Use clear, explicit prompts instructing the model to avoid guessing and cite sources.
- Select models with strong factual accuracy and tune parameters like temperature to reduce randomness.
- Incorporate external verification or retrieval-augmented generation to ground answers in real data.