diff --git a/docs/docs/guides/privacy/presidio_data_anonymization/multi_language.ipynb b/docs/docs/guides/privacy/presidio_data_anonymization/multi_language.ipynb index b6e3100e8d0c6..c7e8d6c87058e 100644 --- a/docs/docs/guides/privacy/presidio_data_anonymization/multi_language.ipynb +++ b/docs/docs/guides/privacy/presidio_data_anonymization/multi_language.ipynb @@ -1,5 +1,15 @@ { "cells": [ + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "---\n", + "sidebar_position: 2\n", + "title: Multi-language anonymization\n", + "---" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -248,15 +258,15 @@ "import langdetect\n", "from langchain.schema import runnable\n", "\n", + "\n", "def detect_language(text: str) -> dict:\n", " language = langdetect.detect(text)\n", " print(language)\n", " return {\"text\": text, \"language\": language}\n", "\n", "\n", - "chain = (\n", - " runnable.RunnableLambda(detect_language)\n", - " | (lambda x: anonymizer.anonymize(x[\"text\"], language=x[\"language\"]))\n", + "chain = runnable.RunnableLambda(detect_language) | (\n", + " lambda x: anonymizer.anonymize(x[\"text\"], language=x[\"language\"])\n", ")" ] }, @@ -345,15 +355,17 @@ "import fasttext\n", "\n", "model = fasttext.load_model(\"lid.176.ftz\")\n", + "\n", + "\n", "def detect_language(text: str) -> dict:\n", - " language = model.predict(text)[0][0].replace('__label__', '')\n", + " language = model.predict(text)[0][0].replace(\"__label__\", \"\")\n", " print(language)\n", " return {\"text\": text, \"language\": language}\n", "\n", - "chain = (\n", - " runnable.RunnableLambda(detect_language)\n", - " | (lambda x: anonymizer.anonymize(x[\"text\"], language=x[\"language\"]))\n", - ")\n" + "\n", + "chain = runnable.RunnableLambda(detect_language) | (\n", + " lambda x: anonymizer.anonymize(x[\"text\"], language=x[\"language\"])\n", + ")" ] }, { diff --git a/docs/docs/guides/privacy/presidio_data_anonymization/qa_privacy_protection.ipynb b/docs/docs/guides/privacy/presidio_data_anonymization/qa_privacy_protection.ipynb new file mode 100644 index 0000000000000..19d58ba9f5c96 --- /dev/null +++ b/docs/docs/guides/privacy/presidio_data_anonymization/qa_privacy_protection.ipynb @@ -0,0 +1,987 @@ +{ + "cells": [ + { + "cell_type": "raw", + "metadata": {}, + "source": [ + "---\n", + "sidebar_position: 3\n", + "title: QA with private data protection\n", + "---" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# QA with private data protection\n", + "\n", + "[![Open In Collab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/langchain-ai/langchain/blob/master/docs/docs/guides/privacy/presidio_data_anonymization/qa_privacy_protection.ipynb)\n", + "\n", + "\n", + "In this notebook, we will look at building a basic system for question answering, based on private data. Before feeding the LLM with this data, we need to protect it so that it doesn't go to an external API (e.g. OpenAI, Anthropic). Then, after receiving the model output, we would like the data to be restored to its original form. Below you can observe an example flow of this QA system:\n", + "\n", + "\n", + "\n", + "\n", + "In the following notebook, we will not go into the details of how the anonymizer works. If you are interested, please visit [this part of the documentation](https://python.langchain.com/docs/guides/privacy/presidio_data_anonymization/).\n", + "\n", + "## Quickstart\n", + "\n", + "### Iterative process of upgrading the anonymizer" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# Install necessary packages\n", + "# !pip install langchain langchain-experimental openai presidio-analyzer presidio-anonymizer spacy Faker faiss-cpu tiktoken\n", + "# ! python -m spacy download en_core_web_lg" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "document_content = \"\"\"Date: October 19, 2021\n", + " Witness: John Doe\n", + " Subject: Testimony Regarding the Loss of Wallet\n", + "\n", + " Testimony Content:\n", + "\n", + " Hello Officer,\n", + "\n", + " My name is John Doe and on October 19, 2021, my wallet was stolen in the vicinity of Kilmarnock during a bike trip. This wallet contains some very important things to me.\n", + "\n", + " Firstly, the wallet contains my credit card with number 4111 1111 1111 1111, which is registered under my name and linked to my bank account, PL61109010140000071219812874.\n", + "\n", + " Additionally, the wallet had a driver's license - DL No: 999000680 issued to my name. It also houses my Social Security Number, 602-76-4532. \n", + "\n", + " What's more, I had my polish identity card there, with the number ABC123456.\n", + "\n", + " I would like this data to be secured and protected in all possible ways. I believe It was stolen at 9:30 AM.\n", + "\n", + " In case any information arises regarding my wallet, please reach out to me on my phone number, 999-888-7777, or through my personal email, johndoe@example.com.\n", + "\n", + " Please consider this information to be highly confidential and respect my privacy. \n", + "\n", + " The bank has been informed about the stolen credit card and necessary actions have been taken from their end. They will be reachable at their official email, support@bankname.com.\n", + " My representative there is Victoria Cherry (her business phone: 987-654-3210).\n", + "\n", + " Thank you for your assistance,\n", + "\n", + " John Doe\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.schema import Document\n", + "\n", + "documents = [Document(page_content=document_content)]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "We only have one document, so before we move on to creating a QA system, let's focus on its content to begin with.\n", + "\n", + "You may observe that the text contains many different PII values, some types occur repeatedly (names, phone numbers, emails), and some specific PIIs are repeated (John Doe)." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [], + "source": [ + "# Util function for coloring the PII markers\n", + "# NOTE: It will not be visible on documentation page, only in the notebook\n", + "import re\n", + "\n", + "\n", + "def print_colored_pii(string):\n", + " colored_string = re.sub(\n", + " r\"(<[^>]*>)\", lambda m: \"\\033[31m\" + m.group(1) + \"\\033[0m\", string\n", + " )\n", + " print(colored_string)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's proceed and try to anonymize the text with the default settings. For now, we don't replace the data with synthetic, we just mark it with markers (e.g. ``), so we set `add_default_faker_operators=False`:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Date: \u001b[31m\u001b[0m\n", + "Witness: \u001b[31m\u001b[0m\n", + "Subject: Testimony Regarding the Loss of Wallet\n", + "\n", + "Testimony Content:\n", + "\n", + "Hello Officer,\n", + "\n", + "My name is \u001b[31m\u001b[0m and on \u001b[31m\u001b[0m, my wallet was stolen in the vicinity of \u001b[31m\u001b[0m during a bike trip. This wallet contains some very important things to me.\n", + "\n", + "Firstly, the wallet contains my credit card with number \u001b[31m\u001b[0m, which is registered under my name and linked to my bank account, \u001b[31m\u001b[0m.\n", + "\n", + "Additionally, the wallet had a driver's license - DL No: \u001b[31m\u001b[0m issued to my name. It also houses my Social Security Number, \u001b[31m\u001b[0m. \n", + "\n", + "What's more, I had my polish identity card there, with the number ABC123456.\n", + "\n", + "I would like this data to be secured and protected in all possible ways. I believe It was stolen at \u001b[31m\u001b[0m.\n", + "\n", + "In case any information arises regarding my wallet, please reach out to me on my phone number, \u001b[31m\u001b[0m, or through my personal email, \u001b[31m\u001b[0m.\n", + "\n", + "Please consider this information to be highly confidential and respect my privacy. \n", + "\n", + "The bank has been informed about the stolen credit card and necessary actions have been taken from their end. They will be reachable at their official email, \u001b[31m\u001b[0m.\n", + "My representative there is \u001b[31m\u001b[0m (her business phone: \u001b[31m\u001b[0m).\n", + "\n", + "Thank you for your assistance,\n", + "\n", + "\u001b[31m\u001b[0m\n" + ] + } + ], + "source": [ + "from langchain_experimental.data_anonymizer import PresidioReversibleAnonymizer\n", + "\n", + "anonymizer = PresidioReversibleAnonymizer(\n", + " add_default_faker_operators=False,\n", + ")\n", + "\n", + "print_colored_pii(anonymizer.anonymize(document_content))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's also look at the mapping between original and anonymized values:" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'CREDIT_CARD': {'': '4111 1111 1111 1111'},\n", + " 'DATE_TIME': {'': 'October 19, 2021', '': '9:30 AM'},\n", + " 'EMAIL_ADDRESS': {'': 'johndoe@example.com',\n", + " '': 'support@bankname.com'},\n", + " 'IBAN_CODE': {'': 'PL61109010140000071219812874'},\n", + " 'LOCATION': {'': 'Kilmarnock'},\n", + " 'PERSON': {'': 'John Doe', '': 'Victoria Cherry'},\n", + " 'PHONE_NUMBER': {'': '999-888-7777'},\n", + " 'UK_NHS': {'': '987-654-3210'},\n", + " 'US_DRIVER_LICENSE': {'': '999000680'},\n", + " 'US_SSN': {'': '602-76-4532'}}\n" + ] + } + ], + "source": [ + "import pprint\n", + "\n", + "pprint.pprint(anonymizer.deanonymizer_mapping)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "In general, the anonymizer works pretty well, but I can observe two things to improve here:\n", + "\n", + "1. Datetime redundancy - we have two different entities recognized as `DATE_TIME`, but they contain different type of information. The first one is a date (*October 19, 2021*), the second one is a time (*9:30 AM*). We can improve this by adding a new recognizer to the anonymizer, which will treat time separately from the date.\n", + "2. Polish ID - polish ID has unique pattern, which is not by default part of anonymizer recognizers. The value *ABC123456* is not anonymized.\n", + "\n", + "The solution is simple: we need to add a new recognizers to the anonymizer. You can read more about it in [presidio documentation](https://microsoft.github.io/presidio/analyzer/adding_recognizers/).\n", + "\n", + "\n", + "Let's add new recognizers:" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [], + "source": [ + "# Define the regex pattern in a Presidio `Pattern` object:\n", + "from presidio_analyzer import Pattern, PatternRecognizer\n", + "\n", + "\n", + "polish_id_pattern = Pattern(\n", + " name=\"polish_id_pattern\",\n", + " regex=\"[A-Z]{3}\\d{6}\",\n", + " score=1,\n", + ")\n", + "time_pattern = Pattern(\n", + " name=\"time_pattern\",\n", + " regex=\"(1[0-2]|0?[1-9]):[0-5][0-9] (AM|PM)\",\n", + " score=1,\n", + ")\n", + "\n", + "# Define the recognizer with one or more patterns\n", + "polish_id_recognizer = PatternRecognizer(\n", + " supported_entity=\"POLISH_ID\", patterns=[polish_id_pattern]\n", + ")\n", + "time_recognizer = PatternRecognizer(supported_entity=\"TIME\", patterns=[time_pattern])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "And now, we're adding recognizers to our anonymizer:" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [], + "source": [ + "anonymizer.add_recognizer(polish_id_recognizer)\n", + "anonymizer.add_recognizer(time_recognizer)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Note that our anonymization instance remembers previously detected and anonymized values, including those that were not detected correctly (e.g., *\"9:30 AM\"* taken as `DATE_TIME`). So it's worth removing this value, or resetting the entire mapping now that our recognizers have been updated:" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [], + "source": [ + "anonymizer.reset_deanonymizer_mapping()" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Let's anonymize the text and see the results:" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Date: \u001b[31m\u001b[0m\n", + "Witness: \u001b[31m\u001b[0m\n", + "Subject: Testimony Regarding the Loss of Wallet\n", + "\n", + "Testimony Content:\n", + "\n", + "Hello Officer,\n", + "\n", + "My name is \u001b[31m\u001b[0m and on \u001b[31m\u001b[0m, my wallet was stolen in the vicinity of \u001b[31m\u001b[0m during a bike trip. This wallet contains some very important things to me.\n", + "\n", + "Firstly, the wallet contains my credit card with number \u001b[31m\u001b[0m, which is registered under my name and linked to my bank account, \u001b[31m\u001b[0m.\n", + "\n", + "Additionally, the wallet had a driver's license - DL No: \u001b[31m\u001b[0m issued to my name. It also houses my Social Security Number, \u001b[31m\u001b[0m. \n", + "\n", + "What's more, I had my polish identity card there, with the number \u001b[31m\u001b[0m.\n", + "\n", + "I would like this data to be secured and protected in all possible ways. I believe It was stolen at \u001b[31m