by Grayson Adkins, updated April 17, 2024
This notebook demonstrates how to evaluate user prompts and LLM responses for personally identifiable infromation (PII) such as contact information, financial or banking info, digital identifiers, job related data, or other sensitive personal information.
We use the bigcode/starpii
PII detection model available on Hugging Face plus LangChain for crafting prompt templates and TruLens for running evaluation and visualizing results.
This notebook builds on examples provided by TruLens.
Both TruLens and LangChain are new frameworks with rapidly changing interfaces. I found several deprecated or broken features that I had to resolve while working on this notebook. Be advised that you may similarly find issues with the code here, due to those dependencies.
!pip install -qU trulens_eval langchain
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
!pip install -qU langchain_openai
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 309.7/309.7 kB 9.1 MB/s eta 0:00:00 WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
import os
from dotenv import load_dotenv,find_dotenv
# # Load OPENAI_API_KEY from local .env file
# load_dotenv(find_dotenv())
# Or set it like this
os.environ["OPENAI_API_KEY"] = "sk-..."
## Print key to check
# print(os.environ["OPENAI_API_KEY"])
from trulens_eval import Feedback
from trulens_eval import OpenAI as trulens_provider_openai
from trulens_eval import Tru
tru = Tru()
tru.reset_database()
# Imports from langchain to build app. You may need to install langchain first
# with the following:
# ! pip install langchain>=0.0.170
from langchain.chains import LLMChain
from langchain_openai import OpenAI
from langchain.prompts import PromptTemplate
from langchain.prompts.chat import HumanMessagePromptTemplate, ChatPromptTemplate
full_prompt = HumanMessagePromptTemplate(
prompt=PromptTemplate(
template=
"Provide a helpful response with relevant background information for the following: {prompt}",
input_variables=["prompt"],
)
)
chat_prompt_template = ChatPromptTemplate.from_messages([full_prompt])
llm = OpenAI(temperature=0.9, max_tokens=128)
chain = LLMChain(llm=llm, prompt=chat_prompt_template, verbose=True)
prompt_input = 'Sam Altman is the CEO at OpenAI, and uses the password: password1234 .'
This feedback function includes chain of thought reasoning.
from trulens_eval.feedback.provider.hugs import Huggingface
# Hugging Face based feedback function collection class
hf_provider = Huggingface()
# Define a pii_detection feedback function using HuggingFace.
# By default this will check language match on the main app input
f_pii_detection = Feedback(hf_provider.pii_detection_with_cot_reasons).on_input()
tru_recorder = TruChain(chain,
app_id='Chain1_ChatApplication',
feedbacks=[f_pii_detection])
with tru_recorder as recording:
llm_response = chain(prompt_input)
display(llm_response)
records, feedback = tru.get_records_and_feedback(app_ids=[])
# Make it a little easier to read
import pandas as pd
pd.set_option("display.max_colwidth", None)
records[["input", "output"] + feedback]
tru.run_dashboard() # open a local streamlit app to explore
# tru.stop_dashboard() # stop if needed