Photo by Ian Dziuk on Unsplash

Why and how to reduce hallucinations in LLMs practically?

Introduction

With the emergence of generative AI, many people are using this tool to generate content like text, images, or codes. In the beginning, everything seems fabulous until you face a piece of false information. There is this typical issue when you are dealing with a Generative AI model, that is known as Hallucination, which means the generated output is not correct and the model behaves as if it is valid information. This is impacting the practical use of this technology especially in the use cases in which you need precise facts.
In this article, you will learn:

  • What is hallucination?
  • When does it matter to take care of hallucination?
  • Why does hallucination happen?
  • How practically reduce hallucination?

What is Hallucination?
Hallucination refers to a case when the model creates an output that is not accurate or misleading. This phenomenon makes it difficult to trust the model and limits its use cases. It is becoming an undesired behavior when you are relying on the generated output. Imagine in a healthcare use case, if the generated output is not trustable, and the doctors cannot validate its output, it is dangerous for the patients. Generating fake content can spread misinformation which leads to confusion or wrong decisions.

When does it matter to take care of hallucination?
It depends on the use cases, For example, in a use case where we need creativity and would like to write a screenplay based on new characters and a fantasy story, you don’t care if the output is not real and it is all imaginary.
Consider this use case: You are setting up a virtual assistant based on a Large language model (LLM) to chat with customers to suggest a product based on their needs; In this case, you want to be sure that the suggested products are real and the virtual assistant is not creating a fake product or url.

Why does hallucination happen?
Hallucination happens due to different reasons:

  • Nature of the model: The generative model objective is predicting the next best word, and for this, it can happen that the next best-predicted words are not conceptually correct. During the training phase, the LLM discovers the pattern in language but it doesn’t mean that they have a real understanding of the language as humans have.
  • Bias on training data: LLMs have been trained on a vast volume of data, such as web pages, articles, online forums, and others. As these data are generated by humans, it has bias and misinformation, and this will impact the quality of generated output by the LLM.
  • No understanding of context: The main characteristic of a generative model is to generate output, but it doesn’t have a real understanding of the context. So if you try to ask chatGPT to generate content about a city called “Xereriyy”, it will generate content for you, because by its nature it can not distinguish what is real and what is not real, and there is no ground truth for the model to validate its output.
  • There is no perfect model: Even in classical machine learning, there is always room for bias and errors. You cannot have a 100% accurate model and actually, if your model exactly mirrors the pattern on the training data, overfitting has happened, which means your mode has copied the rules on the training data and cannot predict the new patterns on the future data.

How practically reduce hallucination?
Mentioning the above points, then see what are the ways that we can reduce this phenomenon. I show you a couple of example prompts, and by default, I have used GPT-3.5-turbo as the model. The main task is to create a blog post about a traveling destination.

  • GPT-4 vs GPT-3.5: Based on the GPT-4 technical paper from OpenAI, GPT-4 has fewer hallucinations with respect to GPT-3.5.

I have tried the prompt both with GPT-3.5 and GPT-4. The example city that I have asked for content generation is “Mjsdubs”.

With GPT-3.5, the model is generating content, no matter if the destination name is fake:

Trying the same prompt with GPT-4, results in the message that the destination name doesn’t seem to be a valid country name, which validates other experiments with GPT-4 which is able to generate more accurate information.

- Indicate in the prompt to use reliable resources: One of the other tricks that can help is to indicate in the prompt that the model uses reliable resources. You can indicate a specific website as the reference. 

As you can see, the generated output indicates that the city is not recognized as a valid country, which is the expected answer.

- Temperature parameter: if you have never used the temperature parameter in the openAI API, it is a variable to trigger the model for more creativity, the higher the value (maximum is 1) the more diverse or creative output you will get from the model. It is experienced that the higher you set the value, there is higher the chance that the model hallucinates. Let’s try the previous prompt, running it with different values of temperature parameters. I have run it once with temperature 0 and once with temperature 1:

With the temperature setting as 0, the model is able to detect that the city’s name is fake while with the temperature as 1, the model generates the output.

- Give a precise instruction: Prompting is a way to give a precise command to the model for generating output, the more accurate you are, the better quality output you will receive.

Here in this example, I have specified in the prompt if the model doesn’t recognize the destination, and is not sure about it, just say the provided information is not correct. This way of prompting is working and the model is outputting that the provided information is not correct

As you can see, despite that I am using the GPT-3.5 model, it could detect that the city doesn’t exist and does not produce any output.

There are other ways to try help to reduce hallucinations:

  • Model tuning: By “fine-tuning” your models with customized data which leads to producing more accurate output, just ensure the quality of your training data to reduce the risks of hallucination.
  • Fact-checking mechanism: If it is possible, for your use case, include the fact-checking mechanism to validate the generated content with the subject matter experts, in this way, you can rely on the quality of the outputs
  • User feedback: If it is applicable to your use case, you can ask users to provide their feedback about the correctness and quality of the outputs by providing thumbs up & down buttons.

As you have seen there are many ways to control the hallucination and you have to try each method to see which one is working for your use case.

Summary

In this article, you have learned what hallucination is, when it needs to be mitigated, why it happens, and how to reduce it. Remember these methods are just some ways to control and reduce hallucination but they won’t eliminate it completely.

Author: Pari

Leave a Reply

Your email address will not be published. Required fields are marked *