Marcio Cunha

How to reduce LLM hallucinations using external knowledge

Learn how to connect language models to external knowledge bases using RAG to ensure accurate answers and eliminate invented data.

Marcio Cunha•4 min
Also available in:PortuguêsEspañol
Summary
  • Connecting artificial intelligence models to external databases restricts answers to verifiable facts.
  • The semantic search process retrieves relevant document excerpts before formulating the final response.
  • Dividing long texts into smaller chunks improves the accuracy of information retrieval within the base.
  • Using re-ranking algorithms ensures the most important data stays at the top of the prompt.
  • Continuous validation and monitoring prevent the model from ignoring supplied context and inventing data.

The challenge of invented answers in artificial intelligence

When chatting with an advanced language model, it is common to notice that it sometimes invents facts with absolute conviction. This phenomenon happens because the artificial intelligence was trained to predict the most likely next word, rather than consult an absolute truth. In practice, this means that if the model does not know the exact answer to your question, it will create text that sounds correct but hides entirely false information. To solve this problem in corporate environments, modern engineering uses a technique called RAG, which serves to feed the intelligence with real documents before it decides what to answer.

The acronym RAG stands for Retrieval-Augmented Generation. In simple terms, this approach works like giving an open recipe book to a cook before asking for a specific dish, instead of demanding they invent everything from scratch. Just as we detail in other technical diagnostic guides, accuracy depends on eliminating assumptions and focusing on concrete data. Rather than relying solely on the model's internal memory, the system first searches a reliable database for the exact excerpts containing the answer and only then asks the intelligence to write the final text based on that data.

How the document search architecture works

The heart of any system based on this technique is how data is stored and searched. First, all company manuals, contracts, or reports are cut into small pieces called chunks. These pieces go through a mathematical process that transforms them into numerical vectors, allowing computers to understand the semantic meaning of words rather than just the exact spelling. When a user asks a question, the system converts that query into a vector and quickly scans the database to find the text fragments that have the closest meaning to what was asked.

To ensure the search works well, you must choose appropriate storage and processing tools. We can use dedicated vector databases or specific extensions in traditional databases to store these mathematical representations. In networks and corporate environments where stability is crucial, much like when resolving network issues by flushing the DNS cache, pre-organizing components prevents bottlenecks and operational errors during service hours. If the system searches documents slowly or inaccurately, the artificial intelligence will receive poor material and continue delivering confusing answers.

Implementing search and prompt injection

After retrieving the most relevant text fragments from the knowledge base, the next step consists of assembling the final command that will be sent to the language model. This command is structured to contain strict behavior instructions, known as a system prompt, followed by the retrieved context and the user's original question. In practice, we explicitly tell the model to use only the information contained in the provided documents and to admit if the answer is not present there, cutting off the tool's urge to invent details.

import openai

def generate_safe_response(user_query, retrieved_documents):
    context = "
".join(retrieved_documents)
    prompt = f"Based strictly on the context below, answer the question. If the answer is not in the text, say you do not know.

Context:
{context}

Question: {user_query}"
    
    response = openai.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.0
    )
    return response.choices[0].message.content

The code above demonstrates a basic integration structure where the model's temperature is set to zero, minimizing unnecessary creativity and forcing strict adherence to the supporting text. Furthermore, much like a bench technician evaluates rigorous physical parameters before validating equipment, as discussed when testing an NTC thermistor with a multimeter, prompt engineering requires constant testing with trap questions to verify if the intelligence tries to bypass imposed rules.

Operational challenges and refinement strategies

Despite being extremely powerful, this approach is not a silver bullet and presents complex engineering challenges. One of the most common problems occurs when the document base is very large and the system ends up retrieving irrelevant excerpts that confuse the language model. To mitigate this effect, engineers use additional re-ranking steps, placing the most promising fragments exactly at the beginning or end of the supporting text, making better use of the artificial intelligence's attention window.

Another critical point involves the constant updating of external data. If a company document changes, the vector system must be updated almost in real time to prevent the model from responding based on old rules. Keeping this synchronization cycle running without performance drops requires continuous infrastructure monitoring. At the end of the day, uniting language models with external bases transforms a technology prone to creative inventions into a reliable, auditable, and highly productive corporate assistant for daily work.