Using LLMs as Fact-Checkers for Hallucination Detection
Introduction: Why Fact-Checking LLMs Matters
Welcome back! In the last lesson, you learned how to measure the consistency of large language models (LLMs) by checking if they give the same answer to the same prompt every time. This is a key part of benchmarking, but there is another important aspect to consider: factual accuracy. Sometimes, LLMs generate answers that sound convincing but are actually incorrect or even completely made up. These are called hallucinations. Detecting hallucinations is crucial, especially if you want to use LLMs in settings where accuracy matters, such as education, research, or customer support.
In this lesson, you will learn how to use one LLM to check the factual accuracy of another LLM’s answers. This approach is becoming more common because it allows you to automate the process of fact-checking at scale. By the end of this lesson, you will know how to set up a simple pipeline in which one model generates answers and another model acts as a judge, telling you if those answers are correct or hallucinated.
Example: Generating Answers To Fact-Check
A note on models and providers: the models used in this example are set up for you on CodeSignal. If you run similar code on your own machine, check your provider's documentation first: model names, API keys and supported parameters (such as
temperature) differ between providers and models.
Let’s start by creating a list of prompts that we want to fact-check. For this example, we will use two prompts: one that is straightforward and factual, and another that is intentionally impossible. Here is the code to generate answers using deepseek-ai/DeepSeek-V3:
In this code, you first define a list of prompts. The first prompt asks a factual question, while the second prompt is a trick question about Atlantis, which is a mythical place and not a real UN member. You then loop through each prompt, send it to DeepSeek V3, and collect the answers. The temperature=0 setting ensures that the model gives the most likely answer, which helps with consistency.
After running this code, you might get output like the following:
As you can see, the model gives a factual answer to the first prompt and correctly identifies the second as a myth. However, sometimes models can hallucinate and provide made-up details, especially for less obvious questions.
