跳到正文
DEV Community· Cristian Gîrlea·· 2 天前AI 評分63

php-python-ai-bridge 示範 PHP 應用如何在資料送往 LLM 前本機遮蔽個人資料

Scrub personal data before it reaches the LLM: a local redaction step for PHP

AI 導讀

php-python-ai-bridge 為 PHP 應用提供本機遮蔽流程,先以 Python worker 遮蔽個人資料,再把遮蔽後的文字送往 LLM。它以規則識別結構化資料,並以兩個 BERT NER 模型辨識人名、組織和地點;在 WNUT-17 預設閾值測試中,三類實體的 exact F1 為 0.45,人名為 0.63。

正文

Support tickets, contracts and chat logs go to hosted language models every day, and the names, card numbers and email addresses in them go along. You can promise users their data stays inside, or you can send it to an API. Doing both takes a step in between that runs on your own machine.

This post shows such a step for PHP applications: a small worker that masks personal data locally, so only the masked text leaves your network. It is php-python-ai-bridge, an MIT package I maintain. Every number below comes from measurements published in the repository, including the ones that are not flattering.

The approach

The worker is a small Python service (its HTTP layer is the standard library; the models run on ONNX Runtime) in a locked-down container (read-only filesystem, no Linux capabilities, on an internal Docker network with no route out). It finds personal data in two ways:

  • Rules with checksums for structured identifiers: card numbers (Luhn-checked), IBANs, email addresses, IPv4 and IPv6 addresses, phone numbers.
  • Two named-entity models (BERT NER, cased and uncased, int8 ONNX on CPU) for people, organisations and places, where no rule can help.

Each span it returns says where it is, what it is, and whether a rule or a model found it, with the model's score. The PHP client is dependency-free; Laravel and Symfony adapters wire it up from two environment variables.

Five minutes in Laravel

Install the package (PHP 8.2 to 8.5):

composer require cristiangirlea/php-python-ai-bridge:^0.5

Run the worker on a private Docker network. The model image downloads its pinned, hash-checked models once into a volume and then runs without network:

docker network create --internal bridge-private
docker volume create bridge-models
docker run --rm -v bridge-models:/cache ghcr.io/cristiangirlea/php-python-ai-bridge-worker-onnx:0.5.0 fetch
docker run -d --name worker --network bridge-private -e BRIDGE_TOKEN -v bridge-models:/cache:ro \
  --read-only --cap-drop ALL --security-opt no-new-privileges:true --pids-limit 128 --memory 1536m \
  --tmpfs /tmp:rw,noexec,nosuid,size=64m,uid=65532,gid=65532 \
  ghcr.io/cristiangirlea/php-python-ai-bridge-worker-onnx:0.5.0

Attach your application's container to bridge-private and set two variables in .env:

BRIDGE_URL=http://worker:8090
BRIDGE_TOKEN=<a random string of 32 or more characters, the same one the worker got>

Package discovery registers the service provider, so you can inject the client by type:

use PhpAiBridge\Client;
use PhpAiBridge\RedactResult;

final class TicketSummary
{
    public function __construct(private Client $bridge, private YourLlmClient $llm) {}

    public function summarize(string $ticket): string
    {
        $job = $this->bridge->submitRedact($ticket, ['PER', 'ORG', 'LOC']);
        $result = RedactResult::fromJob($this->bridge->wait($job->id), $ticket);

        return $this->llm->ask("Summarize this support ticket:\n" . $result->text);
    }
}

$result->text is the masked text; the original never reaches ask(). $result->spans lists what was hidden, which is worth logging (positions and labels, never the text itself) so you can see what the step does on your data.

What the spans tell you

Each span has start, end, label, source (rule:card, rule:iban and so on, or model:PER and its siblings) and score. Rule spans score 1.0, except phone numbers at 0.8, since a long digit run is not always a phone: a card number either passes the Luhn check or it doesn't. Model spans carry the model's score, and submitRedact takes a threshold ($minScore, default 0.85). Lower it and more is hidden, along with more text that was never personal.

Limits, honestly

This is a pre-filter, not a compliance control. On WNUT-17, a public set of English social-media posts full of rare and new names, the models at the default threshold find about half of the entities: exact F1 0.45 over the three labels, 0.63 for persons (precision 0.75, recall 0.54). At a threshold of 0.5, 78% of the entities overlap a masked span, but nine in ten of the 1,278 organisation spans it adds are wrong. Text that names well-known people and companies does better; informal text with nicknames does worse.

So: the rules catch nearly all structured identifiers, the models catch a good share of names, and neither replaces a review where the law requires one. The models are English-only, and the measurements are one run on one CPU box; the testing guide in the repository has the full tables and the commands to reproduce them.

Where it goes next

Multilingual models are the obvious next step. If you run it on your own data, I would like to hear what it catches and what it misses: open an issue with made-up sample text, never real documents. If you want help fitting it into an application, there is an integration help form, and GitHub Sponsors keeps the measurements coming.

來源:DEV Community · dev.to