Jump to contentJump to page navigation: previous page [access key p]/next page [access key n]
documentation.suse.com / Preventing AI hallucinations with prompt engineering

Preventing AI hallucinations with prompt engineering

Publication Date: 09 Oct 2024
WHAT?

A hallucination is an AI-generated content that contains incorrect data.

WHY?

Lear how to create good prompts that can help AI generate valid and accurate content.

EFFORT

Less than 15 minutes of reading.

1 Preventing AI hallucinations

The SUSE AI service is a locally hosted generative AI. It provides secure auditable AI capabilities and allows organizations to deploy AI models on their own infrastructure using SUSE Linux Micro. The service ensures complete control over data and compliance with regulatory requirements.

This topic describes how to create system prompts that can help AI generate valid and accurate content.

1.1 What is an AI hallucination?

A hallucination occurs when an LLM generates information that is not based on real-world facts or evidence. This can include fictional events, incorrect data or irrelevant outputs.

1.2 What causes AI hallucinations?

The most common causes of hallucinations are:

  • Ambiguous prompts. Vague queries can lead to random or inaccurate answers.

  • Lack of clear context. When the language model lacks context, it can fabricate answers.

  • Long generation length. The longer the generated response, the higher the chance that hallucinations can happen.

  • No retrieval-augmented process. LLMs without access to external sources—such as databases or search engines—can produce errors when they need to generate specific information.

1.3 How can I prevent AI from generating hallucinations?

You can help AI to generate more valid and accurate content by creating good prompts. This process is called prompt engineering. This section outlines several techniques to create a good prompt with real-life examples.

1.3.1 Set clear expectations

The clearer the prompt, the less the LLM relies on assumptions or creativity. A well-defined prompt guides the model toward specific information, reducing the likelihood of hallucinations.

Techniques:
  • Use specific language that guides the model.

  • Focus on known data sources or real events.

  • Ask for summaries or paraphrasing from established sources.

Example
  • Ambiguous prompt: Tell me about space.

  • Clearer prompt: Give me a summary of NASA's recent Mars missions, including factual details from their official reports.

Example
  • Ambiguous prompt: What is quantum computing?

  • Clearer prompt: Explain the basic principles of quantum computing, specifically how qubits work compared to classical bits.

1.3.2 Break down complex prompts

Break down complex or broad prompts into manageable pieces. This keeps the language model focused on a narrower scope and reduces the chance of hallucination.

Example
  • Complex query: Explain AI and how it can change the world.

  • Broken down prompt: What are the most recent advancements in AI? How are these advancements being applied in the healthcare industry?

1.3.3 Use retrieval-augmented generation (RAG)

When crafting prompts, encourage the model to retrieve relevant information instead of generating from scratch. Integrating a RAG system allows the LLM to query a specific database or resource.

Techniques
  • Include context cues, for example, Based on the following document or From the official website to point the model toward facts.

  • If using a tool like Milvus or ChromaDB, structure your prompt to refer to specific collections or documents. This reduces hallucination by grounding the LLM in real data.

  • Prompt without RAG: Tell me about the company's AI products.

  • Prompt with RAG: Based on the technical-info collection in Milvus, provide details about the company's AI product line.

1.3.4 Constrain the output

Limit the length or scope of the language model's response. Shorter, more direct answers reduce the chances of the model drifting off-topic or hallucinating extra details.

Technique
  • Use tokens or word limits where possible to enforce the output length.

Example
  • Unconstrained prompt: Give me a detailed report on quantum mechanics

  • Prompt with limited output: In 100 words or fewer, explain the main concept of quantum entanglement.

1.3.5 Prompt for verification

You can structure prompts to ask the LLM for clarification or to cite the source of its statements. This leads the model to produce more grounded and reliable responses.

Examples
  • Where did you find this information?

  • Verify this answer against known historical facts about the event.

1.3.6 Use Chain-of-Thought (CoT) Prompting

By guiding the model through logical steps, you can control the reasoning path and help the model arrive at accurate conclusions. This method.is especially helpful when asking the model to explain complex processes.

Example
  • Step-by-step prompt: Explain the following concepts step by step: 1. How do neural networks learn from data? 2. How is backpropagation used in this process?

1.3.7 Use templates for complex tasks

For complex tasks, for example, answering requests for proposals or technical questions, templates help provide a structure that minimizes hallucinations. This is achieved by making the desired format and content explicit.

Examples
  • Based on the document provided, summarize the key technical features of the product. Format the response as: 1. Feature, 2. Benefit, 3. Use Case. Use only factual information.

  • Assume your task is to create a customer-facing report that follows these guidelines:

    • Direction: Set the tone as professional and informative.

    • Format: Specify output as a formal report.

    • Examples: Provide a template or previous reports as examples.

    • Quality: Use a checklist to ensure accuracy and relevance.

    • Labor: Divide sections into executive summary, detailed findings and conclusions.

    The resulting prompt that corresponds to the above guidelines may look similar to the following example. You can copy and paste the whole text into an AI-driven chatbot prompt.

    Direction:
    
    Adopt a professional and informative tone throughout the report. The
    content should be clear, concise, and tailored to a business audience.
    
    Format:
    
    The output should be in the form of a formal report, structured with headings,
    subheadings, and bullet points where necessary. Use professional language
    and adhere to business writing standards.
    
    Examples:
    
    Here is a template of the report structure to follow:
    
    Executive Summary:
    - Overview of key findings and recommendations.
    Detailed Findings:
    - Section 1: [Topic]
    - Section 2: [Topic]
    - Section 3: [Topic]
    Conclusions:
    - Summary of findings and next steps.
    
    Please refer to previous reports such as [Report A] and [Report B] for style
    and formatting guidelines.
    
    Quality:
    
    Use the following checklist to ensure the report's accuracy and relevance:
      1. Verify all data and statistics are correct and up-to-date.
      2. Ensure the report is free of grammatical and typographical errors.
      3. Cross-check that all sections are consistent and logically structured.
      4. Validate that the report meets the customer’s objectives and addresses
         their concerns.
    
    Labor:
    
    Divide the report into the following sections:
      1. Executive Summary: Summarize the main findings and key recommendations in a 
         concise manner.
      2. Detailed Findings: Provide in-depth analysis and data for each key area of
         focus, divided into logical subsections.
      3. Conclusions: Offer a summary of the findings and suggest actionable next
         steps for the customer.