Skip to main content

Build a RAG for Sensitive Data

Use this guide to build an assistant that answers from your files while reducing sensitive data exposure.

This guide focuses on the application pattern. For the SDK reference flow, see Data Sources and RAG.

For sensitive files, use this sequence:

  1. Review the file content.
  2. De-identify the file if needed.
  3. Add the safer file as an assistant data source.
  4. Deploy the assistant.
  5. Test the assistant with safe, fictional prompts first.
Sensitive source file
-> Guardian Layer
-> De-identified file
-> Assistant data source
-> RAG-backed chat

Step 1: De-identify the Source File

For CSV:

curl -X POST "$CUSTODIAN_API_BASE_URL/api/v1/deidentify/text/proprietary/outputs/csv" \
-H "X-API-Key: $CUSTODIAN_API_KEY" \
-F "file=@customers.csv" \
-F "masking_type=transform" \
--output deid_customers.csv

For PDF:

curl -X POST "$CUSTODIAN_API_BASE_URL/api/v1/deidentify/text/proprietary/outputs/pdf" \
-H "X-API-Key: $CUSTODIAN_API_KEY" \
-F "file=@policy.pdf" \
-F "masking_type=transform" \
--output deid_policy.pdf

Step 2: Add the File to an Assistant

from custodian_labs import Custodian

custodian = Custodian(
model="gpt-4o",
system_prompt="Answer using the uploaded knowledge file. If the answer is not in the file, say so.",
)

custodian.add_data_source_file("./deid_policy.pdf")

app = custodian.deploy()

Step 3: Chat with Retrieved Context

response = app.chat("What does the policy say about account access?")

print(response.response)
print(response.retrieved_contexts)

Use retrieved_contexts to inspect whether the assistant is using the expected source material.

Practical Tips

  • Prefer de-identified files for RAG when the original file contains sensitive data.
  • Keep original files in a secure system, not in the public application path.
  • Use clear headings in source documents to improve retrieval quality.
  • Test with representative but fictional questions before using real workflows.
  • Review whether transform or redact is more appropriate for the source material.

Next Steps