import{s as Kt,n as Ot,o as es}from"../chunks/scheduler.65852ee5.js";import{S as ts,i as ss,g as p,s as n,r,A as ls,h as i,f as s,c as a,j as Dt,u as o,x as m,k as ft,y as ns,a as l,v as M,d as c,t as u,w as T}from"../chunks/index.aa74147d.js";import{D as as,C as J}from"../chunks/DocNotebookDropdown.1a7e6a42.js";import{H as we,E as ps}from"../chunks/index.e2566494.js";function is(Ut){let h,de,he,be,w,fe,d,Ue,b,jt='<em>作者: <a href="https://github.com/asusevski" rel="nofollow">Anthony Susevski</a></em>',je,f,It="采用大语言模型（LLM）进行企业级应用时，常见的投诉之一就是数据隐私问题，尤其是对于处理敏感数据的团队来说。尽管开源模型通常是一个不错的选择，<em>如果可能的话应该尝试使用</em>，但有时我们只想快速演示一下，或者有充分的理由使用 LLM API。在这种情况下，最好采用某种网关来处理个人身份信息（PII）数据的清洗，从而降低 PII 泄露的风险。",Ie,U,$t='总部位于加拿大多伦多的金融科技公司 <strong>Wealthsimple</strong> 已经为了这个目的 <a href="https://github.com/wealthsimple/llm-gateway" rel="nofollow">开源了一个代码库</a>。在本 Notebook 中，我们将探索如何利用这个代码库，在向 LLM 提供商发出 API 调用之前，对数据进行清洗。为此，我们将使用来自<a href="https://huggingface.co/datasets/ai4privacy/pii-masking-200k" rel="nofollow">AI4Privacy</a>的 PII 数据集，并使用 Cohere 的 <a href="https://huggingface.co/CohereForAI/c4ai-command-r-plus" rel="nofollow">Command R+</a>模型的<a href="https://cohere.com/blog/free-developer-tier-announcement" rel="nofollow">免费试用 API</a>，演示 Wealthsimple 的 PII 清洗功能。',$e,j,Ct='首先，请按照 <a href="https://github.com/wealthsimple/llm-gateway" rel="nofollow">README</a> 中的说明进行安装：',Ce,I,_t="<li>安装 Poetry 和 Pyenv</li> <li>安装 pyenv 版本 3.11.3</li> <li>安装项目所需的依赖：</li>",_e,$,Be,y,Bt="<li>运行 <code>cp .envrc.example .envrc</code> 并用 API 密钥更新配置。</li>",ge,C,ke,_,ve,B,Ee,g,gt="包装器对象是一个简单的封装器，它在发起 API 调用之前，将“清洗器”应用到输入的提示语上。使用包装器发起请求时，我们将获得一个响应和一个 <code>db_record</code> 对象。在深入了解更多细节之前，让我们先来看一下它的实际应用。",Re,k,Ve,v,Ze,E,xe,R,kt=`&#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}}}
`,Ne,V,vt="响应返回的是 LLM 的输出；在这个例子中，由于我们要求模型对一个已经很简短的句子进行总结，它返回了以下消息：",Xe,Z,Et="<code>[&#39;Michael Smith made a mistake by using PyTorch Trainer instead of HF Trainer.&#39;]</code>",Ge,x,Qe,N,Rt=`&#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'}
`,Se,X,Vt="第二个返回项是数据库记录。该代码库是为使用 Postgres 后端而设计的；实际上，代码库自带一个使用 Docker 构建的完整前端。Postgres 数据库用于存储网关的聊天历史记录。然而，它也非常有用，因为它展示了每个请求中实际发送的数据。如我们所见，提示语经过了清洗，实际发送的内容如下：",He,G,Zt="<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>",We,Q,xt="但等等，我听到你在想。Michael Smith 不就是 PII 吗？确实是。但是这个代码库实际上并没有实现姓名清洗功能。接下来，我们将探讨在提示语中应用了哪些清洗器：",Ae,S,Nt=`<p>[!提示]<br/>
Cohere 的 <code>generate</code> 端点实际上已经被弃用，因此，如果有人能为 Cohere API 的新的 Chat 端点创建并提交集成，这将是一个非常棒的开源贡献。</p>`,ze,H,Pe,W,Xt="根据他们的代码库，以下是实现的清洗器：",qe,A,Fe,z,Gt="网关会依次应用每个清洗器。",Le,P,Qt="这虽然有些 “hacky”（不够优雅），但如果你确实需要实现另一个清洗器，可以通过修改包装器方法来实现。以下是一个演示：",Ye,q,St=`<p>[!提示]<br/>
作者提到，SIN（社会保险号）清洗器特别容易误清洗数据，因此它会被放在最后，以确保其他与数字相关的 PII 先被清洗。</p>`,De,F,Ke,L,Oe,Y,et,D,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}}}
`,tt,K,st,O,Wt=`&#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'}
`,lt,ee,At="如果你确实需要这样做，请务必记住，清洗器是按顺序应用的，因此，如果你的自定义清洗器与任何默认清洗器发生冲突，可能会导致一些意外行为。",nt,te,zt='例如，针对姓名的清洗，实际上有<a href="https://github.com/kylemclaren/scrub" rel="nofollow">其他清洗库</a>可以探索，这些库采用更复杂的算法来清洗 PII。这个代码库涵盖了更多的PII类型，例如 <a href="https://github.com/kylemclaren/scrub/blob/master/scrubadubdub/scrub.py" rel="nofollow">IP 地址、主机名等</a>。然而，如果你仅仅需要删除特定的匹配项，你仍然可以使用上述代码进行处理。',at,se,pt,le,Pt="让我们在一个完整的数据集上演示这个包装器的实际应用。",it,ne,mt,ae,rt,pe,ot,ie,qt=`&#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}}}
`,Mt,me,ct,re,Ft=`&#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'}
`,ut,oe,Tt,Me,Lt="如果我们直接将文本发送到端点而不进行任何清洗，摘要结果如下所示：",Jt,ce,ht,ue,yt,Te,Yt="总结一下，在 Notebook 中，我们演示了如何使用 Wealthsimple 开源的 PII 检测示例网关，并在此基础上添加了自定义清洗器。如果你真的需要可靠的 PII 检测，确保运行自己的测试，验证你所采用的清洗算法是否真正覆盖了你的应用场景。最重要的是，尽可能在你自己托管的基础设施上部署开源模型，这将始终是构建 LLM 应用时最安全、最可靠的选择 :)",wt,Je,dt,ye,bt;return w=new as({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/zh-CN/llm_gateway_pii_detection.ipynb"}]}}),d=new we({props:{title:"个人身份信息（PII）检测的 LLM 网关",local:"个人身份信息pii检测的-llm-网关",headingTag:"h1"}}),$=new J({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}}),C=new J({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}}),_=new J({props:{code:"Q09IRVJFX0FQSV9LRVklMjAlM0QlMjBvcy5lbnZpcm9uJTVCJTIyQ09IRVJFX0FQSV9LRVklMjIlNUQlMEFEQVRBQkFTRV9VUkwlMjAlM0QlMjBvcy5lbnZpcm9uJTVCJTBBJTIwJTIwJTIwJTIwJTIyREFUQUJBU0VfVVJMJTIyJTBBJTVEJTIwJTIwJTIzJTIwZGVmYXVsdCUyMGRhdGFiYXNlJTIwdXJsJTNBJTIwJTIycG9zdGdyZXNxbCUzQSUyRiUyRnBvc3RncmVzJTNBcG9zdGdyZXMlNDBwb3N0Z3JlcyUzQTU0MzIlMkZsbG1fZ2F0ZXdheSUyMg==",highlighted:`COHERE_API_KEY = os.environ[<span class="hljs-string">&quot;COHERE_API_KEY&quot;</span>]
DATABASE_URL = os.environ[
    <span class="hljs-string">&quot;DATABASE_URL&quot;</span>
]  <span class="hljs-comment"># default database url: &quot;postgresql://postgres:postgres@postgres:5432/llm_gateway&quot;</span>`,wrap:!1}}),B=new we({props:{title:"LLM 包装器",local:"llm-包装器",headingTag:"h2"}}),k=new J({props:{code:"d3JhcHBlciUyMCUzRCUyMENvaGVyZVdyYXBwZXIoKQ==",highlighted:"wrapper = CohereWrapper()",wrap:!1}}),v=new J({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}}),E=new J({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}}),x=new J({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 we({props:{title:"清洗器！",local:"清洗器",headingTag:"h2"}}),A=new J({props:{code:"QUxMX1NDUlVCQkVSUyUyMCUzRCUyMCU1QiUwQSUyMCUyMCUyMCUyMHNjcnViX3Bob25lX251bWJlcnMlMkMlMEElMjAlMjAlMjAlMjBzY3J1Yl9jcmVkaXRfY2FyZF9udW1iZXJzJTJDJTBBJTIwJTIwJTIwJTIwc2NydWJfZW1haWxfYWRkcmVzc2VzJTJDJTBBJTIwJTIwJTIwJTIwc2NydWJfcG9zdGFsX2NvZGVzJTJDJTBBJTIwJTIwJTIwJTIwc2NydWJfc2luX251bWJlcnMlMkMlMEElNUQ=",highlighted:`ALL_SCRUBBERS = [
    scrub_phone_numbers,
    scrub_credit_card_numbers,
    scrub_email_addresses,
    scrub_postal_codes,
    scrub_sin_numbers,
]`,wrap:!1}}),F=new J({props:{code:"ZGVmJTIwbXlfY3VzdG9tX3NjcnViYmVyKHRleHQlM0ElMjBzdHIpJTIwLSUzRSUyMHN0ciUzQSUwQSUyMCUyMCUyMCUyMCUyMiUyMiUyMiUwQSUyMCUyMCUyMCUyMFNjcnViJTIwTWljaGFlbCUyMFNtaXRoJTIwaW4lMjB0ZXh0JTBBJTBBJTIwJTIwJTIwJTIwJTNBcGFyYW0lMjB0ZXh0JTNBJTIwSW5wdXQlMjB0ZXh0JTIwdG8lMjBzY3J1YiUwQSUyMCUyMCUyMCUyMCUzQXR5cGUlMjB0ZXh0JTNBJTIwc3RyJTBBJTIwJTIwJTIwJTIwJTNBcmV0dXJuJTNBJTIwSW5wdXQlMjB0ZXh0JTIwd2l0aCUyMGFueSUyMG1lbnRpb25zJTIwb2YlMjBNaWNoYWVsJTIwU21pdGglMjBzY3J1YmJlZCUwQSUyMCUyMCUyMCUyMCUzQXJ0eXBlJTNBJTIwc3RyJTBBJTIwJTIwJTIwJTIwJTIyJTIyJTIyJTBBJTIwJTIwJTIwJTIwcmV0dXJuJTIwcmUuc3ViKHIlMjJNaWNoYWVsJTIwU21pdGglMjIlMkMlMjAlMjIlNUJSRURBQ1RFRCUyMFBFUlNPTiU1RCUyMiUyQyUyMHRleHQlMkMlMjByZS5JR05PUkVDQVNFKQ==",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}}),L=new J({props:{code:"b3JpZ2luYWxfbWV0aG9kJTIwJTNEJTIwd3JhcHBlci5zZW5kX2NvaGVyZV9yZXF1ZXN0JTBBJTBBJTBBZGVmJTIwbW9kaWZpZWRfbWV0aG9kKHNlbGYlMkMlMjAqKmt3YXJncyklM0ElMEElMjAlMjAlMjAlMjBzZWxmLl92YWxpZGF0ZV9jb2hlcmVfZW5kcG9pbnQoa3dhcmdzLmdldCglMjJlbmRwb2ludCUyMiUyQyUyME5vbmUpKSUyMCUyMCUyMyUyMFVuZm9ydHVuYXRlJTIwZG91YmxlJTIwdmFsaWRhdGUlMjBjb2hlcmUlMjBlbmRwb2ludCUyMGNhbGwlMEElMjAlMjAlMjAlMjBwcm9tcHQlMjAlM0QlMjBrd2FyZ3MuZ2V0KCUyMnByb21wdCUyMiUyQyUyME5vbmUpJTBBJTIwJTIwJTIwJTIwdGV4dCUyMCUzRCUyMG15X2N1c3RvbV9zY3J1YmJlcihwcm9tcHQpJTBBJTIwJTIwJTIwJTIwa3dhcmdzJTVCJTIycHJvbXB0JTIyJTVEJTIwJTNEJTIwdGV4dCUwQSUyMCUyMCUyMCUyMHJldHVybiUyMG9yaWdpbmFsX21ldGhvZCgqKmt3YXJncyklMEElMEElMEElMjMlMjBBc3NpZ24lMjB0aGUlMjBuZXclMjBtZXRob2QlMjB0byUyMHRoZSUyMGluc3RhbmNlJTBBd3JhcHBlci5zZW5kX2NvaGVyZV9yZXF1ZXN0JTIwJTNEJTIwdHlwZXMuTWV0aG9kVHlwZShtb2RpZmllZF9tZXRob2QlMkMlMjB3cmFwcGVyKQ==",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">&quot;endpoint&quot;</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">&quot;prompt&quot;</span>, <span class="hljs-literal">None</span>)
    text = my_custom_scrubber(prompt)
    kwargs[<span class="hljs-string">&quot;prompt&quot;</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}}),Y=new J({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}}),K=new J({props:{code:"cHJpbnQoZGJfcmVjb3JkKQ==",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(db_record)',wrap:!1}}),se=new we({props:{title:"数据集",local:"数据集",headingTag:"h2"}}),ne=new J({props:{code:"cGlpX2RzJTIwJTNEJTIwbG9hZF9kYXRhc2V0KCUyMmFpNHByaXZhY3klMkZwaWktbWFza2luZy0yMDBrJTIyKQ==",highlighted:'pii_ds = load_dataset(<span class="hljs-string">&quot;ai4privacy/pii-masking-200k&quot;</span>)',wrap:!1}}),ae=new J({props:{code:"cGlpX2RzJTVCJTIydHJhaW4lMjIlNUQlNUIzNiU1RCU1QiUyMnNvdXJjZV90ZXh0JTIyJTVE",highlighted:'pii_ds[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">36</span>][<span class="hljs-string">&quot;source_text&quot;</span>]',wrap:!1}}),pe=new J({props:{code:"ZXhhbXBsZSUyMCUzRCUyMHBpaV9kcyU1QiUyMnRyYWluJTIyJTVEJTVCMzYlNUQlNUIlMjJzb3VyY2VfdGV4dCUyMiU1RCUwQSUwQXJlc3BvbnNlJTJDJTIwZGJfcmVjb3JkJTIwJTNEJTIwd3JhcHBlci5zZW5kX2NvaGVyZV9yZXF1ZXN0KCUwQSUyMCUyMCUyMCUyMGVuZHBvaW50JTNEJTIyZ2VuZXJhdGUlMjIlMkMlMEElMjAlMjAlMjAlMjBtb2RlbCUzRCUyMmNvbW1hbmQtci1wbHVzJTIyJTJDJTBBJTIwJTIwJTIwJTIwbWF4X3Rva2VucyUzRDUwJTJDJTBBJTIwJTIwJTIwJTIwcHJvbXB0JTNEZiUyMiU3QmV4YW1wbGUlN0QlNUNuJTVDblN1bW1hcml6ZSUyMHRoZSUyMGFib3ZlJTIwdGV4dCUyMGluJTIwMS0yJTIwc2VudGVuY2VzLiUyMiUyQyUwQSUyMCUyMCUyMCUyMHRlbXBlcmF0dXJlJTNEMC4zJTJDJTBBKSUwQSUwQXByaW50KHJlc3BvbnNlKQ==",highlighted:`<span class="hljs-meta">&gt;&gt;&gt; </span>example = pii_ds[<span class="hljs-string">&quot;train&quot;</span>][<span class="hljs-number">36</span>][<span class="hljs-string">&quot;source_text&quot;</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}}),me=new J({props:{code:"cHJpbnQoZGJfcmVjb3JkKQ==",highlighted:'<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-built_in">print</span>(db_record)',wrap:!1}}),oe=new we({props:{title:"常规输出",local:"常规输出",headingTag:"h2"}}),ce=new J({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}}),ue=new J({props:{code:"cmVzcG9uc2VfdmFuaWxsYQ==",highlighted:"response_vanilla",wrap:!1}}),Je=new ps({props:{source:"https://github.com/huggingface/cookbook/blob/main/notebooks/zh-CN/llm_gateway_pii_detection.md"}}),{c(){h=p("meta"),de=n(),he=p("p"),be=n(),r(w.$$.fragment),fe=n(),r(d.$$.fragment),Ue=n(),b=p("p"),b.innerHTML=jt,je=n(),f=p("p"),f.innerHTML=It,Ie=n(),U=p("p"),U.innerHTML=$t,$e=n(),j=p("p"),j.innerHTML=Ct,Ce=n(),I=p("ol"),I.innerHTML=_t,_e=n(),r($.$$.fragment),Be=n(),y=p("ol"),y.innerHTML=Bt,ge=n(),r(C.$$.fragment),ke=n(),r(_.$$.fragment),ve=n(),r(B.$$.fragment),Ee=n(),g=p("p"),g.innerHTML=gt,Re=n(),r(k.$$.fragment),Ve=n(),r(v.$$.fragment),Ze=n(),r(E.$$.fragment),xe=n(),R=p("pre"),R.textContent=kt,Ne=n(),V=p("p"),V.textContent=vt,Xe=n(),Z=p("p"),Z.innerHTML=Et,Ge=n(),r(x.$$.fragment),Qe=n(),N=p("pre"),N.textContent=Rt,Se=n(),X=p("p"),X.textContent=Vt,He=n(),G=p("p"),G.innerHTML=Zt,We=n(),Q=p("p"),Q.textContent=xt,Ae=n(),S=p("blockquote"),S.innerHTML=Nt,ze=n(),r(H.$$.fragment),Pe=n(),W=p("p"),W.textContent=Xt,qe=n(),r(A.$$.fragment),Fe=n(),z=p("p"),z.textContent=Gt,Le=n(),P=p("p"),P.textContent=Qt,Ye=n(),q=p("blockquote"),q.innerHTML=St,De=n(),r(F.$$.fragment),Ke=n(),r(L.$$.fragment),Oe=n(),r(Y.$$.fragment),et=n(),D=p("pre"),D.textContent=Ht,tt=n(),r(K.$$.fragment),st=n(),O=p("pre"),O.textContent=Wt,lt=n(),ee=p("p"),ee.textContent=At,nt=n(),te=p("p"),te.innerHTML=zt,at=n(),r(se.$$.fragment),pt=n(),le=p("p"),le.textContent=Pt,it=n(),r(ne.$$.fragment),mt=n(),r(ae.$$.fragment),rt=n(),r(pe.$$.fragment),ot=n(),ie=p("pre"),ie.textContent=qt,Mt=n(),r(me.$$.fragment),ct=n(),re=p("pre"),re.textContent=Ft,ut=n(),r(oe.$$.fragment),Tt=n(),Me=p("p"),Me.textContent=Lt,Jt=n(),r(ce.$$.fragment),ht=n(),r(ue.$$.fragment),yt=n(),Te=p("p"),Te.textContent=Yt,wt=n(),r(Je.$$.fragment),dt=n(),ye=p("p"),this.h()},l(e){const t=ls("svelte-u9bgzb",document.head);h=i(t,"META",{name:!0,content:!0}),t.forEach(s),de=a(e),he=i(e,"P",{}),Dt(he).forEach(s),be=a(e),o(w.$$.fragment,e),fe=a(e),o(d.$$.fragment,e),Ue=a(e),b=i(e,"P",{"data-svelte-h":!0}),m(b)!=="svelte-1104mn5"&&(b.innerHTML=jt),je=a(e),f=i(e,"P",{"data-svelte-h":!0}),m(f)!=="svelte-1lync38"&&(f.innerHTML=It),Ie=a(e),U=i(e,"P",{"data-svelte-h":!0}),m(U)!=="svelte-dsfr6t"&&(U.innerHTML=$t),$e=a(e),j=i(e,"P",{"data-svelte-h":!0}),m(j)!=="svelte-1s5i2q6"&&(j.innerHTML=Ct),Ce=a(e),I=i(e,"OL",{"data-svelte-h":!0}),m(I)!=="svelte-14ol4v2"&&(I.innerHTML=_t),_e=a(e),o($.$$.fragment,e),Be=a(e),y=i(e,"OL",{start:!0,"data-svelte-h":!0}),m(y)!=="svelte-148lsng"&&(y.innerHTML=Bt),ge=a(e),o(C.$$.fragment,e),ke=a(e),o(_.$$.fragment,e),ve=a(e),o(B.$$.fragment,e),Ee=a(e),g=i(e,"P",{"data-svelte-h":!0}),m(g)!=="svelte-1rxqakp"&&(g.innerHTML=gt),Re=a(e),o(k.$$.fragment,e),Ve=a(e),o(v.$$.fragment,e),Ze=a(e),o(E.$$.fragment,e),xe=a(e),R=i(e,"PRE",{"data-svelte-h":!0}),m(R)!=="svelte-fgwnhs"&&(R.textContent=kt),Ne=a(e),V=i(e,"P",{"data-svelte-h":!0}),m(V)!=="svelte-18qze46"&&(V.textContent=vt),Xe=a(e),Z=i(e,"P",{"data-svelte-h":!0}),m(Z)!=="svelte-2gbnj8"&&(Z.innerHTML=Et),Ge=a(e),o(x.$$.fragment,e),Qe=a(e),N=i(e,"PRE",{"data-svelte-h":!0}),m(N)!=="svelte-1obiiyg"&&(N.textContent=Rt),Se=a(e),X=i(e,"P",{"data-svelte-h":!0}),m(X)!=="svelte-1buo559"&&(X.textContent=Vt),He=a(e),G=i(e,"P",{"data-svelte-h":!0}),m(G)!=="svelte-1jrlmt8"&&(G.innerHTML=Zt),We=a(e),Q=i(e,"P",{"data-svelte-h":!0}),m(Q)!=="svelte-vwratb"&&(Q.textContent=xt),Ae=a(e),S=i(e,"BLOCKQUOTE",{"data-svelte-h":!0}),m(S)!=="svelte-1toh78c"&&(S.innerHTML=Nt),ze=a(e),o(H.$$.fragment,e),Pe=a(e),W=i(e,"P",{"data-svelte-h":!0}),m(W)!=="svelte-1dm36or"&&(W.textContent=Xt),qe=a(e),o(A.$$.fragment,e),Fe=a(e),z=i(e,"P",{"data-svelte-h":!0}),m(z)!=="svelte-1z0bsxf"&&(z.textContent=Gt),Le=a(e),P=i(e,"P",{"data-svelte-h":!0}),m(P)!=="svelte-1a1t4q8"&&(P.textContent=Qt),Ye=a(e),q=i(e,"BLOCKQUOTE",{"data-svelte-h":!0}),m(q)!=="svelte-17b22ip"&&(q.innerHTML=St),De=a(e),o(F.$$.fragment,e),Ke=a(e),o(L.$$.fragment,e),Oe=a(e),o(Y.$$.fragment,e),et=a(e),D=i(e,"PRE",{"data-svelte-h":!0}),m(D)!=="svelte-1ibz1w3"&&(D.textContent=Ht),tt=a(e),o(K.$$.fragment,e),st=a(e),O=i(e,"PRE",{"data-svelte-h":!0}),m(O)!=="svelte-s3f3d0"&&(O.textContent=Wt),lt=a(e),ee=i(e,"P",{"data-svelte-h":!0}),m(ee)!=="svelte-1lidaje"&&(ee.textContent=At),nt=a(e),te=i(e,"P",{"data-svelte-h":!0}),m(te)!=="svelte-1f7cnzr"&&(te.innerHTML=zt),at=a(e),o(se.$$.fragment,e),pt=a(e),le=i(e,"P",{"data-svelte-h":!0}),m(le)!=="svelte-1060ic6"&&(le.textContent=Pt),it=a(e),o(ne.$$.fragment,e),mt=a(e),o(ae.$$.fragment,e),rt=a(e),o(pe.$$.fragment,e),ot=a(e),ie=i(e,"PRE",{"data-svelte-h":!0}),m(ie)!=="svelte-1ap78y8"&&(ie.textContent=qt),Mt=a(e),o(me.$$.fragment,e),ct=a(e),re=i(e,"PRE",{"data-svelte-h":!0}),m(re)!=="svelte-45v8zv"&&(re.textContent=Ft),ut=a(e),o(oe.$$.fragment,e),Tt=a(e),Me=i(e,"P",{"data-svelte-h":!0}),m(Me)!=="svelte-woqvmo"&&(Me.textContent=Lt),Jt=a(e),o(ce.$$.fragment,e),ht=a(e),o(ue.$$.fragment,e),yt=a(e),Te=i(e,"P",{"data-svelte-h":!0}),m(Te)!=="svelte-1d6s1k6"&&(Te.textContent=Yt),wt=a(e),o(Je.$$.fragment,e),dt=a(e),ye=i(e,"P",{}),Dt(ye).forEach(s),this.h()},h(){ft(h,"name","hf:doc:metadata"),ft(h,"content",ms),ft(y,"start","4")},m(e,t){ns(document.head,h),l(e,de,t),l(e,he,t),l(e,be,t),M(w,e,t),l(e,fe,t),M(d,e,t),l(e,Ue,t),l(e,b,t),l(e,je,t),l(e,f,t),l(e,Ie,t),l(e,U,t),l(e,$e,t),l(e,j,t),l(e,Ce,t),l(e,I,t),l(e,_e,t),M($,e,t),l(e,Be,t),l(e,y,t),l(e,ge,t),M(C,e,t),l(e,ke,t),M(_,e,t),l(e,ve,t),M(B,e,t),l(e,Ee,t),l(e,g,t),l(e,Re,t),M(k,e,t),l(e,Ve,t),M(v,e,t),l(e,Ze,t),M(E,e,t),l(e,xe,t),l(e,R,t),l(e,Ne,t),l(e,V,t),l(e,Xe,t),l(e,Z,t),l(e,Ge,t),M(x,e,t),l(e,Qe,t),l(e,N,t),l(e,Se,t),l(e,X,t),l(e,He,t),l(e,G,t),l(e,We,t),l(e,Q,t),l(e,Ae,t),l(e,S,t),l(e,ze,t),M(H,e,t),l(e,Pe,t),l(e,W,t),l(e,qe,t),M(A,e,t),l(e,Fe,t),l(e,z,t),l(e,Le,t),l(e,P,t),l(e,Ye,t),l(e,q,t),l(e,De,t),M(F,e,t),l(e,Ke,t),M(L,e,t),l(e,Oe,t),M(Y,e,t),l(e,et,t),l(e,D,t),l(e,tt,t),M(K,e,t),l(e,st,t),l(e,O,t),l(e,lt,t),l(e,ee,t),l(e,nt,t),l(e,te,t),l(e,at,t),M(se,e,t),l(e,pt,t),l(e,le,t),l(e,it,t),M(ne,e,t),l(e,mt,t),M(ae,e,t),l(e,rt,t),M(pe,e,t),l(e,ot,t),l(e,ie,t),l(e,Mt,t),M(me,e,t),l(e,ct,t),l(e,re,t),l(e,ut,t),M(oe,e,t),l(e,Tt,t),l(e,Me,t),l(e,Jt,t),M(ce,e,t),l(e,ht,t),M(ue,e,t),l(e,yt,t),l(e,Te,t),l(e,wt,t),M(Je,e,t),l(e,dt,t),l(e,ye,t),bt=!0},p:Ot,i(e){bt||(c(w.$$.fragment,e),c(d.$$.fragment,e),c($.$$.fragment,e),c(C.$$.fragment,e),c(_.$$.fragment,e),c(B.$$.fragment,e),c(k.$$.fragment,e),c(v.$$.fragment,e),c(E.$$.fragment,e),c(x.$$.fragment,e),c(H.$$.fragment,e),c(A.$$.fragment,e),c(F.$$.fragment,e),c(L.$$.fragment,e),c(Y.$$.fragment,e),c(K.$$.fragment,e),c(se.$$.fragment,e),c(ne.$$.fragment,e),c(ae.$$.fragment,e),c(pe.$$.fragment,e),c(me.$$.fragment,e),c(oe.$$.fragment,e),c(ce.$$.fragment,e),c(ue.$$.fragment,e),c(Je.$$.fragment,e),bt=!0)},o(e){u(w.$$.fragment,e),u(d.$$.fragment,e),u($.$$.fragment,e),u(C.$$.fragment,e),u(_.$$.fragment,e),u(B.$$.fragment,e),u(k.$$.fragment,e),u(v.$$.fragment,e),u(E.$$.fragment,e),u(x.$$.fragment,e),u(H.$$.fragment,e),u(A.$$.fragment,e),u(F.$$.fragment,e),u(L.$$.fragment,e),u(Y.$$.fragment,e),u(K.$$.fragment,e),u(se.$$.fragment,e),u(ne.$$.fragment,e),u(ae.$$.fragment,e),u(pe.$$.fragment,e),u(me.$$.fragment,e),u(oe.$$.fragment,e),u(ce.$$.fragment,e),u(ue.$$.fragment,e),u(Je.$$.fragment,e),bt=!1},d(e){e&&(s(de),s(he),s(be),s(fe),s(Ue),s(b),s(je),s(f),s(Ie),s(U),s($e),s(j),s(Ce),s(I),s(_e),s(Be),s(y),s(ge),s(ke),s(ve),s(Ee),s(g),s(Re),s(Ve),s(Ze),s(xe),s(R),s(Ne),s(V),s(Xe),s(Z),s(Ge),s(Qe),s(N),s(Se),s(X),s(He),s(G),s(We),s(Q),s(Ae),s(S),s(ze),s(Pe),s(W),s(qe),s(Fe),s(z),s(Le),s(P),s(Ye),s(q),s(De),s(Ke),s(Oe),s(et),s(D),s(tt),s(st),s(O),s(lt),s(ee),s(nt),s(te),s(at),s(pt),s(le),s(it),s(mt),s(rt),s(ot),s(ie),s(Mt),s(ct),s(re),s(ut),s(Tt),s(Me),s(Jt),s(ht),s(yt),s(Te),s(wt),s(dt),s(ye)),s(h),T(w,e),T(d,e),T($,e),T(C,e),T(_,e),T(B,e),T(k,e),T(v,e),T(E,e),T(x,e),T(H,e),T(A,e),T(F,e),T(L,e),T(Y,e),T(K,e),T(se,e),T(ne,e),T(ae,e),T(pe,e),T(me,e),T(oe,e),T(ce,e),T(ue,e),T(Je,e)}}}const ms='{"title":"个人身份信息（PII）检测的 LLM 网关","local":"个人身份信息pii检测的-llm-网关","sections":[{"title":"LLM 包装器","local":"llm-包装器","sections":[],"depth":2},{"title":"清洗器！","local":"清洗器","sections":[],"depth":2},{"title":"数据集","local":"数据集","sections":[],"depth":2},{"title":"常规输出","local":"常规输出","sections":[],"depth":2}],"depth":1}';function rs(Ut){return es(()=>{new URLSearchParams(window.location.search).get("fw")}),[]}class Ts extends ts{constructor(h){super(),ss(this,h,rs,is,Kt,{})}}export{Ts as component};
