1. Home
  2. Blog
  3. Hide private data before a call to a model, and put it back in the answer

How-to

Hide private data before a call to a model, and put it back in the answer

Greg StoosFounder of Opami
  • 4 min read
A developer compares two terminal windows on a monitor, with a notebook diagram beside the keyboard.
In this article
  1. The four steps
  2. Set up
  3. Send and hide
  4. Call the model on the badges
  5. Put the real details back
  6. Run it
  7. What is kept, and where
  8. Limits
  9. Try it on your own prompts

When your code sends a customer's ticket to a model, the provider receives the whole ticket. The model needs the problem. It does not need the name, the email address or the account number. Below, Opami Protect finds those details and two short Python functions do the rest: one hides them before the call, one puts them back in the answer. Where Opami Protect finds a detail, the model reads a badge such as [PERSON_A] in its place; a detail it misses reaches the model as written, which is why the Limits section asks you to check a sample.

The four steps

  • Send. Your code sends the text to Opami Protect. It returns each private or sensitive piece it found, with a type and a position.
  • Hide. Your code swaps each piece for a badge and keeps a table from badge to real value.
  • Call. The model works on the text with badges in it.
  • Restore. Your code puts the real details back in the answer.

Opami Protect does the finding. The swap, the call and the restore are yours, so you decide what the model sees and what your user sees.

Set up

Register at console.opami.ai and create a key. There is no sales call. Opami Protect costs €2.00 per 1 million characters, up to 1 billion characters a month, and less above that.

bash
pip install requests openai
export OPAMI_PROTECT_KEY="opk_..."
export OPENAI_API_KEY="sk-..."
export OPENAI_MODEL="..."   # the model you use today

We wrote the example against an OpenAI model and have not run it against other providers; with another provider, only ask_model() changes.

Send and hide

python
import os
from string import ascii_uppercase

import requests
from openai import OpenAI

URL = "https://api-protect.opami.ai/v1/detect"
HEADERS = {"Authorization": f"Bearer {os.environ['OPAMI_PROTECT_KEY']}"}


def hide(text):
    resp = requests.post(URL, headers=HEADERS, json={"model": "protect-v1", "text": text})
    resp.raise_for_status()
    found = sorted(resp.json()["detections"], key=lambda d: d["start"])

    badge_for = {}
    for d in found:
        if d["text"] not in badge_for:
            kind, _, _ = d["type"].partition(".")
            kind = kind.upper()
            n = sum(b.startswith(f"[{kind}_") for b in badge_for.values())
            badge_for[d["text"]] = f"[{kind}_{ascii_uppercase[n]}]"

    hidden = text
    for d in reversed(found):
        hidden = hidden[: d["start"]] + badge_for[d["text"]] + hidden[d["end"]:]

    return hidden, {badge: real for real, badge in badge_for.items()}

Each detection carries the text found, a type such as person.name.full, and its start and end in your text. Print resp.json() once on your first call and check those names before you build on them. The badge takes the first part of the type: a name becomes [PERSON_A].

Why go backwards? Every swap changes the length of the text, so a forward pass would leave each later position pointing at the wrong characters after the first replacement. Start from the last piece and the earlier positions never move.

This sketch matches on exact text. "Ana Silva" twice is [PERSON_A] twice, while a shorter "Ana" gets a badge of its own, and you can merge the two in your table if your prompts need it. Letters stop at Z for one kind of data; if a text can hold more, switch to numbers and restore the longest badges first, or a short badge will eat the start of a longer one.

Call the model on the badges

python
client = OpenAI()


def ask_model(hidden):
    resp = client.responses.create(
        model=os.environ["OPENAI_MODEL"],
        instructions="You draft short, polite support replies. "
        "Copy placeholders such as [PERSON_A] exactly as written.",
        input=hidden,
    )
    return resp.output_text

Keep the instruction to copy badges. A model that writes "Person A" instead breaks the restore, because nothing in the table matches it.

Put the real details back

python
def restore(answer, real_for):
    for badge, real in real_for.items():
        answer = answer.replace(badge, real)
    return answer

Run it

python
ticket = (
    "Hi, this is Ana Silva from Lusomar. My invoice went to the wrong address. "
    "Please write back to ana.silva@lusomar.pt. Thanks, Ana Silva"
)

hidden, real_for = hide(ticket)
print(hidden)
print(restore(ask_model(hidden), real_for))

The first print is what your provider receives: the ticket with the details Opami Protect found swapped for badges. We show no sample output here. The badges you get follow the types Opami Protect returns for your text, so the output of your own first run is the one to read. The second print is the reply your user sees, with the real name and address back in place.

What is kept, and where

The table real_for lives in your process for one request. Opami Protect keeps no text and no log of requests. If you need a record of what was hidden, keep it on your side.

Limits

Opami Protect does not catch everything, so read the hidden text for a sample of your own requests before you ship.

It hides who and what is private, not the facts around it. A wrong address on an invoice is still a wrong address when the model reads it.

The restore works on a complete answer. We have nothing to state yet about streamed answers, where a badge can arrive split across chunks.

Try it on your own prompts

With a key from console.opami.ai, run hide() on a few of your real prompts and print what your model would receive, before you touch production code. If a name gets through, tell us: a miss on a real prompt is the bug report we want.

Try it on your own data

Opami Protect is open to anyone. Register at the console, run a sample of your own text, and see what it hides. No sales call.

Keep reading

All articles →

Get the answers in your inbox

Each week, the three articles, one thing we learned and one number, in one email.

What do you want to hear? Tick any

One vision, four products

Hides the private and sensitive data in a text before the AI reads it. For the people who build with AI.

Opami Protect

Work with the best AI models, with nothing private reaching them.

Opami Chat

The confidential and proactive AI assistant. It connects to your apps and inboxes, knows you and how you work, reminds you and prepares the work for your approval.

Opami Bot

Your personal or company AI memory system without a single private or sensitive detail.

Opami Brain