Understanding the challenge
The root of this problem lies in how modern language models work. These models are trained on vast amounts of data and retain this knowledge. When asked a question, they naturally try to be helpful by combining:- Information from the provided context
- Their pre-existing knowledge
- Patterns they’ve learned from similar questions
- Used the context correctly (location and designer)
- Added the completion date (1889) from its training data
- Included visitor statistics it “knew” from pre-training
- Users can’t verify the source of this information
- The response mixes verified knowledge base facts with external information
- There’s no distinction between what came from our documents and what didn’t
The path to a solution
The key to solving this problem lies in understanding that language models will naturally try to be helpful by providing complete answers. They need explicit constraints and clear instructions to override this behavior. Let’s look at how we can transform our example to prevent out-of-context information. First, here’s how we typically implement a RAG system with minimal constraints:- The system message is too general
- The prompt doesn’t explicitly restrict external knowledge
- There’s no guidance on handling missing information
- It’s honest about what information is available
- It clearly indicates what the source documents tell us
- It maintains the integrity of our knowledge base
- A stronger system message that explicitly defines the model’s role and limitations
- A structured prompt that:
- Uses clear, direct language (“STRICTLY”, “DO NOT”)
- Provides explicit instructions for handling missing information
- Reinforces the importance of staying within the provided context
Building a complete solution
To implement this approach in your own RAG system, you’ll need several components working together. Let’s walk through a complete implementation that demonstrates both the problem and its solution.1
Setting Up the Environment
First, let’s set up our environment with the necessary imports and configurations:This setup includes:
- Loading environment variables for API keys
- Setting up Galileo logging for tracking operations
- Creating an OpenAI client for model interactions
2
Understanding the Document Retriever
The document retriever is designed to demonstrate how incomplete context can lead to out-of-context information:Key points about the retriever:
- It simulates real-world document retrieval with intentionally incomplete information
- Uses predefined contexts to demonstrate the out-of-context problem
- Includes metadata for tracking document sources and relevance
3
Demonstrating the Problem
Let’s look at how a weak prompt can lead to out-of-context information:Problems with this approach:
- The weak prompt doesn’t explicitly constrain the model
- The system message is too generic
- No explicit instruction to avoid using external knowledge
4
Implementing the Solution
Now, let’s see how to prevent out-of-context information with a stronger prompt:Key improvements:
- Explicit instruction to use only provided context
- Clear directive to acknowledge when information is missing
- Stronger system message that reinforces context adherence
5
Running the Interactive Demo
The main function provides an interactive way to test and compare both approaches:Using this code, you can:
- Test predefined queries that highlight the problem
- Compare responses from both approaches
- See the effectiveness of context constraints
6
Analyzing the Results
When running the demo, you’ll notice:Constrained Response:The constrained response demonstrates better adherence to the available context, even though it provides less information.
- Unconstrained Responses: May include information not present in the context
- Constrained Responses: Strictly adhere to provided information
- Completeness vs. Accuracy: Trade-off between complete answers and factual accuracy
7
Best Practices and Recommendations
To prevent out-of-context information in your RAG system:
-
Strong Prompting:
- Be explicit about using only provided context
- Include clear instructions for handling missing information
- Use system messages that reinforce context adherence
-
Context Management:
- Ensure retrieved documents are relevant and complete
- Include metadata for tracking document sources
- Monitor and log context utilization
-
Response Validation:
- Compare responses against provided context
- Track and measure context adherence
- Use Galileo metrics to monitor performance
-
User Experience:
- Clearly communicate when information is limited
- Provide transparent source attribution
- Balance completeness with accuracy