Wykrywanie i pomiar halucynacji
Poznaj praktyczne techniki wykrywania, kiedy system RAG tworzy fakty niepoparte pobranym kontekstem, oraz sposoby ilościowego mierzenia współczynnika halucynacji w ramach ewaluacji.
Wykrywanie i pomiar halucynacji to bezpłatna lekcja LLM Apps in Production (RAG + Vector DB + Caching) na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej LLM Apps in Production (RAG + Vector DB + Caching), a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs LLM Apps in Production (RAG + Vector DB + Caching) zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
What Is a RAG Hallucination?
A hallucination is an answer that is fluent and confident but not supported by the retrieved context. In RAG, the cure is grounding: every claim should trace back to a source.
Measuring hallucination rate is essential to trust your system.
Faithfulness vs Correctness
Two different things to measure:
- Faithfulness — is the answer supported by the provided context?
- Correctness — is the answer factually true in the real world?
A RAG answer can be correct but unfaithful (right by luck) or faithful but wrong (the source was wrong).
Claim Decomposition
To check faithfulness, break the answer into atomic claims, then verify each against the context.
answer = 'Paris is the capital of France and has 5 million people.'
claims = [c.strip() for c in answer.replace(' and ', '. ').split('.') if c.strip()]
for c in claims:
print('CLAIM:', c)Context Entailment
For each claim, ask: does the retrieved context entail it? An NLI model or an LLM judge labels each claim as supported, contradicted, or not-mentioned.
- Supported = grounded
- Not-mentioned = potential hallucination
- Contradicted = definite error
LLM-as-Judge for Faithfulness
A common pattern: prompt a strong model with the context, the answer, and ask it to score whether the answer is fully supported. Calibrate the judge against human labels.
Computing Hallucination Rate
Hallucination rate = fraction of claims (or answers) that are unsupported.
labels = ['supported', 'supported', 'not_mentioned', 'contradicted']
bad = sum(1 for x in labels if x != 'supported')
rate = bad / len(labels)
print('Hallucination rate:', round(rate, 2))Citation Coverage
If your system outputs citations, you can measure citation coverage: the share of sentences that point to a retrieved chunk that actually supports them. Low coverage signals hallucination risk.
Detecting Missing Context
Many hallucinations happen because retrieval failed and the model filled the gap. Track cases where the context lacks the answer but the model still answered confidently instead of saying 'I do not know'.
A Simple Faithfulness Score
Aggregate per-claim labels into a single score per answer.
def faithfulness(labels):
return sum(1 for x in labels if x == 'supported') / len(labels)
print(faithfulness(['supported', 'supported', 'not_mentioned']))Reducing Hallucinations
Once measured, reduce hallucinations by:
- Improving retrieval recall
- Instructing the model to abstain when unsupported
- Requiring inline citations
- Post-hoc filtering of unsupported claims
Tracking Over Time
Add hallucination rate to your regular eval runs. Watch it on every prompt or model change so a regression is caught before it reaches users.
Quick Check
Test your understanding of faithfulness.
Recap
You learned to detect hallucinations by separating faithfulness from correctness, decomposing answers into claims, checking entailment against context with an LLM judge, and computing a hallucination rate. Track it over time and reduce it with better retrieval, abstention, and citations.
Często zadawane pytania
Czy lekcja „Wykrywanie i pomiar halucynacji” jest bezpłatna?
Tak — pełny tekst „Wykrywanie i pomiar halucynacji” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu LLM Apps in Production (RAG + Vector DB + Caching), przejdź na CoddyKit PRO. Kurs LLM Apps in Production (RAG + Vector DB + Caching) zawiera 4 lekcji w sumie.
Co nauczysz się w „Wykrywanie i pomiar halucynacji”?
Poznaj praktyczne techniki wykrywania, kiedy system RAG tworzy fakty niepoparte pobranym kontekstem, oraz sposoby ilościowego mierzenia współczynnika halucynacji w ramach ewaluacji. Ćwiczysz LLM Apps in Production (RAG + Vector DB + Caching) z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć LLM Apps in Production (RAG + Vector DB + Caching)?
Nie wymagamy żadnego doświadczenia. LLM Apps in Production (RAG + Vector DB + Caching) w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Wykrywanie i pomiar halucynacji”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji LLM Apps in Production (RAG + Vector DB + Caching)?
Tak. Każda lekcja LLM Apps in Production (RAG + Vector DB + Caching) zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Najważniejsze miary wydajności RAG
- Tworzenie benchmarków ewaluacyjnych
- Testy A/B i pętle informacji zwrotnej od użytkowników
- Wykrywanie i pomiar halucynacji