LangChain Prompt Sanitization Tutorial
LangChain Tutorial: Integrating Nightfall for Secure Prompt Sanitization
Generative AI systems like OpenAI's ChatGPT have revolutionized how we interact with technology, but they come with a significant risk: the inadvertent exposure of sensitive information (OWASP LLM06). Without proper safeguards, these AI platforms may receive, process, and potentially retain confidential data, including:
Personally Identifiable Information (PII)
Protected Health Information (PHI)
Financial details (e.g., credit card numbers, bank account information)
Intellectual property
Real-world scenarios highlight the urgency of this issue:
Support Chatbots: Imagine a customer service AI powered by OpenAI. Users, in their quest for help, might unknowingly share credit card numbers or Social Security information. Without content filtering, this sensitive data could be transmitted to OpenAI and logged in your support system.
Healthcare Applications: Consider an AI-moderated health app that processes patient and doctor communications. These exchanges may contain protected health information (PHI), which, if not filtered, could be unnecessarily exposed to the AI system.
Content filtering is a crucial safeguard, removing sensitive data before it reaches the AI system. This ensures that only necessary, non-sensitive information is used for content generation, effectively preventing the spread of confidential data to AI platforms.
Python Example
Let's examine this in a Python example using the LangChain, Anthropic, and Nightfall Python SDKs. You can download this sample code here.
Step 1: Setup Nightfall
If you don't yet have a Nightfall account, sign up here.
Create a Nightfall key. Here are the instructions.
Install the necessary packages using the command line:
Set up environment variables. Create a .env
file in your project directory:
Step 2: Configure Detection
Create an inline detection rule with the Nightfall API or SDK client, or use a pre-configured detection rule in the Nightfall account. In this example, we will do the former.
If you specify a redaction config, you can automatically get de-identified data back, including a reconstructed, redacted copy of your original payload. Learn more about redaction here.
Step 3: Classify, Redact, Filter Your User Input
to integrate content filtering into our LangChain pipeline seamlessly. We'll create a custom LangChain component for Nightfall sanitization. This allows us to seamlessly integrate content filtering into our LangChain pipeline.
Explanation
We start by importing necessary modules and loading environment variables.
We initialize the Nightfall client and define detection rules for credit card numbers.
The
NightfallSanitizationChain
class is a custom LangChain component that handles content sanitization using Nightfall.We set up the Anthropic LLM and create a prompt template for customer service responses.
We create separate chains for sanitization and response generation, then combine them using
SimpleSequentialChain
.The
process_customer_input
function provides an easy-to-use interface for our chain.
Error Handling and Logging
In a production environment, you might want to add more robust error handling and logging. For example:
Usage
To use this script, you can either run it directly or import the process_customer_input
function in another script.
Running the Script Directly
Simply run the script:
This will process the example customer input and print the sanitized input and final response.
Using in Another Script
You can import the process_customer_input
function in another script:
Expected Output
What does success look like?
If the example runs properly, you should expect to see an output demonstrating the sanitization process and the final response from Claude. Here's what the output might look like:
Last updated