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