<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Cristian Gîrlea</title>
    <description>The latest articles on DEV Community by Cristian Gîrlea (@cristiangirlea).</description>
    <link>https://dev.to/cristiangirlea</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4256060%2F73558b7d-42b6-43ea-875a-c1fb51f6d9b4.png</url>
      <title>DEV Community: Cristian Gîrlea</title>
      <link>https://dev.to/cristiangirlea</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/cristiangirlea"/>
    <language>en</language>
    <item>
      <title>Scrub personal data before it reaches the LLM: a local redaction step for PHP</title>
      <dc:creator>Cristian Gîrlea</dc:creator>
      <pubDate>Fri, 20 Nov 2026 16:58:06 +0000</pubDate>
      <link>https://dev.to/cristiangirlea/scrub-personal-data-before-it-reaches-the-llm-a-local-redaction-step-for-php-55p2</link>
      <guid>https://dev.to/cristiangirlea/scrub-personal-data-before-it-reaches-the-llm-a-local-redaction-step-for-php-55p2</guid>
      <description>&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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 &lt;a href="https://github.com/cristiangirlea/php-python-ai-bridge" rel="noopener noreferrer"&gt;php-python-ai-bridge&lt;/a&gt;, an MIT package I maintain. Every number below comes from measurements published in the repository, including the ones that are not flattering.&lt;/p&gt;

&lt;h2&gt;
  
  
  The approach
&lt;/h2&gt;

&lt;p&gt;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:&lt;/p&gt;

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

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Five minutes in Laravel
&lt;/h2&gt;

&lt;p&gt;Install the package (PHP 8.2 to 8.5):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;composer require cristiangirlea/php-python-ai-bridge:^0.5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;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
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Attach your application's container to &lt;code&gt;bridge-private&lt;/code&gt; and set two variables in &lt;code&gt;.env&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BRIDGE_URL=http://worker:8090
BRIDGE_TOKEN=&amp;lt;a random string of 32 or more characters, the same one the worker got&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Package discovery registers the service provider, so you can inject the client by type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;PhpAiBridge&lt;/span&gt;\&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;use&lt;/span&gt; &lt;span class="nc"&gt;PhpAiBridge&lt;/span&gt;\&lt;span class="nc"&gt;RedactResult&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;final&lt;/span&gt; &lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;TicketSummary&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Client&lt;/span&gt; &lt;span class="nv"&gt;$bridge&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;YourLlmClient&lt;/span&gt; &lt;span class="nv"&gt;$llm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;summarize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$ticket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nv"&gt;$job&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;bridge&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;submitRedact&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$ticket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'PER'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'ORG'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'LOC'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nv"&gt;$result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;RedactResult&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;fromJob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;bridge&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;wait&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$job&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$ticket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;llm&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;ask&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Summarize this support ticket:\n"&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt; &lt;span class="nv"&gt;$result&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;$result-&amp;gt;text&lt;/code&gt; is the masked text; the original never reaches &lt;code&gt;ask()&lt;/code&gt;. &lt;code&gt;$result-&amp;gt;spans&lt;/code&gt; 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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What the spans tell you
&lt;/h2&gt;

&lt;p&gt;Each span has &lt;code&gt;start&lt;/code&gt;, &lt;code&gt;end&lt;/code&gt;, &lt;code&gt;label&lt;/code&gt;, &lt;code&gt;source&lt;/code&gt; (&lt;code&gt;rule:card&lt;/code&gt;, &lt;code&gt;rule:iban&lt;/code&gt; and so on, or &lt;code&gt;model:PER&lt;/code&gt; and its siblings) and &lt;code&gt;score&lt;/code&gt;. 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 &lt;code&gt;submitRedact&lt;/code&gt; takes a threshold (&lt;code&gt;$minScore&lt;/code&gt;, default 0.85). Lower it and more is hidden, along with more text that was never personal.&lt;/p&gt;

&lt;h2&gt;
  
  
  Limits, honestly
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where it goes next
&lt;/h2&gt;

&lt;p&gt;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 &lt;a href="https://github.com/cristiangirlea/php-python-ai-bridge/issues/new?template=integration-help.yml" rel="noopener noreferrer"&gt;integration help form&lt;/a&gt;, and &lt;a href="https://github.com/sponsors/cristiangirlea" rel="noopener noreferrer"&gt;GitHub Sponsors&lt;/a&gt; keeps the measurements coming.&lt;/p&gt;

</description>
      <category>php</category>
      <category>laravel</category>
      <category>privacy</category>
      <category>ai</category>
    </item>
  </channel>
</rss>
