A secure middleware that sanitizes OpenAI API traffic by:
- Automatically detecting and removing Personal Identifiable Information (PII)
- Preserving message context and meaning
- Operating as a drop-in proxy for existing OpenAI integrations
What your user sends:
Hello, my card number is 4111-1111-1111-1111. Call me at (123) 456-7890
What the OpenAI API sees:
Hello, my card number is
<VISA-CARD>
. Call me at<US-NUMBER>
- Introduction + adding a new rule
- Improving a rule with the AI agent
- Using SanitAI with the OpenAI Python package
# get the code
git clone https://github.com/edublancas/sanitAI
cd sanitAI/
# set your OpenAI key (used for the agent that helps you define the PII rules)
export OPENAI_API_KEY=SOME-OPENAI-KEY
# build the docker image
# NOTE: if building using ARM (Apple silicon, read the section at the end)
docker build -t sanitai .
# run
docker run -p 5001:80 --rm --name sanitai \
-e OPENAI_API_KEY=$OPENAI_API_KEY \
sanitai
Then, open: http://localhost:5001/admin/
Login:
- Email: [email protected]
- Password: admin123
You can test it in the Playground
tab. Alternatively, you can run the sample.py
script:
export OPENAI_API_KEY=SOME-OPENAI-KEY
pip install openai
python example.py
Then, look at the logs from the server and you'll see that the user sent:
Hello, my card number is 4111-1111-1111-1111. Call me at (123) 456-7890
But the proxy intercepts the request and sends this to OpenAI:
Hello, my card number is
<VISA-CARD>
. Call me at<US-NUMBER>
This is the source code for example.py
:
from openai import OpenAI
# point to the proxy
client = OpenAI(base_url="http://localhost:5001/proxy/v1")
response = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{
"role": "user",
"content": "Hello, my card number is 4111 1111 1111 1111. Call me at (123) 456-7890",
},
],
)
print(response.choices[0].message.content)
You can deploy this to any platform that supports containerized applications (e.g.
Ploomber Cloud). The app runs on a single container (see the Dockerfile
), and spawns the proxy and the UI via supervisord and NGINX.
If you're looking for enterprise support, contact me.
- Drop-in proxy for OpenAI (no need to change your code)
- UI to admin and test rules
- AI agent to add new rules
- AI agent to fix rules
If you're running on ARM (Apple silicon), uncommend uncomment the line that installs
spaCy in
the Dockerfile
.