Rilevamento e mitigazione delle allucinazioni
Le allucinazioni sono output degli LLM falsi ma espressi con sicurezza. Impari perché si verificano, come rilevarle e quali tecniche concrete usare per ridurle in produzione.
Rilevamento e mitigazione delle allucinazioni è una lezione Prompt Engineering & LLM Optimization for Developers gratuita su CoddyKit. Questa è la lezione 4 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Prompt Engineering & LLM Optimization for Developers, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Prompt Engineering & LLM Optimization for Developers include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
What is a Hallucination?
A hallucination is when an LLM produces text that is fluent and confident but factually wrong or unsupported by any source.
Because the output looks authoritative, hallucinations are dangerous in user-facing apps.
Why They Happen
LLMs predict the most likely next token, not the truth. With no grounding, they fill gaps with plausible-sounding inventions.
- Missing knowledge in training data
- Ambiguous or leading prompts
- Pressure to always answer
Types of Hallucination
Two broad categories:
- Factual: wrong dates, fake citations, invented APIs
- Faithfulness: answer contradicts the provided context (common in RAG)
Grounding with Context
The strongest defense is grounding: give the model trusted source text and instruct it to answer only from that text.
Answer ONLY using the context below.
If the answer is not in the context, say "I do not know".
Context:
{retrieved_docs}Forcing Citations
Ask the model to cite which passage supports each claim. Unsupported sentences become easy to spot and verify.
For each sentence, add a [doc_id] citation.
Do not make claims you cannot cite.Lowering Temperature
Higher temperature increases creativity — and invention. For factual tasks, set a low temperature so the model stays close to high-probability, well-grounded tokens.
const res = await client.chat.completions.create({
model: "gpt-4o-mini",
temperature: 0,
messages
});Self-Consistency Checks
Generate the answer several times. If the model gives different facts each run, the claim is likely a hallucination. Agreement is a weak but useful signal of reliability.
LLM-as-a-Judge Verification
Use a second model call to check whether the answer is supported by the context. The judge returns a faithfulness verdict you can act on.
Does the ANSWER follow only from the CONTEXT?
Reply: SUPPORTED, PARTIAL, or UNSUPPORTED.Programmatic Validation
When outputs are structured, validate them. A cited URL should resolve, a quoted number should match the source, a JSON field should match a schema.
function validateCitation(cit, docs) {
return docs.some(d => d.id === cit.doc_id);
}Letting the Model Abstain
Give the model permission to say I do not know. Removing the pressure to always answer measurably reduces fabricated content.
Human Review for High Stakes
For medical, legal, or financial outputs, route low-confidence or uncited answers to a human before showing them to users. Automation plus oversight beats either alone.
Quick Check
Test your understanding.
Recap
You learned to fight hallucinations: ground answers in trusted context, force citations, lower temperature, use self-consistency and LLM-as-a-judge checks, validate outputs programmatically, allow abstention, and add human review for high-stakes cases.
Impara Prompt Engineering & LLM Optimization for Developers con un tutor IA — gratis
Scrivi ed esegui vero codice nel tuo browser, ricevi aiuto istantaneo da un tutor IA disponibile 24/7, e riprendi da dove hai lasciato sul web o nell'app.
- Corsi
- 12
- Lezioni
- 48
Domande Frequenti
La lezione «Rilevamento e mitigazione delle allucinazioni» è gratuita?
Sì — il testo completo di «Rilevamento e mitigazione delle allucinazioni» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Prompt Engineering & LLM Optimization for Developers, passa a CoddyKit PRO. Il corso Prompt Engineering & LLM Optimization for Developers include 4 lezioni in totale.
Cosa imparerò in «Rilevamento e mitigazione delle allucinazioni»?
Le allucinazioni sono output degli LLM falsi ma espressi con sicurezza. Impari perché si verificano, come rilevarle e quali tecniche concrete usare per ridurle in produzione. Eserciti Prompt Engineering & LLM Optimization for Developers con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Prompt Engineering & LLM Optimization for Developers?
Non è richiesta alcuna esperienza precedente. Prompt Engineering & LLM Optimization for Developers su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 4 di 4.
Quanto tempo richiede la lezione «Rilevamento e mitigazione delle allucinazioni»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Prompt Engineering & LLM Optimization for Developers?
Sì. Ogni lezione Prompt Engineering & LLM Optimization for Developers include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Metriche e benchmark per la valutazione degli LLM
- Sistemi di feedback human-in-the-loop
- Prompt injection e best practice di sicurezza
- Rilevamento e mitigazione delle allucinazioni