LangChain における個人識別情報の取り扱い
LangChain Blog は、LLM アプリケーションにおける個人識別情報(PII)のリスクと、GDPR などの規制対応のために入力データをマスクする重要性を解説し、具体的な対策オプションを提示している。
キーポイント
PII の定義と LLM 特有の課題
個人識別情報(PII)は GDPR などの規制により厳格に管理される必要があるが、チャットボットではユーザーが自発的に情報を提供するため、明示的な入力フォーム以外でも PII が混入するリスクがある。
LLM プロバイダーのデータ利用リスク
OpenAI や Anthropic などのプロバイダーは、API を通じて送信されたデータをモデル改善やトレーニングに使用する可能性があり、これにより過去の PII が将来のモデル出力として再現されるリスクが生じる。
入力マスクリングの必要性
プライバシーポリシーの変更やデータ漏洩のリスクを回避するため、LLM プロバイダーへ送信する前に入力データを自動的にマスキング(隠蔽)する仕組みの実装が強く推奨されている。
LangSmith との連携における懸念
開発・監視ツールである LangSmith が会話ログを記録する場合、そこに PII が含まれる可能性があり、適切な設定やデータ処理が必要となる点が議論されている。
影響分析・編集コメントを表示
影響分析
この記事は、生成 AI の実用化が進む中で、法的リスクとプライバシー保護の観点から「データ入力前のフィルタリング」が最重要課題の一つであることを浮き彫りにしています。開発者は単に機能を実装するだけでなく、プロバイダーのポリシーや規制要件を考慮したセキュリティ設計(マスキングなど)をデフォルトで組み込むべきという示唆を与えています。
編集コメント
LLM の普及に伴い、プライバシー保護は技術的な工夫だけでなく法的コンプライアンスの観点からも最優先事項となっています。このブログは、開発者がプロバイダーのポリシーを盲信せず、自社のアプリケーション層で PII を防御する具体的な思考プロセスを提供しています。
このブログ記事は、Pampa Labs の創設者である Francisco によって執筆されました。彼は、正確かつコスト効率の高い LLM アプリケーションの開発を企業に支援しています。
イントロダクション
PII は「個人識別情報」の略であり、個人の身元を特定するために使用できる個人データを指します。最近では、GDPR のような規制により、企業が PII データを効果的に処理するソリューションを見つけることがますます重要になっています。
LLM を用いた PII データの管理は難しい場合があります。多くのアプリケーションでは、フォームという形で明示的に要求しているため、PII データを扱っていることを知っています。チャットボットのケースでは、ユーザーが会話中に自発的に PII データ(名前や姓などの自己紹介)を送信することがあり、アプリケーションの種類によっては、この情報が LLM プロバイダーに送信され、品質向上のためにログに残される可能性があります。
以下のような質問がよく寄せられます:
- PII データをマスキングするための利用可能なオプションは何ですか?
- LangSmith についてはどうでしょうか。LangSmith がアプリの会話をログに記録する場合、PII データも同時にログに記録されるのでしょうか?
- OpenAI にデータを送信した場合、PII データが漏洩するリスクがあるのでしょうか?他のプロバイダーの場合はどうでしょうか?
これらの質問に対する決定版の回答を提案するわけではありませんが、今日利用可能ないくつかのオプションを紹介することで、LLM を扱う際に PII データをどのように処理するのが最善の方法かという議論に火をつけることができます。
LLM プロバイダーへの入力マスキングを重視すべき理由
OpenAI、Anthropic、Cohere といった LLM プロバイダーはすべて、API を使用して送信されたデータの扱いについて詳細を定めた独自のプライバシーポリシーを持っています。例えば、一部の企業では、モデルの改善方法を調査するためにそのデータを使用したり、新しいモデルのトレーニングにデータを活用したりすることもあります。LLM は学習した内容を明示的に記憶することがあるため、これは将来のバージョンの LLM が他のユーザーに対して PII データを再現する可能性があることを意味します。
これらのプライバシーポリシーの重要性と、それらが時間とともに変更される可能性を考慮すると、サービスを利用する前にプロバイダーのプライバシーポリシーを確認することを強く推奨します。一例として、OpenAI のプライバシーポリシーはこちらです。
LLM プロバイダー向けのデータサニタイズ
最初の質問は、データをどのように匿名化するかです。LangChain エコシステムには、アプリケーションで使用するためにデータをマスクするのを容易にするツールが統合されています。いくつか見てみましょう:
*Microsoft Presidio*
Microsoft Presidio は、入力テキストにおける PII(個人識別情報)の特定と匿名化を支援するツールです。つまり、テキスト内の PII 要素を一般的な等価物に置き換えることができます。クレジットカード番号、電子メール、電話番号、住所、氏名など、そのようなエンティティを置き換えることができるため、非常に便利です。
Presidio は 2 つのステップで動作します。まず、テキストを分析して PII データを検索します。これはおそらく Presidio の決定的な差別化機能であり、ルールベースロジックと NER(名前付きエンティティ認識)機械学習モデルを組み合わせて、テキスト内の PII データを特定します。2 つ目のステップでは、データを一般的な等価物(例:'PERSON')に置き換えることで匿名化します。LangChain 内で使用する場合は、ライブラリは faker を使用して、テキストを LLM により自然に見せるためにエンティティを偽の値(例:'John Smith')に置き換えます。
Presidio が PII データをどのように特定するかについてさらに詳細を知りたい場合は、こちらで、Presidio が標準で検出するエンティティと、それらを検出するために使用されるメソッドを見つけることができます。
Presidio を LangChain と併用するのは非常に簡単です。匿名化が行われるチェーンに追加ステップを加えるだけで済みます。以下のように実装できます:
anonymizer = PresidioAnonymizer()
template = """Rewrite this text into an official, short email:
{anonymized_text}"""
prompt = PromptTemplate.from_template(template)
llm = ChatOpenAI(temperature=0)
chain = {"anonymized_text": anonymizer.anonymize} | prompt | llm
response = chain.invoke(text)
LangChain のドキュメントでは、Microsoft Presidio の使用方法に関する完全な チュートリアル や、英語以外の言語で Presidio を使用する際の チュートリアル を確認できます。Presidio の利用を検討している場合は、LLM セキュリティ用のツールスイートを含む LLMGuard と併用することも検討してください。これには入力制御とガード(個人識別情報(PII: Personally Identifiable Information)の匿名化、ジャイルブレイク対策)、出力制御(悪意のあるリンク、毒性コンテンツ)が含まれています。
*OpaquePrompts*
データを匿名化するもう一つの優れた選択肢として OpaquePrompts の利用があります。複数の技術を併用するのではなく、OpaquePrompts は 1 つの機械学習(ML: Machine Learning)モデルを用いて個人識別情報(PII)を検出し、適切にマスキングします。
OpaquePrompts を使用することの主な利点の一つは、LangChain との親和性が極めて高いことです。具体的には、OpenAI などの LangChain LLM を初期化引数として受け取る OpaquePrompts LLM クラスを使用するだけで済みます。
chain = LLMChain(
prompt=prompt,
# llm=OpenAI(),
llm=OpaquePrompts(base_llm=OpenAI()),
memory=memory,
)
その他の差別化された側面として、confidential computing(機密コンピューティング)を利用している点が挙げられます。これは、匿名化サービスであっても元のデータにアクセスできないことを意味し、プライバシーを重視するユーザーにとって優れた機能です。最後に、LLM からの応答を取得した後にデータを復号化(deanonymize)するため、ユーザーは自分が言及または要求した元のエンティティを含む回答を受け取ることができます。
LangSmith 用のデータサニタイズ
LangSmith を使用してアプリの会話ログを記録したい場合、別の課題に直面する可能性があります:データをサニタイズされた形式で保存することです。前述のツールを先ほど説明した通りに使用するだけでは、LLM プロバイダーにはサニタイズされた入力のみが渡されますが、LangSmith には元の非サニタイズ入力が保存されてしまいます。これをどう回避すればよいでしょうか?
*オプション #1:入力または出力を保存しない*
LangSmith に PII データを一切保存したくない場合、すべてのクエリに対する入力と出力を LangSmith から隠すことができます。これには、LangSmith が入力/出力のログ記録が必要かどうかを判断するために使用するいくつかの環境変数を使用します:
LANGCHAIN_HIDE_INPUTS=true
LANGCHAIN_HIDE_OUTPUTS=true
このようにして、ログに記録されるものと記録されないものを直接制御することができます。これに関する詳細については、ドキュメントをご参照ください。
この機能を使用する際は、LLM(大規模言語モデル)が回答の中で PII 個人識別情報データポイントに言及する可能性があるため(例:「こんにちはジョン・スミスさん!お会いできて嬉しいです」)、入力と出力の両方を隠蔽することを検討することが重要です。
*オプション#2:保存前にマスク*
LangSmith のデータをアプリケーションのデバッグに使用したい場合や、独自モデルのファインチューニングを検討している場合があるとしましょう。この場合、ユーザーの会話ログがないと、実際のデータを使用してアプリケーションの品質を向上させることが困難になります。
このようなケースにおける別のオプションは、入力に対してマスクを適用し、LLM と追跡ソフトウェアの両方にマスクされた入力を送信することです。これは、チェーンへの入力として送信する前に入力をマスクすることで実現できます。この方法の利点は、追跡機能や後での会話データの利用を可能にしつつ、LLM プロバイダーおよび LangSmith 追跡のためにデータを洗浄(sanitized)した状態に保てる点にあります。
結論
PII データを効果的に扱うことは、安全かつ信頼性の高いデータアプリケーションを構築する上で重要な側面です。本稿ではこの問題に対するいくつかの提案された解決策を紹介しましたが、LangChain エコシステムは常に新しい革新的な代替案を取り入れ続けています。この分野における新展開をいち早く知るためには、LangChain の ニュースレター をぜひご購読ください!
本ブログ記事をお楽しみいただけた場合は、来週にも同テーマに関するウェビナーを開催いたします - こちらからサインアップ してぜひご参加ください!
原文を表示
This blog post was written by Francisco, a founder at Pampa Labs, who assists companies in developing LLM applications that are accurate and cost efficient.
Introduction
PII stands for “personally identifiable information” and it refers to personal data that can be used to uncover an individual’s identity. Lately, regulations such as GDPR make it so that companies find it increasingly important to find solutions that consider handling PII data effectively.
Managing PII data with LLMs can be tricky. In many applications, you know when you are handling PII data because you explicitly ask for it in the shape of a form. In the case of chatbots, the user might spontaneously send PII data within the conversation (like presenting themselves with their name and surname) and depending on the application, this information might be sent to an LLM provider and logged for quality purposes.
There are some questions that commonly arise:
- What are the available options to mask PII data?
- What about LangSmith? If LangSmith logs my apps conversations, will it be logging PII data as well?
- Do we risk PII data being leaked if we send it to OpenAI? What about other providers?
While we don’t propose to have the definitive answers to these questions, by presenting a few options that are available today we can ignite the conversation on which are the best ways to handle PII data when working with LLMs.
Why you should care about masking inputs for LLM providers
LLM providers like OpenAI, Anthropic and Cohere all have their own privacy policies which detail how they use the data sent to them when using their API. For example, some companies might use that data for investigating how to make models better or even use the data to train new models. Since LLMs can sometimes remember explicitly what they have learned in training, this could mean that future versions of their LLMs could potentially reproduce your PII data to other users.
Given the importance of these privacy policies and the fact that they might change over time, we strongly suggest users to take a look at providers’ privacy policies before using their services. As an example, here’s OpenAI’s privacy policy.
Sanitizing data for LLM providers
The first question is: how can we anonymize the data? The LangChain ecosystem is integrated with tools that make it easy to mask the data before using it in the application. Let’s take a look a a few of them:
*Microsoft Presidio*
Microsoft Presidio is a tool that facilitates PII identification and anonymization in input text. In other words, it can replace your text’s PII elements by generic equivalents. It is quite handy as it can replace credit card numbers, emails, phones, addresses and full names among other such entities.
Presidio works in two steps. First, it analyzes the text searching for PII data. This is probably the key differentiating feature in Presidio, since it uses a combination of different methods to identify the PII data in the text, including both rule based logic and NER Machine Learning models. As a second step, it anonymizes the data by replacing it by a a generic equivalent (i.e. ‘<PERSON>’). If using it within LangChain, the library uses faker to replace the entities by a fake value (‘John Smith’) to make the text more natural to the LLM.
If you want more detail on how Presidio identifies PII data, here you can find the entities Presidio detects out of the box, plus the methods used to detect them.
Using Presidio with LangChain is quite straightforward; one needs to add an extra step to the chain where the anonymization takes place, like so:
anonymizer = PresidioAnonymizer()
template = """Rewrite this text into an official, short email:
{anonymized_text}"""
prompt = PromptTemplate.from_template(template)
llm = ChatOpenAI(temperature=0)
chain = {"anonymized_text": anonymizer.anonymize} | prompt | llm
response = chain.invoke(text)In the LangChain documentation we can find a full tutorial on how to use Microsoft Presidio plus a tutorial on how to use Presidio with non-English languages. If planning on using Presidio, you can also consider using it with LLMGuard which contains a suite of tools for LLM security including both input controls and guards (anonymization for PII data, jailbreaks) and output controls (malicious links, toxicity).
*OpaquePrompts*
Another good option to anonymize data is using OpaquePrompts. Instead of using several techniques in unison, OpaquePrompts uses one ML model to detect PII data and mask it appropriately.
One of the main advantages of using OpaquePrompts is just how easy it is to use with LangChain, where we just use an OpaquePrompts LLM class which we initialize with a LangChain LLM like OpenAI.
chain = LLMChain(
prompt=prompt,
# llm=OpenAI(),
llm=OpaquePrompts(base_llm=OpenAI()),
memory=memory,
)Another of its differential aspects is that it usesconfidential computingwhich means that not even their anonymization service can access the original data; a great feature for privacy seeking users. Finally, it will deanonymize the data after getting the response from the LLM so the user will get an answer that contains the original entities that they mentioned / requested.
Sanitizing data for LangSmith
In case we want to use LangSmith to log our app’s conversations, we might face another challenge: saving the data in a sanitized form. If we just use the aforementioned tools in the way described earlier, the LLM provider will receive the input in a sanitized form but we would be saving the original, unsanitized input to LangSmith. How can we avoid this?
*Option #1: do not save inputs or outputs*
If we want to make sure we are not saving any PII data to LangSmith, we can just hide the inputs and outputs of all of our queries from LangSmith. This can be done with a few environmental variables that LangSmith will use to understand whether it needs to log inputs/outputs or not:
LANGCHAIN_HIDE_INPUTS=true
LANGCHAIN_HIDE_OUTPUTS=trueIn this way we can directly control what is logged and what is not. For more information on this, please refer to the docs.
It is important to note that if using this functionality, we should consider hiding both the inputs and the outputs since the LLM might mention PII data points in its answer (‘Hi John Smith! Nice to meet you’).
*Option #2: mask before saving*
Say we want to use the data in LangSmith to debug our application or we were thinking of finetuning a model of our own. In this case, not having the logs of our user’s conversations would hinder us from using real data to improve the quality of our application.
Another option for cases like this is to mask* *the input so that the LLM and the tracing software both receive masked inputs. This can be achieved by masking the input *before* sending it as input to the chain. This method has the advantage of allowing us to trace and later use the conversation data while also keeping the data sanitized for the LLM providers and LangSmith tracing.
Conclusion
Handling PII data effectively is an important aspect of building safe and reliable data applications. We have presented a few proposed solutions for this problem but the LangChain ecosystem is consistently integrating new and innovative alternatives. Stay tuned to the LangChain newsletter to be the first to hear of new developments on this front!
If you enjoyed this blog post, we're also doing a webinar on this subject next week - sign up here to join!
関連記事
今日のまとめ
AI日報で今日の重要ニュースをまとめ読み