import{s as es,n as ts,o as ss}from"../chunks/scheduler.1f027d5a.js";import{S as ls,i as as,e as p,s as a,c as o,h as ns,a as r,d as s,b as n,f as Ot,g as m,j as i,k as fe,l as ps,m as l,n as c,t as M,o as u,p as h}from"../chunks/index.cedebf1a.js";import{C as rs}from"../chunks/CopyLLMTxtMenu.9763f102.js";import{D as is,C as y}from"../chunks/DocNotebookDropdown.aa615a4d.js";import{H as Te,E as os}from"../chunks/MermaidChart.svelte_svelte_type_style_lang.71ce9e32.js";function ms(Ct){let d,Ue,we,je,T,Je,U,Ce,j,$e,J,$t='<em>Authored by: <a href="https://github.com/asusevski" rel="nofollow">Anthony Susevski</a></em>',ge,C,gt="A common complaint around adopting LLMs for enterprise use-cases are those around data privacy; particularly for teams that deal with sensitive data. While open-weight models are always a great option and <em>should be trialed if possible</em>, sometimes we just want to demo things really quickly or have really good reasons for using an LLM API. In these cases, it is good practice to have some gateway that can handle scrubbing of Personal Identifiable Information (PII) data to mitigate the risk of PII leaking.",Ie,$,It='Wealthsimple, a FinTech headquartered in Toronto Canada, have <a href="https://github.com/wealthsimple/llm-gateway" rel="nofollow">open-sourced a repo</a> that was created for exactly this purpose. In this notebook we’ll explore how we can leverage this repo to scrub our data before making an API call to an LLM provider. To do this, we’ll look at a <a href="https://huggingface.co/datasets/ai4privacy/pii-masking-200k" rel="nofollow">PII Dataset from AI4Privacy</a> and make use of the <a href="https://cohere.com/blog/free-developer-tier-announcement" rel="nofollow">free trial api</a> for Cohere’s <a href="https://huggingface.co/CohereForAI/c4ai-command-r-plus" rel="nofollow">Command R+</a> model to demonstrate the Wealthsimple repo for PII Scrubbing.',ke,g,kt='To start, follow these instructions from the <a href="https://github.com/wealthsimple/llm-gateway" rel="nofollow">README</a> to install:',_e,I,_t="<li>Install Poetry and Pyenv</li> <li>Install pyenv install 3.11.3</li> <li>Install project requirements</li>",Be,k,ve,w,Bt="<li>Run <code>cp .envrc.example .envrc</code> and update with API secrets</li>",xe,_,Ee,B,Re,v,Ze,x,vt="The wrapper obejct is a simple wrapper that applies “scrubbers” to the prompt before making the API call. Upon making a request with the wrapper, we are returned a response and a db_record object. Let’s see it in action before we dive into more specifics.",Ve,E,Ge,R,Xe,Z,Qe,V,xt=`&#123;'data': ['Michael Smith made a mistake by using PyTorch Trainer instead of HF Trainer.'], 'return_likelihoods': None, 'meta': &#123;'api_version': &#123;'version': '1'}, 'billed_units': &#123;'input_tokens': 48, 'output_tokens': 14}}}
`,Ae,G,Et="The response returns the LLM output; in this case, since we asked the model to return a summary of an already short sentence, it returned the message:",Se,X,Rt="<code>[&#39;Michael Smith made a mistake by using PyTorch Trainer instead of HF Trainer.&#39;]</code>",Ne,Q,We,A,Zt=`&#123;'user_input': 'Michael Smith ([REDACTED EMAIL ADDRESS], (+1) [REDACTED PHONE NUMBER]) committed a mistake when he used PyTorch Trainer instead of HF Trainer.\\n\\nSummarize the above text in 1-2 sentences.', 'user_email': None, 'cohere_response': &#123;'data': ['Michael Smith made a mistake by using PyTorch Trainer instead of HF Trainer.'], 'return_likelihoods': None, 'meta': &#123;'api_version': &#123;'version': '1'}, 'billed_units': &#123;'input_tokens': 48, 'output_tokens': 14}}}, 'cohere_model': 'command-r-plus', 'temperature': 0.3, 'extras': '&#123;}', 'created_at': datetime.datetime(2024, 6, 10, 2, 16, 7, 666438), 'cohere_endpoint': 'generate'}
`,He,S,Vt="The second item returned is the database record. The repo is intended for use with a postgres backend; in fact, the repo comes with a full front-end built with Docker. The postgres database is to store the chat history for the gateway. However, it is also extremely helpful as it shows us what data was actually sent in each request. As we can see, the prompt was scrubbed and the following was sent:",ze,N,Gt="<code>Michael Smith ([REDACTED EMAIL ADDRESS], (+1) [REDACTED PHONE NUMBER]) committed a mistake when he used PyTorch Trainer instead of HF Trainer.\\n\\nSummarize the above text in 1-2 sentences.</code>",Fe,W,Xt="But wait, I hear you thinking. Isn’t Michael Smith PII? Probably. But this repo does not actually implement a name scrubber. Below, we will investigate what scrubbers are applied to the prompt:",Pe,b,Qt="<p>The generate endpoint is actually deprecated for Cohere, so it would be a phenomenal open-source contribution to create and commit an integration for the new Chat endpoint for Cohere’s API.</p>",Le,H,Ye,z,At="From their repo, these are the scrubbers they implemented:",De,F,qe,P,St="The gateway will apply each scrubber sequentially.",Ke,L,Nt="This is pretty hacky, but if you really need to implement another scrubber, you can do that by modifying the wrapper’s method that calls the scrubber. Below we’ll demonstrate:",Oe,f,Wt="<p>The authors mention that the sin scrubber is particularly prone to scrubbing things, so they apply it last to ensure that other number-related PII are scrubbed first</p>",et,Y,tt,D,st,q,lt,K,Ht=`&#123;'data': ['[REDACTED PERSON] made an error by using PyTorch Trainer instead of HF Trainer. They can be contacted at [RED'], 'return_likelihoods': None, 'meta': &#123;'api_version': &#123;'version': '1'}, 'billed_units': &#123;'input_tokens': 52, 'output_tokens': 25}}}
`,at,O,nt,ee,zt=`&#123;'user_input': '[REDACTED PERSON] ([REDACTED EMAIL ADDRESS], (+1) [REDACTED PHONE NUMBER]) committed a mistake when he used PyTorch Trainer instead of HF Trainer.\\n\\nSummarize the above text in 1-2 sentences.', 'user_email': None, 'cohere_response': &#123;'data': ['[REDACTED PERSON] made an error by using PyTorch Trainer instead of HF Trainer. They can be contacted at [RED'], 'return_likelihoods': None, 'meta': &#123;'api_version': &#123;'version': '1'}, 'billed_units': &#123;'input_tokens': 52, 'output_tokens': 25}}}, 'cohere_model': 'command-r-plus', 'temperature': 0.3, 'extras': '&#123;}', 'created_at': datetime.datetime(2024, 6, 10, 2, 59, 58, 733195), 'cohere_endpoint': 'generate'}
`,pt,te,Ft="If you really have to do something like this, ensure you keep in mind that the scrubbers are applied sequentially, so if your custom scrubber interferes with any of the default scrubbers, there may be some odd behavior.",rt,se,Pt='For example, for names specifically, there are <a href="https://github.com/kylemclaren/scrub" rel="nofollow">other scrubbing libraries</a> you can explore that employ more sophisitcated algorithms to scrub PII. This repo covers more PII such as <a href="https://github.com/kylemclaren/scrub/blob/master/scrubadubdub/scrub.py" rel="nofollow">ip addresses, hostnames, etc…</a>. If all you need is to remove specific matches, however, you can revert back to the above code.',it,le,ot,ae,Lt="Let’s explore this wrapper in action on a full dataset.",mt,ne,ct,pe,Mt,re,ut,ie,Yt=`&#123;'data': ["The person is requesting an update on assessment results and is offering Kip 100,000 in exchange for the information and the recipient's account details."], 'return_likelihoods': None, 'meta': &#123;'api_version': &#123;'version': '1'}, 'billed_units': &#123;'input_tokens': 64, 'output_tokens': 33}}}
`,ht,oe,yt,me,Dt=`&#123;'user_input': "I need the latest update on assessment results. Please send the files to V[REDACTED EMAIL ADDRESS]. For your extra time, we'll offer you Kip 100,000 but please provide your лв account details.\\n\\nSummarize the above text in 1-2 sentences.", 'user_email': None, 'cohere_response': &#123;'data': ["The person is requesting an update on assessment results and is offering Kip 100,000 in exchange for the information and the recipient's account details."], 'return_likelihoods': None, 'meta': &#123;'api_version': &#123;'version': '1'}, 'billed_units': &#123;'input_tokens': 64, 'output_tokens': 33}}}, 'cohere_model': 'command-r-plus', 'temperature': 0.3, 'extras': '&#123;}', 'created_at': datetime.datetime(2024, 6, 10, 3, 10, 51, 416091), 'cohere_endpoint': 'generate'}
`,dt,ce,wt,Me,qt="Here is what the summary would have looked like if we simply sent the text as is to the endpoint:",bt,ue,ft,he,Tt,ye,Kt="To recap, in this notebook we demonstrated how to use an example Gateway for PII detection helpfully open-sourced by Wealthsimple and we built upon it by adding a custom scrubber. If you actually need reliable PII detection, ensure you run your own tests to verify that whatever scrubbing algorithms you employ actually cover your use-cases. And most importantly, wherever possible, deploying open-sourced models on infrastructure you host will always be the safest and most secure option for building with LLMs :)",Ut,de,jt,be,Jt;return T=new is({props:{classNames:"absolute z-10 right-0 top-0",options:[{label:"Google Colab",value:"https://colab.research.google.com/github/huggingface/cookbook/blob/main/notebooks/en/llm_gateway_pii_detection.ipynb"}]}}),U=new rs({props:{containerStyle:"float: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"}}),j=new Te({props:{title:"LLM Gateway for PII Detection",local:"llm-gateway-for-pii-detection",headingTag:"h1"}}),k=new y({props:{code:"YnJldyUyMGluc3RhbGwlMjBnaXRsZWFrcyUwQXBvZXRyeSUyMGluc3RhbGwlMEFwb2V0cnklMjBydW4lMjBwcmUtY29tbWl0JTIwaW5zdGFsbA==",highlighted:`brew <span class="hljs-keyword">install</span> gitleaks
poetry <span class="hljs-keyword">install</span>
poetry run pre-commit <span class="hljs-keyword">install</span>`,wrap:!1}}),_=new y({props:{code:"aW1wb3J0JTIwb3MlMEFmcm9tJTIwbGxtX2dhdGV3YXkucHJvdmlkZXJzLmNvaGVyZSUyMGltcG9ydCUyMENvaGVyZVdyYXBwZXIlMEFmcm9tJTIwZGF0YXNldHMlMjBpbXBvcnQlMjBsb2FkX2RhdGFzZXQlMEFpbXBvcnQlMjBjb2hlcmUlMEFpbXBvcnQlMjB0eXBlcyUwQWltcG9ydCUyMHJl",highlighted:`<span class="hljs-keyword">import</span> os
<span class="hljs-keyword">from</span> llm_gateway.providers.cohere <span class="hljs-keyword">import</span> CohereWrapper
<span class="hljs-keyword">from</span> datasets <span class="hljs-keyword">import</span> load_dataset
<span class="hljs-keyword">import</span> cohere
<span class="hljs-keyword">import</span> types
<span class="hljs-keyword">import</span> re`,wrap:!1}}),B=new y({props:{code:"Q09IRVJFX0FQSV9LRVklMjAlM0QlMjBvcy5lbnZpcm9uJTVCJ0NPSEVSRV9BUElfS0VZJyU1RCUwQURBVEFCQVNFX1VSTCUyMCUzRCUyMG9zLmVudmlyb24lNUInREFUQUJBU0VfVVJMJyU1RCUyMCUyMyUyMGRlZmF1bHQlMjBkYXRhYmFzZSUyMHVybCUzQSUyMCUyMnBvc3RncmVzcWwlM0ElMkYlMkZwb3N0Z3JlcyUzQXBvc3RncmVzJTQwcG9zdGdyZXMlM0E1NDMyJTJGbGxtX2dhdGV3YXklMjI=",highlighted:`COHERE_API_KEY = os.environ[<span class="hljs-string">&#x27;COHERE_API_KEY&#x27;</span>]
DATABASE_URL = os.environ[<span class="hljs-string">&#x27;DATABASE_URL&#x27;</span>] <span class="hljs-comment"># default database url: &quot;postgresql://postgres:postgres@postgres:5432/llm_gateway&quot;</span>`,wrap:!1}}),v=new Te({props:{title:"LLM Wrapper",local:"llm-wrapper",headingTag:"h2"}}),E=new y({props:{code:"d3JhcHBlciUyMCUzRCUyMENvaGVyZVdyYXBwZXIoKQ==",highlighted:"wrapper = CohereWrapper()",wrap:!1}}),R=new y({props:{code:"ZXhhbXBsZSUyMCUzRCUyMCUyMk1pY2hhZWwlMjBTbWl0aCUyMChtc21pdGglNDBnbWFpbC5jb20lMkMlMjAoJTJCMSklMjAxMTEtMTExLTExMTEpJTIwY29tbWl0dGVkJTIwYSUyMG1pc3Rha2UlMjB3aGVuJTIwaGUlMjB1c2VkJTIwUHlUb3JjaCUyMFRyYWluZXIlMjBpbnN0ZWFkJTIwb2YlMjBIRiUyMFRyYWluZXIuJTIy",highlighted:'example = <span class="hljs-string">&quot;Michael Smith (msmith@gmail.com, (+1) 111-111-1111) committed a mistake when he used PyTorch Trainer instead of HF Trainer.&quot;</span>',wrap:!1}}),Z=new y({props:{code:"cmVzcG9uc2UlMkMlMjBkYl9yZWNvcmQlMjAlM0QlMjB3cmFwcGVyLnNlbmRfY29oZXJlX3JlcXVlc3QoJTBBJTIwJTIwJTIwJTIwZW5kcG9pbnQlM0QlMjJnZW5lcmF0ZSUyMiUyQyUwQSUyMCUyMCUyMCUyMG1vZGVsJTNEJTIyY29tbWFuZC1yLXBsdXMlMjIlMkMlMEElMjAlMjAlMjAlMjBtYXhfdG9rZW5zJTNEMjUlMkMlMEElMjAlMjAlMjAlMjBwcm9tcHQlM0RmJTIyJTdCZXhhbXBsZSU3RCU1Q24lNUNuU3VtbWFyaXplJTIwdGhlJTIwYWJvdmUlMjB0ZXh0JTIwaW4lMjAxLTIlMjBzZW50ZW5jZXMuJTIyJTJDJTBBJTIwJTIwJTIwJTIwdGVtcGVyYXR1cmUlM0QwLjMlMkMlMEEpJTBBJTBBcHJpbnQocmVzcG9uc2Up",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>response, db_record = wrapper.send_cohere_request(
<span class="hljs-meta">... </span>    endpoint=<span class="hljs-string">&quot;generate&quot;</span>,
<span class="hljs-meta">... </span>    model=<span class="hljs-string">&quot;command-r-plus&quot;</span>,
<span class="hljs-meta">... </span>    max_tokens=<span class="hljs-number">25</span>,
<span class="hljs-meta">... </span>    prompt=<span class="hljs-string">f&quot;<span class="hljs-subst">{example}</span>\\n\\nSummarize the above text in 1-2 sentences.&quot;</span>,
<span class="hljs-meta">... </span>    temperature=<span class="hljs-number">0.3</span>,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(response)`,wrap:!1}}),Q=new y({props:{code:"cHJpbnQoZGJfcmVjb3JkKQ==",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(db_record)',wrap:!1}}),H=new Te({props:{title:"Scrubbers!",local:"scrubbers",headingTag:"h2"}}),F=new y({props:{code:"QUxMX1NDUlVCQkVSUyUyMCUzRCUyMCU1QiUwQSUyMCUyMCUyMCUyMHNjcnViX3Bob25lX251bWJlcnMlMkMlMEElMjAlMjAlMjAlMjBzY3J1Yl9jcmVkaXRfY2FyZF9udW1iZXJzJTJDJTBBJTIwJTIwJTIwJTIwc2NydWJfZW1haWxfYWRkcmVzc2VzJTJDJTBBJTIwJTIwJTIwJTIwc2NydWJfcG9zdGFsX2NvZGVzJTJDJTBBJTIwJTIwJTIwJTIwc2NydWJfc2luX251bWJlcnMlMkMlMEElNUQ=",highlighted:`ALL_SCRUBBERS = [
    scrub_phone_numbers,
    scrub_credit_card_numbers,
    scrub_email_addresses,
    scrub_postal_codes,
    scrub_sin_numbers,
]`,wrap:!1}}),Y=new y({props:{code:"ZGVmJTIwbXlfY3VzdG9tX3NjcnViYmVyKHRleHQlM0ElMjBzdHIpJTIwLSUzRSUyMHN0ciUzQSUwQSUyMCUyMCUyMCUyMCUyMiUyMiUyMiUwQSUyMCUyMCUyMCUyMFNjcnViJTIwTWljaGFlbCUyMFNtaXRoJTIwaW4lMjB0ZXh0JTBBJTBBJTIwJTIwJTIwJTIwJTNBcGFyYW0lMjB0ZXh0JTNBJTIwSW5wdXQlMjB0ZXh0JTIwdG8lMjBzY3J1YiUwQSUyMCUyMCUyMCUyMCUzQXR5cGUlMjB0ZXh0JTNBJTIwc3RyJTBBJTIwJTIwJTIwJTIwJTNBcmV0dXJuJTNBJTIwSW5wdXQlMjB0ZXh0JTIwd2l0aCUyMGFueSUyMG1lbnRpb25zJTIwb2YlMjBNaWNoYWVsJTIwU21pdGglMjBzY3J1YmJlZCUwQSUyMCUyMCUyMCUyMCUzQXJ0eXBlJTNBJTIwc3RyJTBBJTIwJTIwJTIwJTIwJTIyJTIyJTIyJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwcmUuc3ViKCUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHIlMjJNaWNoYWVsJTIwU21pdGglMjIlMkMlMEElMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjIlNUJSRURBQ1RFRCUyMFBFUlNPTiU1RCUyMiUyQyUwQSUyMCUyMCUyMCUyMCUyMCUyMCUyMCUyMHRleHQlMkMlMEElMjAlMjAlMjAlMjAlMjAlMjAlMjAlMjByZS5JR05PUkVDQVNFJTBBJTIwJTIwJTIwJTIwKQ==",highlighted:`<span class="hljs-keyword">def</span> <span class="hljs-title function_">my_custom_scrubber</span>(<span class="hljs-params">text: <span class="hljs-built_in">str</span></span>) -&gt; <span class="hljs-built_in">str</span>:
    <span class="hljs-string">&quot;&quot;&quot;
    Scrub Michael Smith in text

    :param text: Input text to scrub
    :type text: str
    :return: Input text with any mentions of Michael Smith scrubbed
    :rtype: str
    &quot;&quot;&quot;</span>
    <span class="hljs-keyword">return</span> re.sub(
        <span class="hljs-string">r&quot;Michael Smith&quot;</span>,

        
        <span class="hljs-string">&quot;[REDACTED PERSON]&quot;</span>,
        text,
        re.IGNORECASE
    )`,wrap:!1}}),D=new y({props:{code:"b3JpZ2luYWxfbWV0aG9kJTIwJTNEJTIwd3JhcHBlci5zZW5kX2NvaGVyZV9yZXF1ZXN0JTBBJTBBZGVmJTIwbW9kaWZpZWRfbWV0aG9kKHNlbGYlMkMlMjAqKmt3YXJncyklM0ElMEElMjAlMjAlMjAlMjBzZWxmLl92YWxpZGF0ZV9jb2hlcmVfZW5kcG9pbnQoa3dhcmdzLmdldCgnZW5kcG9pbnQnJTJDJTIwTm9uZSkpJTIwJTIzJTIwVW5mb3J0dW5hdGUlMjBkb3VibGUlMjB2YWxpZGF0ZSUyMGNvaGVyZSUyMGVuZHBvaW50JTIwY2FsbCUwQSUyMCUyMCUyMCUyMHByb21wdCUyMCUzRCUyMGt3YXJncy5nZXQoJ3Byb21wdCclMkMlMjBOb25lKSUwQSUyMCUyMCUyMCUyMHRleHQlMjAlM0QlMjBteV9jdXN0b21fc2NydWJiZXIocHJvbXB0KSUwQSUyMCUyMCUyMCUyMGt3YXJncyU1Qidwcm9tcHQnJTVEJTIwJTNEJTIwdGV4dCUwQSUyMCUyMCUyMCUyMHJldHVybiUyMG9yaWdpbmFsX21ldGhvZCgqKmt3YXJncyklMEElMEElMjMlMjBBc3NpZ24lMjB0aGUlMjBuZXclMjBtZXRob2QlMjB0byUyMHRoZSUyMGluc3RhbmNlJTBBd3JhcHBlci5zZW5kX2NvaGVyZV9yZXF1ZXN0JTIwJTNEJTIwdHlwZXMuTWV0aG9kVHlwZShtb2RpZmllZF9tZXRob2QlMkMlMjB3cmFwcGVyKQ==",highlighted:`original_method = wrapper.send_cohere_request

<span class="hljs-keyword">def</span> <span class="hljs-title function_">modified_method</span>(<span class="hljs-params">self, **kwargs</span>):
    self._validate_cohere_endpoint(kwargs.get(<span class="hljs-string">&#x27;endpoint&#x27;</span>, <span class="hljs-literal">None</span>)) <span class="hljs-comment"># Unfortunate double validate cohere endpoint call</span>
    prompt = kwargs.get(<span class="hljs-string">&#x27;prompt&#x27;</span>, <span class="hljs-literal">None</span>)
    text = my_custom_scrubber(prompt)
    kwargs[<span class="hljs-string">&#x27;prompt&#x27;</span>] = text
    <span class="hljs-keyword">return</span> original_method(**kwargs)

<span class="hljs-comment"># Assign the new method to the instance</span>
wrapper.send_cohere_request = types.MethodType(modified_method, wrapper)`,wrap:!1}}),q=new y({props:{code:"cmVzcG9uc2UlMkMlMjBkYl9yZWNvcmQlMjAlM0QlMjB3cmFwcGVyLnNlbmRfY29oZXJlX3JlcXVlc3QoJTBBJTIwJTIwJTIwJTIwZW5kcG9pbnQlM0QlMjJnZW5lcmF0ZSUyMiUyQyUwQSUyMCUyMCUyMCUyMG1vZGVsJTNEJTIyY29tbWFuZC1yLXBsdXMlMjIlMkMlMEElMjAlMjAlMjAlMjBtYXhfdG9rZW5zJTNEMjUlMkMlMEElMjAlMjAlMjAlMjBwcm9tcHQlM0RmJTIyJTdCZXhhbXBsZSU3RCU1Q24lNUNuU3VtbWFyaXplJTIwdGhlJTIwYWJvdmUlMjB0ZXh0JTIwaW4lMjAxLTIlMjBzZW50ZW5jZXMuJTIyJTJDJTBBJTIwJTIwJTIwJTIwdGVtcGVyYXR1cmUlM0QwLjMlMkMlMEEpJTBBJTBBcHJpbnQocmVzcG9uc2Up",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>response, db_record = wrapper.send_cohere_request(
<span class="hljs-meta">... </span>    endpoint=<span class="hljs-string">&quot;generate&quot;</span>,
<span class="hljs-meta">... </span>    model=<span class="hljs-string">&quot;command-r-plus&quot;</span>,
<span class="hljs-meta">... </span>    max_tokens=<span class="hljs-number">25</span>,
<span class="hljs-meta">... </span>    prompt=<span class="hljs-string">f&quot;<span class="hljs-subst">{example}</span>\\n\\nSummarize the above text in 1-2 sentences.&quot;</span>,
<span class="hljs-meta">... </span>    temperature=<span class="hljs-number">0.3</span>,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(response)`,wrap:!1}}),O=new y({props:{code:"cHJpbnQoZGJfcmVjb3JkKQ==",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(db_record)',wrap:!1}}),le=new Te({props:{title:"Dataset",local:"dataset",headingTag:"h2"}}),ne=new y({props:{code:"cGlpX2RzJTIwJTNEJTIwbG9hZF9kYXRhc2V0KCUyMmFpNHByaXZhY3klMkZwaWktbWFza2luZy0yMDBrJTIyKQ==",highlighted:'pii_ds = load_dataset(<span class="hljs-string">&quot;ai4privacy/pii-masking-200k&quot;</span>)',wrap:!1}}),pe=new y({props:{code:"cGlpX2RzJTVCJ3RyYWluJyU1RCU1QjM2JTVEJTVCJ3NvdXJjZV90ZXh0JyU1RA==",highlighted:'pii_ds[<span class="hljs-string">&#x27;train&#x27;</span>][<span class="hljs-number">36</span>][<span class="hljs-string">&#x27;source_text&#x27;</span>]',wrap:!1}}),re=new y({props:{code:"ZXhhbXBsZSUyMCUzRCUyMHBpaV9kcyU1Qid0cmFpbiclNUQlNUIzNiU1RCU1Qidzb3VyY2VfdGV4dCclNUQlMEElMEFyZXNwb25zZSUyQyUyMGRiX3JlY29yZCUyMCUzRCUyMHdyYXBwZXIuc2VuZF9jb2hlcmVfcmVxdWVzdCglMEElMjAlMjAlMjAlMjBlbmRwb2ludCUzRCUyMmdlbmVyYXRlJTIyJTJDJTBBJTIwJTIwJTIwJTIwbW9kZWwlM0QlMjJjb21tYW5kLXItcGx1cyUyMiUyQyUwQSUyMCUyMCUyMCUyMG1heF90b2tlbnMlM0Q1MCUyQyUwQSUyMCUyMCUyMCUyMHByb21wdCUzRGYlMjIlN0JleGFtcGxlJTdEJTVDbiU1Q25TdW1tYXJpemUlMjB0aGUlMjBhYm92ZSUyMHRleHQlMjBpbiUyMDEtMiUyMHNlbnRlbmNlcy4lMjIlMkMlMEElMjAlMjAlMjAlMjB0ZW1wZXJhdHVyZSUzRDAuMyUyQyUwQSklMEElMEFwcmludChyZXNwb25zZSk=",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>example = pii_ds[<span class="hljs-string">&#x27;train&#x27;</span>][<span class="hljs-number">36</span>][<span class="hljs-string">&#x27;source_text&#x27;</span>]

<span class="hljs-meta">&gt;&gt;&gt; </span>response, db_record = wrapper.send_cohere_request(
<span class="hljs-meta">... </span>    endpoint=<span class="hljs-string">&quot;generate&quot;</span>,
<span class="hljs-meta">... </span>    model=<span class="hljs-string">&quot;command-r-plus&quot;</span>,
<span class="hljs-meta">... </span>    max_tokens=<span class="hljs-number">50</span>,
<span class="hljs-meta">... </span>    prompt=<span class="hljs-string">f&quot;<span class="hljs-subst">{example}</span>\\n\\nSummarize the above text in 1-2 sentences.&quot;</span>,
<span class="hljs-meta">... </span>    temperature=<span class="hljs-number">0.3</span>,
<span class="hljs-meta">... </span>)

<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(response)`,wrap:!1}}),oe=new y({props:{code:"cHJpbnQoZGJfcmVjb3JkKQ==",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(db_record)',wrap:!1}}),ce=new Te({props:{title:"Regular Output",local:"regular-output",headingTag:"h2"}}),ue=new y({props:{code:"JTIwY28lMjAlM0QlMjBjb2hlcmUuQ2xpZW50KCUwQSUyMCUyMCUyMCUyMGFwaV9rZXklM0Rvcy5lbnZpcm9uJTVCJ0NPSEVSRV9BUElfS0VZJyU1RCUwQSUwQSUwQSUyMGVzcG9uc2VfdmFuaWxsYSUyMCUzRCUyMGNvLmdlbmVyYXRlKCUwQSUyMCUyMCUyMCUyMHByb21wdCUzRGYlMjIlN0JleGFtcGxlJTdEJTVDbiU1Q25TdW1tYXJpemUlMjB0aGUlMjBhYm92ZSUyMHRleHQlMjBpbiUyMDEtMiUyMHNlbnRlbmNlcy4lMjIlMkMlMEElMjAlMjAlMjAlMjBtb2RlbCUzRCUyMmNvbW1hbmQtci1wbHVzJTIyJTJDJTBBJTIwJTIwJTIwJTIwbWF4X3Rva2VucyUzRDUwJTJDJTBBJTIwJTIwJTIwJTIwdGVtcGVyYXR1cmUlM0QwLjM=",highlighted:` co = cohere.Client(
    api_key=os.environ[<span class="hljs-string">&#x27;COHERE_API_KEY&#x27;</span>]


 esponse_vanilla = co.generate(
    prompt=<span class="hljs-string">f&quot;<span class="hljs-subst">{example}</span>\\n\\nSummarize the above text in 1-2 sentences.&quot;</span>,
    model=<span class="hljs-string">&quot;command-r-plus&quot;</span>,
    max_tokens=<span class="hljs-number">50</span>,
    temperature=<span class="hljs-number">0.3</span>`,wrap:!1}}),he=new y({props:{code:"cmVzcG9uc2VfdmFuaWxsYQ==",highlighted:"response_vanilla",wrap:!1}}),de=new os({props:{source:"https://github.com/huggingface/cookbook/blob/main/notebooks/en/llm_gateway_pii_detection.md"}}),{c(){d=p("meta"),Ue=a(),we=p("p"),je=a(),o(T.$$.fragment),Je=a(),o(U.$$.fragment),Ce=a(),o(j.$$.fragment),$e=a(),J=p("p"),J.innerHTML=$t,ge=a(),C=p("p"),C.innerHTML=gt,Ie=a(),$=p("p"),$.innerHTML=It,ke=a(),g=p("p"),g.innerHTML=kt,_e=a(),I=p("ol"),I.innerHTML=_t,Be=a(),o(k.$$.fragment),ve=a(),w=p("ol"),w.innerHTML=Bt,xe=a(),o(_.$$.fragment),Ee=a(),o(B.$$.fragment),Re=a(),o(v.$$.fragment),Ze=a(),x=p("p"),x.textContent=vt,Ve=a(),o(E.$$.fragment),Ge=a(),o(R.$$.fragment),Xe=a(),o(Z.$$.fragment),Qe=a(),V=p("pre"),V.textContent=xt,Ae=a(),G=p("p"),G.textContent=Et,Se=a(),X=p("p"),X.innerHTML=Rt,Ne=a(),o(Q.$$.fragment),We=a(),A=p("pre"),A.textContent=Zt,He=a(),S=p("p"),S.textContent=Vt,ze=a(),N=p("p"),N.innerHTML=Gt,Fe=a(),W=p("p"),W.textContent=Xt,Pe=a(),b=p("blockquote"),b.innerHTML=Qt,Le=a(),o(H.$$.fragment),Ye=a(),z=p("p"),z.textContent=At,De=a(),o(F.$$.fragment),qe=a(),P=p("p"),P.textContent=St,Ke=a(),L=p("p"),L.textContent=Nt,Oe=a(),f=p("blockquote"),f.innerHTML=Wt,et=a(),o(Y.$$.fragment),tt=a(),o(D.$$.fragment),st=a(),o(q.$$.fragment),lt=a(),K=p("pre"),K.textContent=Ht,at=a(),o(O.$$.fragment),nt=a(),ee=p("pre"),ee.textContent=zt,pt=a(),te=p("p"),te.textContent=Ft,rt=a(),se=p("p"),se.innerHTML=Pt,it=a(),o(le.$$.fragment),ot=a(),ae=p("p"),ae.textContent=Lt,mt=a(),o(ne.$$.fragment),ct=a(),o(pe.$$.fragment),Mt=a(),o(re.$$.fragment),ut=a(),ie=p("pre"),ie.textContent=Yt,ht=a(),o(oe.$$.fragment),yt=a(),me=p("pre"),me.textContent=Dt,dt=a(),o(ce.$$.fragment),wt=a(),Me=p("p"),Me.textContent=qt,bt=a(),o(ue.$$.fragment),ft=a(),o(he.$$.fragment),Tt=a(),ye=p("p"),ye.textContent=Kt,Ut=a(),o(de.$$.fragment),jt=a(),be=p("p"),this.h()},l(e){const t=ns("svelte-u9bgzb",document.head);d=r(t,"META",{name:!0,content:!0}),t.forEach(s),Ue=n(e),we=r(e,"P",{}),Ot(we).forEach(s),je=n(e),m(T.$$.fragment,e),Je=n(e),m(U.$$.fragment,e),Ce=n(e),m(j.$$.fragment,e),$e=n(e),J=r(e,"P",{"data-svelte-h":!0}),i(J)!=="svelte-10g1t83"&&(J.innerHTML=$t),ge=n(e),C=r(e,"P",{"data-svelte-h":!0}),i(C)!=="svelte-msdsl8"&&(C.innerHTML=gt),Ie=n(e),$=r(e,"P",{"data-svelte-h":!0}),i($)!=="svelte-1acsyb0"&&($.innerHTML=It),ke=n(e),g=r(e,"P",{"data-svelte-h":!0}),i(g)!=="svelte-1uh518a"&&(g.innerHTML=kt),_e=n(e),I=r(e,"OL",{"data-svelte-h":!0}),i(I)!=="svelte-70jl22"&&(I.innerHTML=_t),Be=n(e),m(k.$$.fragment,e),ve=n(e),w=r(e,"OL",{start:!0,"data-svelte-h":!0}),i(w)!=="svelte-1p91lr0"&&(w.innerHTML=Bt),xe=n(e),m(_.$$.fragment,e),Ee=n(e),m(B.$$.fragment,e),Re=n(e),m(v.$$.fragment,e),Ze=n(e),x=r(e,"P",{"data-svelte-h":!0}),i(x)!=="svelte-nmoklt"&&(x.textContent=vt),Ve=n(e),m(E.$$.fragment,e),Ge=n(e),m(R.$$.fragment,e),Xe=n(e),m(Z.$$.fragment,e),Qe=n(e),V=r(e,"PRE",{"data-svelte-h":!0}),i(V)!=="svelte-fgwnhs"&&(V.textContent=xt),Ae=n(e),G=r(e,"P",{"data-svelte-h":!0}),i(G)!=="svelte-hjvwqj"&&(G.textContent=Et),Se=n(e),X=r(e,"P",{"data-svelte-h":!0}),i(X)!=="svelte-2gbnj8"&&(X.innerHTML=Rt),Ne=n(e),m(Q.$$.fragment,e),We=n(e),A=r(e,"PRE",{"data-svelte-h":!0}),i(A)!=="svelte-1obiiyg"&&(A.textContent=Zt),He=n(e),S=r(e,"P",{"data-svelte-h":!0}),i(S)!=="svelte-196tu5x"&&(S.textContent=Vt),ze=n(e),N=r(e,"P",{"data-svelte-h":!0}),i(N)!=="svelte-1jrlmt8"&&(N.innerHTML=Gt),Fe=n(e),W=r(e,"P",{"data-svelte-h":!0}),i(W)!=="svelte-1xq8mdu"&&(W.textContent=Xt),Pe=n(e),b=r(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(b)!=="svelte-l5reb8"&&(b.innerHTML=Qt),Le=n(e),m(H.$$.fragment,e),Ye=n(e),z=r(e,"P",{"data-svelte-h":!0}),i(z)!=="svelte-e4th09"&&(z.textContent=At),De=n(e),m(F.$$.fragment,e),qe=n(e),P=r(e,"P",{"data-svelte-h":!0}),i(P)!=="svelte-o1505i"&&(P.textContent=St),Ke=n(e),L=r(e,"P",{"data-svelte-h":!0}),i(L)!=="svelte-pky6il"&&(L.textContent=Nt),Oe=n(e),f=r(e,"BLOCKQUOTE",{class:!0,"data-svelte-h":!0}),i(f)!=="svelte-17voav7"&&(f.innerHTML=Wt),et=n(e),m(Y.$$.fragment,e),tt=n(e),m(D.$$.fragment,e),st=n(e),m(q.$$.fragment,e),lt=n(e),K=r(e,"PRE",{"data-svelte-h":!0}),i(K)!=="svelte-1ibz1w3"&&(K.textContent=Ht),at=n(e),m(O.$$.fragment,e),nt=n(e),ee=r(e,"PRE",{"data-svelte-h":!0}),i(ee)!=="svelte-s3f3d0"&&(ee.textContent=zt),pt=n(e),te=r(e,"P",{"data-svelte-h":!0}),i(te)!=="svelte-20zvgm"&&(te.textContent=Ft),rt=n(e),se=r(e,"P",{"data-svelte-h":!0}),i(se)!=="svelte-1xpcfov"&&(se.innerHTML=Pt),it=n(e),m(le.$$.fragment,e),ot=n(e),ae=r(e,"P",{"data-svelte-h":!0}),i(ae)!=="svelte-1ghfd0h"&&(ae.textContent=Lt),mt=n(e),m(ne.$$.fragment,e),ct=n(e),m(pe.$$.fragment,e),Mt=n(e),m(re.$$.fragment,e),ut=n(e),ie=r(e,"PRE",{"data-svelte-h":!0}),i(ie)!=="svelte-1ap78y8"&&(ie.textContent=Yt),ht=n(e),m(oe.$$.fragment,e),yt=n(e),me=r(e,"PRE",{"data-svelte-h":!0}),i(me)!=="svelte-45v8zv"&&(me.textContent=Dt),dt=n(e),m(ce.$$.fragment,e),wt=n(e),Me=r(e,"P",{"data-svelte-h":!0}),i(Me)!=="svelte-z0u4id"&&(Me.textContent=qt),bt=n(e),m(ue.$$.fragment,e),ft=n(e),m(he.$$.fragment,e),Tt=n(e),ye=r(e,"P",{"data-svelte-h":!0}),i(ye)!=="svelte-es6asg"&&(ye.textContent=Kt),Ut=n(e),m(de.$$.fragment,e),jt=n(e),be=r(e,"P",{}),Ot(be).forEach(s),this.h()},h(){fe(d,"name","hf:doc:metadata"),fe(d,"content",cs),fe(w,"start","4"),fe(b,"class","tip"),fe(f,"class","tip")},m(e,t){ps(document.head,d),l(e,Ue,t),l(e,we,t),l(e,je,t),c(T,e,t),l(e,Je,t),c(U,e,t),l(e,Ce,t),c(j,e,t),l(e,$e,t),l(e,J,t),l(e,ge,t),l(e,C,t),l(e,Ie,t),l(e,$,t),l(e,ke,t),l(e,g,t),l(e,_e,t),l(e,I,t),l(e,Be,t),c(k,e,t),l(e,ve,t),l(e,w,t),l(e,xe,t),c(_,e,t),l(e,Ee,t),c(B,e,t),l(e,Re,t),c(v,e,t),l(e,Ze,t),l(e,x,t),l(e,Ve,t),c(E,e,t),l(e,Ge,t),c(R,e,t),l(e,Xe,t),c(Z,e,t),l(e,Qe,t),l(e,V,t),l(e,Ae,t),l(e,G,t),l(e,Se,t),l(e,X,t),l(e,Ne,t),c(Q,e,t),l(e,We,t),l(e,A,t),l(e,He,t),l(e,S,t),l(e,ze,t),l(e,N,t),l(e,Fe,t),l(e,W,t),l(e,Pe,t),l(e,b,t),l(e,Le,t),c(H,e,t),l(e,Ye,t),l(e,z,t),l(e,De,t),c(F,e,t),l(e,qe,t),l(e,P,t),l(e,Ke,t),l(e,L,t),l(e,Oe,t),l(e,f,t),l(e,et,t),c(Y,e,t),l(e,tt,t),c(D,e,t),l(e,st,t),c(q,e,t),l(e,lt,t),l(e,K,t),l(e,at,t),c(O,e,t),l(e,nt,t),l(e,ee,t),l(e,pt,t),l(e,te,t),l(e,rt,t),l(e,se,t),l(e,it,t),c(le,e,t),l(e,ot,t),l(e,ae,t),l(e,mt,t),c(ne,e,t),l(e,ct,t),c(pe,e,t),l(e,Mt,t),c(re,e,t),l(e,ut,t),l(e,ie,t),l(e,ht,t),c(oe,e,t),l(e,yt,t),l(e,me,t),l(e,dt,t),c(ce,e,t),l(e,wt,t),l(e,Me,t),l(e,bt,t),c(ue,e,t),l(e,ft,t),c(he,e,t),l(e,Tt,t),l(e,ye,t),l(e,Ut,t),c(de,e,t),l(e,jt,t),l(e,be,t),Jt=!0},p:ts,i(e){Jt||(M(T.$$.fragment,e),M(U.$$.fragment,e),M(j.$$.fragment,e),M(k.$$.fragment,e),M(_.$$.fragment,e),M(B.$$.fragment,e),M(v.$$.fragment,e),M(E.$$.fragment,e),M(R.$$.fragment,e),M(Z.$$.fragment,e),M(Q.$$.fragment,e),M(H.$$.fragment,e),M(F.$$.fragment,e),M(Y.$$.fragment,e),M(D.$$.fragment,e),M(q.$$.fragment,e),M(O.$$.fragment,e),M(le.$$.fragment,e),M(ne.$$.fragment,e),M(pe.$$.fragment,e),M(re.$$.fragment,e),M(oe.$$.fragment,e),M(ce.$$.fragment,e),M(ue.$$.fragment,e),M(he.$$.fragment,e),M(de.$$.fragment,e),Jt=!0)},o(e){u(T.$$.fragment,e),u(U.$$.fragment,e),u(j.$$.fragment,e),u(k.$$.fragment,e),u(_.$$.fragment,e),u(B.$$.fragment,e),u(v.$$.fragment,e),u(E.$$.fragment,e),u(R.$$.fragment,e),u(Z.$$.fragment,e),u(Q.$$.fragment,e),u(H.$$.fragment,e),u(F.$$.fragment,e),u(Y.$$.fragment,e),u(D.$$.fragment,e),u(q.$$.fragment,e),u(O.$$.fragment,e),u(le.$$.fragment,e),u(ne.$$.fragment,e),u(pe.$$.fragment,e),u(re.$$.fragment,e),u(oe.$$.fragment,e),u(ce.$$.fragment,e),u(ue.$$.fragment,e),u(he.$$.fragment,e),u(de.$$.fragment,e),Jt=!1},d(e){e&&(s(Ue),s(we),s(je),s(Je),s(Ce),s($e),s(J),s(ge),s(C),s(Ie),s($),s(ke),s(g),s(_e),s(I),s(Be),s(ve),s(w),s(xe),s(Ee),s(Re),s(Ze),s(x),s(Ve),s(Ge),s(Xe),s(Qe),s(V),s(Ae),s(G),s(Se),s(X),s(Ne),s(We),s(A),s(He),s(S),s(ze),s(N),s(Fe),s(W),s(Pe),s(b),s(Le),s(Ye),s(z),s(De),s(qe),s(P),s(Ke),s(L),s(Oe),s(f),s(et),s(tt),s(st),s(lt),s(K),s(at),s(nt),s(ee),s(pt),s(te),s(rt),s(se),s(it),s(ot),s(ae),s(mt),s(ct),s(Mt),s(ut),s(ie),s(ht),s(yt),s(me),s(dt),s(wt),s(Me),s(bt),s(ft),s(Tt),s(ye),s(Ut),s(jt),s(be)),s(d),h(T,e),h(U,e),h(j,e),h(k,e),h(_,e),h(B,e),h(v,e),h(E,e),h(R,e),h(Z,e),h(Q,e),h(H,e),h(F,e),h(Y,e),h(D,e),h(q,e),h(O,e),h(le,e),h(ne,e),h(pe,e),h(re,e),h(oe,e),h(ce,e),h(ue,e),h(he,e),h(de,e)}}}const cs='{"title":"LLM Gateway for PII Detection","local":"llm-gateway-for-pii-detection","sections":[{"title":"LLM Wrapper","local":"llm-wrapper","sections":[],"depth":2},{"title":"Scrubbers!","local":"scrubbers","sections":[],"depth":2},{"title":"Dataset","local":"dataset","sections":[],"depth":2},{"title":"Regular Output","local":"regular-output","sections":[],"depth":2}],"depth":1}';function Ms(Ct){return ss(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class bs extends ls{constructor(d){super(),as(this,d,Ms,ms,es,{})}}export{bs as component};
