{"slug": "rag-hallucination-detection", "title": "RAG - Hallucination Detection", "summary": "A developer explains how to detect hallucinations in retrieval-augmented generation (RAG) systems, where an LLM generates responses not supported by the retrieved context. Techniques include comparing embeddings, using the BERT-based LettuceDetect model, leveraging evaluation frameworks like RAGAS and DeepEval, and integrating these checks into CI/CD pipelines.", "body_md": "Hallucination means making an assumption or making up something when the LLM does not know the answer.\n\nExample:\n\nSuppose we have a PDF file that contains information about Python, but it does not have any details about **Decorators**.\n\nHere, the document is split into chunks, and the chunks are stored in the vector database.\n\nSuppose we ask the query:\n\n\"What is a decorator?\"\n\nThe LLM should not give any response because the information about decorators is not available in the database.\n\nBut the LLM may give a response from its own knowledge, which may be correct, but it is **not from the database**.\n\nThis is called **hallucination** in RAG.\n\nThe response given by the LLM should be supported by the information given in the context.\n\nIf the response is not supported by the context, it is considered hallucinated.\n\nCompare the embedding of the context with the embedding of the LLM response to check how closely they are related.\n\nLettuceDetect is a BERT-based model used to find where hallucinations could occur at the character level.\n\nIt checks the response word by word to identify possible hallucinations.\n\nGive the response to another LLM and ask it to detect hallucinations.\n\nThe drawback here is that the LLM may hallucinate again, and it can also be expensive unless we use a local model.\n\nRAGAS is also called an **evaluation framework**. It provides different metrics that can be used to evaluate RAG systems and identify potential hallucinations.\n\nSome of the important metrics are:\n\n**Faithfulness** – Checks whether the results are backed by the provided context. This can be used to check hallucination.\n\n**Answer Relevancy** – Suppose the context and answer are relevant. This metric checks how relevant the answer is to the query.\n\n**Context Precision** – Checks how many of the retrieved documents are useful and relevant.\n\n**Context Recall** – Checks whether the necessary documents have been retrieved for the query.\n\n**Answer Correctness** – Checks the response against the ground truth.\n\n**Answer Similarity** – Checks the semantic similarity between the expected answer and the generated answer.\n\nRAGAS can be used in a **CI/CD pipeline**.\n\nWe can write unit test cases and compare the actual answer with the expected answer using the above metrics.\n\n**DeepEval** can also be used to evaluate RAG systems and detect hallucinations.", "url": "https://wpnews.pro/news/rag-hallucination-detection", "canonical_source": "https://dev.to/ramya_perumal/rag-hallucination-detection-2k60", "published_at": "2026-08-21 00:54:08+00:00", "updated_at": "2026-08-21 01:13:48.198720+00:00", "lang": "en", "topics": ["large-language-models", "artificial-intelligence", "ai-safety", "mlops", "developer-tools"], "entities": ["LettuceDetect", "RAGAS", "DeepEval", "BERT"], "alternates": {"html": "https://wpnews.pro/news/rag-hallucination-detection", "markdown": "https://wpnews.pro/news/rag-hallucination-detection.md", "text": "https://wpnews.pro/news/rag-hallucination-detection.txt", "jsonld": "https://wpnews.pro/news/rag-hallucination-detection.jsonld"}}