You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openai-cookbook/examples/Developing_hallucination_gu...

1099 lines
54 KiB
Plaintext

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Developing Hallucination Guardrails\n",
"\n",
"A guardrail is a set of rules and checks designed to ensure that the outputs of an LLM are accurate, appropriate, and aligned with user expectations. For more additional information on developing guardrails, you can refer to this [guide on developing guardrails](https://cookbook.openai.com/examples/how_to_use_guardrails).\n",
"\n",
"In this notebook, we'll walk through the process of developing an output guardrail that specifically checks model outputs for hallucinations. \n",
"\n",
"This notebook will focus on:\n",
"1. Building out a strong eval set\n",
"2. Identifying specific criteria to measure hallucinations\n",
"3. Improving the accuracy of our guardrail with few-shot prompting\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-20T21:05:07.490991Z",
"start_time": "2024-05-20T21:05:07.475169Z"
}
},
"outputs": [],
"source": [
"from concurrent.futures import ThreadPoolExecutor\n",
"from IPython.display import display, HTML\n",
"import json\n",
"import pandas as pd\n",
"from sklearn.metrics import precision_score, recall_score\n",
"from typing import List\n",
"from openai import OpenAI\n",
"\n",
"client = OpenAI()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Function to set up display options for pandas\n",
"def setup_pandas_display():\n",
" # Increase display limits\n",
" pd.set_option('display.max_rows', 500)\n",
" pd.set_option('display.max_columns', 500)\n",
"\n",
"# Function to make DataFrame scrollable in the notebook output\n",
"def make_scrollable(df):\n",
" style = (\n",
" '<style>'\n",
" 'div.output_scroll {'\n",
" 'resize: both;'\n",
" 'overflow: auto;'\n",
" '}'\n",
" '</style>'\n",
" )\n",
" html = f\"{style}{df.to_html()}\"\n",
" display(HTML(html))\n",
"\n",
"# Main function to display DataFrame\n",
"def display_dataframe(df):\n",
" setup_pandas_display() # Enable scrollable view\n",
" make_scrollable(df)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1. Building out an eval set\n",
"\n",
"Imagine we are a customer support team that is building out an automated support agent. We will be feeding the assistant information from our knowledge base about a specific set of policies for how to handle tickets such as returns, refunds, feedback, and expect the model to follow the policy when interacting with customers.\n",
"\n",
"The first thing we will do is use GPT-4o to build out a set of policies that we will want to follow.\n",
"\n",
"If you want to do deep dive into generating synthetic data, you can review our Synthetic Data Generation Cookbook [here](https://cookbook.openai.com/examples/sdg1)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-20T21:05:08.750241Z",
"start_time": "2024-05-20T21:05:08.748425Z"
}
},
"outputs": [],
"source": [
"system_input_prompt = \"\"\"\n",
"You are a helpful assistant that can generate policies for a support agent at a fictional company to follow. You will be provided with a topic (ie. returns, refunds, feedback) and you are to generate a sample policy for how to handle the it.\n",
"\n",
"When constructing the policy, it should contain step-by-step instructions for how to handle the customer inquiry. It should include decision logic for what to do if a customer falls under a certain category, and provide requirements for taking specific actions.\n",
"\"\"\"\n",
"\n",
"user_policy_example_1 = \"\"\"\"\n",
"RETURN POLICY\n",
"\"\"\"\n",
"\n",
"assistant_policy_example_1 = \"\"\"\n",
"RETURN POLICY\n",
"\n",
"1. ** Ask the customer why they want the order replaced **\n",
" - Categorize their issue into one of the following buckets:\n",
" - damaged: They received the product in a damaged state\n",
" - satisfaction: The customer is not satisfied with the item and does not like the product.\n",
" - unnecessary: They no longer need the item\n",
"2a. **If return category is 'damaged'\n",
" - Ask customer for a picture of the damaged item\n",
" - If the item is indeed damaged, continue to step 3\n",
" - If the item is not damaged, notify the customer that this does not meet our requirements for return and they are not eligible for a refund\n",
" - Skip step 3 and go straight to step 4\n",
"\n",
"2b. **If return category is either 'satisfaction' or 'unnecessary'**\n",
" - Ask the customer if they can provide feedback on the quality of the item\n",
" - If the order was made within 30 days, notify them that they are eligible for a full refund\n",
" - If the order was made within 31-60 days, notify them that they are eligible for a partial refund of 50%\n",
" - If the order was made greater than 60 days ago, notify them that they are not eligible for a refund\n",
"\n",
"3. **If the customer is eligible for a return or refund**\n",
" - Ask the customer to confirm that they would like a return or refund\n",
" - Once they confirm, process their request\n",
"\n",
"4 **Provide additional support before closing out ticket**\n",
" - Ask the customer if there is anything else you can do to help them today.\n",
"\n",
"\"\"\"\n",
"\n",
"user_policy_input = \"\"\"\n",
"{{POLICY}}\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-20T21:05:57.764869Z",
"start_time": "2024-05-20T21:05:09.939704Z"
}
},
"outputs": [],
"source": [
"def generate_policy(policy: str) -> str:\n",
" input_message = user_policy_input.replace(\"{{POLICY}}\", policy)\n",
" \n",
" response = client.chat.completions.create(\n",
" messages= [\n",
" {\"role\": \"system\", \"content\": system_input_prompt},\n",
" {\"role\": \"user\", \"content\": user_policy_example_1},\n",
" {\"role\": \"assistant\", \"content\": assistant_policy_example_1},\n",
" {\"role\": \"user\", \"content\": input_message},\n",
" ],\n",
" model=\"gpt-4o\"\n",
" )\n",
" \n",
" return response.choices[0].message.content\n",
"\n",
"def generate_policies() -> List[str]:\n",
" # List of different types of policies to generate \n",
" policies = ['PRODUCT FEEDBACK POLICY', 'SHIPPING POLICY', 'WARRANTY POLICY', 'ACCOUNT DELETION', 'COMPLAINT RESOLUTION']\n",
" \n",
" with ThreadPoolExecutor() as executor:\n",
" policy_instructions_list = list(executor.map(generate_policy, policies))\n",
" \n",
" return policy_instructions_list\n",
"\n",
"policy_instructions = generate_policies()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Next we'll take these policies and generate sample customer interactions that do or do not follow the instructions."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-20T21:06:30.811764Z",
"start_time": "2024-05-20T21:06:30.808903Z"
}
},
"outputs": [],
"source": [
"system_input_prompt = \"\"\"\"\n",
"You are a helpful assistant that can generate fictional interactions between a support assistant and a customer user. You will be given a set of policy instructions that the support agent is instructed to follow.\n",
"\n",
"Based on the instructions, you must generate a relevant single-turn or multi-turn interaction between the assistant and the user. It should average between 1-3 turns total.\n",
"\n",
"For a given set of instructions, generate an example conversation that where the assistant either does or does not follow the instructions properly. In the assistant's responses, have it give a combination of single sentence and multi-sentence responses.\n",
"\n",
"The output must be in a json format with the following three parameters:\n",
" - accurate: \n",
" - This should be a boolean True or False value that matches whether or not the final assistant message accurately follows the policy instructions\n",
" - kb_article:\n",
" - This should be the entire policy instruction that is passed in from the user\n",
" - chat_history: \n",
" - This should contain the entire conversation history except for the final assistant message. \n",
" - This should be in a format of an array of jsons where each json contains two parameters: role, and content. \n",
" - Role should be set to either 'user' to represent the customer, or 'assistant' to represent the customer support assistant. \n",
" - Content should contain the message from the appropriate role.\n",
" - The final message in the chat history should always come from the user. The assistant response in the following parameter will be a response to this use message.\n",
" - assistant_response: \n",
" - This should contain the final response from the assistant. This is what we will evaluate to determine whether or not it is accurately following the policy.\n",
"\"\"\"\n",
"\n",
"user_example_1 = \"\"\"\"\n",
"Here are the policy instructions:\n",
"RETURN POLICY\n",
"\n",
"1. ** Ask the customer why they want the order replaced **\n",
" - Categorize their issue into one of the following buckets:\n",
" - damaged: They received the product in a damaged state\n",
" - satisfaction: The customer is not satisfied with the item and does not like the product.\n",
" - unnecessary: They no longer need the item\n",
"2a. **If return category is 'damaged'\n",
" - Ask customer for a picture of the damaged item\n",
" - If the item is indeed damaged, continue to step 3\n",
" - If the item is not damaged, notify the customer that this does not meet our requirements for return and they are not eligible for a refund\n",
" - Skip step 3 and go straight to step 4\n",
"\n",
"2b. **If return category is either 'satisfaction' or 'unnecessary'**\n",
" - Ask the customer if they can provide feedback on the quality of the item\n",
" - If the order was made within 30 days, notify them that they are eligible for a full refund\n",
" - If the order was made within 31-60 days, notify them that they are eligible for a partial refund of 50%\n",
" - If the order was made greater than 60 days ago, notify them that they are not eligible for a refund\n",
"\n",
"3. **If the customer is eligible for a return or refund**\n",
" - Ask the customer to confirm that they would like a return or refund\n",
" - Once they confirm, process their request\n",
"\n",
"4 **Provide additional support before closing out ticket**\n",
" - Ask the customer if there is anything else you can do to help them today.\n",
"\"\"\"\n",
"\n",
"assistant_example_1 = \"\"\"\n",
"{\n",
" \"accurate\": \"true\",\n",
" \"kb_article\": \"1. ** Ask the customer why they want the order replaced ** - Categorize their issue into one of the following buckets: - damaged: They received the product in a damaged state - satisfaction: The customer is not satisfied with the item and does not like the product. - unnecessary: They no longer need the item 2a. **If return category is 'damaged' - Ask customer for a picture of the damaged item - If the item is indeed damaged, continue to step 3 - If the item is not damaged, notify the customer that this does not meet our requirements for return and they are not eligible for a refund - Skip step 3 and go straight to step 4 2b. **If return category is either 'satisfaction' or 'unnecessary'** - Ask the customer if they can provide feedback on the quality of the item - If the order was made within 30 days, notify them that they are eligible for a full refund - If the order was made within 31-60 days, notify them that they are eligible for a partial refund of 50% - If the order was made greater than 60 days ago, notify them that they are not eligible for a refund 3. **If the customer is eligible for a return or refund** - Ask the customer to confirm that they would like a return or refund - Once they confirm, process their request 4 **Provide additional support before closing out ticket** - Ask the customer if there is anything else you can do to help them today.\",\n",
" \"chat_history\": [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"I would like to return this shirt\"\n",
" },\n",
" {\n",
" \"role\": \"assistant\",\n",
" \"content\": \"Hi there, I'm happy to help with processing this return. Can you please provide an explanation for why you'd like to return this shirt?\"\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"Yes, I am not satisfied with the design\"\n",
" }\n",
" ],\n",
" \"assistant_response\": {\n",
" \"role\": \"assistant\",\n",
" \"content\": \"I see. Because the shirt was ordered in the last 30 days, we can provide you with a full refund. Would you like me to process the refund?\"\n",
" }\n",
"}\n",
"\"\"\"\n",
"\n",
"user_example_2 = \"\"\"\"\n",
"Here are the policy instructions:\n",
"RETURN POLICY\n",
"\n",
"1. ** Ask the customer why they want the order replaced **\n",
" - Categorize their issue into one of the following buckets:\n",
" - damaged: They received the product in a damaged state\n",
" - satisfaction: The customer is not satisfied with the item and does not like the product.\n",
" - unnecessary: They no longer need the item\n",
"2a. **If return category is 'damaged'\n",
" - Ask customer for a picture of the damaged item\n",
" - If the item is indeed damaged, continue to step 3\n",
" - If the item is not damaged, notify the customer that this does not meet our requirements for return and they are not eligible for a refund\n",
" - Skip step 3 and go straight to step 4\n",
"\n",
"2b. **If return category is either 'satisfaction' or 'unnecessary'**\n",
" - Ask the customer if they can provide feedback on the quality of the item\n",
" - If the order was made within 30 days, notify them that they are eligible for a full refund\n",
" - If the order was made within 31-60 days, notify them that they are eligible for a partial refund of 50%\n",
" - If the order was made greater than 60 days ago, notify them that they are not eligible for a refund\n",
"\n",
"3. **If the customer is eligible for a return or refund**\n",
" - Ask the customer to confirm that they would like a return or refund\n",
" - Once they confirm, process their request\n",
"\n",
"4 **Provide additional support before closing out ticket**\n",
" - Ask the customer if there is anything else you can do to help them today.\n",
"\"\"\"\n",
"\n",
"assistant_example_2 = \"\"\"\n",
"{\n",
" \"accurate\": \"false\",\n",
" \"kb_article\": \"1. ** Ask the customer why they want the order replaced ** - Categorize their issue into one of the following buckets: - damaged: They received the product in a damaged state - satisfaction: The customer is not satisfied with the item and does not like the product. - unnecessary: They no longer need the item 2a. **If return category is 'damaged' - Ask customer for a picture of the damaged item - If the item is indeed damaged, continue to step 3 - If the item is not damaged, notify the customer that this does not meet our requirements for return and they are not eligible for a refund - Skip step 3 and go straight to step 4 2b. **If return category is either 'satisfaction' or 'unnecessary'** - Ask the customer if they can provide feedback on the quality of the item - If the order was made within 30 days, notify them that they are eligible for a full refund - If the order was made within 31-60 days, notify them that they are eligible for a partial refund of 50% - If the order was made greater than 60 days ago, notify them that they are not eligible for a refund 3. **If the customer is eligible for a return or refund** - Ask the customer to confirm that they would like a return or refund - Once they confirm, process their request 4 **Provide additional support before closing out ticket** - Ask the customer if there is anything else you can do to help them today.\",\n",
" \"chat_history\": [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"I would like to return this shirt\"\n",
" },\n",
" {\n",
" \"role\": \"assistant\",\n",
" \"content\": \"Hi there, I'm happy to help with processing this return. Can you please provide an explanation for why you'd like to return this shirt?\"\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content\": \"Yes, I am not satisfied with the design\"\n",
" }\n",
" ],\n",
" \"assistant_response\": {\n",
" \"role\": \"assistant\",\n",
" \"content\": \"I see. Because the shirt was ordered in the last 60 days, we cannot process a refund.\" \n",
" }\n",
"}\n",
"\"\"\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now let's iterate through the policies and generate some examples."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-20T21:07:06.972776Z",
"start_time": "2024-05-20T21:06:37.958418Z"
}
},
"outputs": [],
"source": [
"customer_interactions = []\n",
"\n",
"def fetch_response(policy):\n",
" messages = [\n",
" { \"role\": \"system\", \"content\": system_input_prompt},\n",
" { \"role\": \"user\", \"content\": user_example_1},\n",
" { \"role\": \"assistant\", \"content\": assistant_example_1},\n",
" { \"role\": \"user\", \"content\": user_example_2},\n",
" { \"role\": \"assistant\", \"content\": assistant_example_2},\n",
" { \"role\": \"user\", \"content\": policy}\n",
" ]\n",
"\n",
" response = client.chat.completions.create(\n",
" model=\"gpt-4o\",\n",
" messages=messages,\n",
" temperature=0.7,\n",
" n=10\n",
" )\n",
" return response.choices\n",
"\n",
"with ThreadPoolExecutor() as executor:\n",
" futures = [executor.submit(fetch_response, policy) for policy in policy_instructions]\n",
" for future in futures:\n",
" choices = future.result()\n",
" customer_interactions.extend([choice.message.content for choice in choices])\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<style>div.output_scroll {resize: both;overflow: auto;}</style><table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>accurate</th>\n",
" <th>kb_article</th>\n",
" <th>chat_history</th>\n",
" <th>assistant_response</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>true</td>\n",
" <td>PRODUCT FEEDBACK POLICY 1. **Acknowledge Reception** - Thank the customer for taking the time to provide feedback. - Use a personalized greeting: \"Thank you for your feedback, [Customer Name]. We appreciate your input.\" 2. **Categorize Feedback** - Determine the type of feedback: - **Positive Feedback** - **Negative Feedback** - **Suggestions for Improvement** - Document the feedback under the appropriate category in the internal database. 3. **Responding to Positive Feedback** - Express gratitude: \"We're thrilled to hear that you enjoyed our product. Thank you for letting us know!\" - If possible, offer a small token of appreciation (e.g., discount or voucher for future purchases). 4. **Responding to Negative Feedback** - Apologize sincerely and acknowledge the customer's concerns: \"We apologize that our product did not meet your expectations. Your feedback is important to us.\" - Ask for additional details if necessary to understand the issue better. - Reassure the customer that their feedback will be escalated to the product development team. 5. **Responding to Suggestions** - Acknowledge the suggestion: \"Thank you for your suggestion. We value input from our customers as it helps us improve our products.\" - Inform the customer that their suggestion will be reviewed: \"We will share your idea with our product team for further consideration.\" 6. **Internal Processing** - Log all feedback under the respective category in the internal database. - Forward detailed feedback to the product development team bi-weekly. - High-priority issues should be escalated immediately to the senior management team. 7. **Follow-Up** - Monitor whether the customer's feedback leads to any product updates or changes. - If the customers feedback resulted in product enhancement, send a follow-up email to inform them: \"Thank you for your valuable feedback. We wanted to let you know that we've made some improvements based on your input.\" 8. **Closing the Loop** - Ask if there is anything else you can assist the customer with: \"Is there anything else we can help you with today?\" - Close the ticket once all queries and feedback are appropriately addressed. 9. **Continuous Improvement** - Analyze feedback trends monthly to identify recurring issues and areas for improvement. - Use feedback insights for product development meetings and strategic planning sessions. By following these steps, we ensure that customer feedback is valued, documented, and acted upon to continuously improve our product offerings.</td>\n",
" <td>[{'role': 'user', 'content': 'I wanted to let you know that the new app update is fantastic! The interface is so much smoother now.'}]</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you for your feedback! We appreciate your input. We're thrilled to hear that you enjoyed our product. Thank you for letting us know! As a token of our appreciation, we're offering you a 10% discount on your next purchase. Is there anything else we can help you with today?'}</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"interaction_dict = json.loads(customer_interactions[0])\n",
"\n",
"df_interaction = pd.DataFrame([interaction_dict])\n",
"\n",
"# Pretty print the DataFrame\n",
"display_dataframe(df_interaction)\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-20T21:10:44.913077Z",
"start_time": "2024-05-20T21:10:44.907165Z"
}
},
"outputs": [],
"source": [
"# Decode the JSON strings\n",
"data = [json.loads(entry) for entry in customer_interactions]\n",
"\n",
"# Create a DataFrame from the cleaned data\n",
"df = pd.DataFrame(data)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-20T21:10:47.001453Z",
"start_time": "2024-05-20T21:10:46.980790Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>accurate</th>\n",
" <th>kb_article</th>\n",
" <th>chat_history</th>\n",
" <th>assistant_response</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>true</td>\n",
" <td>PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep...</td>\n",
" <td>[{'role': 'user', 'content': 'I wanted to let ...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>true</td>\n",
" <td>PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep...</td>\n",
" <td>[{'role': 'user', 'content': 'I wanted to let ...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>true</td>\n",
" <td>PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep...</td>\n",
" <td>[{'role': 'user', 'content': 'I wanted to give...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>true</td>\n",
" <td>PRODUCT FEEDBACK POLICY\\n\\n1. **Acknowledge Re...</td>\n",
" <td>[{'role': 'user', 'content': 'I really enjoyed...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>true</td>\n",
" <td>PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep...</td>\n",
" <td>[{'role': 'user', 'content': 'I wanted to give...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>true</td>\n",
" <td>PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep...</td>\n",
" <td>[{'role': 'user', 'content': 'I wanted to let ...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>true</td>\n",
" <td>PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep...</td>\n",
" <td>[{'role': 'user', 'content': 'I didn't like th...</td>\n",
" <td>{'role': 'assistant', 'content': 'We apologize...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>true</td>\n",
" <td>PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep...</td>\n",
" <td>[{'role': 'user', 'content': 'I have some feed...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>true</td>\n",
" <td>PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep...</td>\n",
" <td>[{'role': 'user', 'content': 'I really love th...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>true</td>\n",
" <td>1. **Acknowledge Reception** - Thank the custo...</td>\n",
" <td>[{'role': 'user', 'content': 'I wanted to say ...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" accurate kb_article \\\n",
"0 true PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep... \n",
"1 true PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep... \n",
"2 true PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep... \n",
"3 true PRODUCT FEEDBACK POLICY\\n\\n1. **Acknowledge Re... \n",
"4 true PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep... \n",
"5 true PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep... \n",
"6 true PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep... \n",
"7 true PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep... \n",
"8 true PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep... \n",
"9 true 1. **Acknowledge Reception** - Thank the custo... \n",
"\n",
" chat_history \\\n",
"0 [{'role': 'user', 'content': 'I wanted to let ... \n",
"1 [{'role': 'user', 'content': 'I wanted to let ... \n",
"2 [{'role': 'user', 'content': 'I wanted to give... \n",
"3 [{'role': 'user', 'content': 'I really enjoyed... \n",
"4 [{'role': 'user', 'content': 'I wanted to give... \n",
"5 [{'role': 'user', 'content': 'I wanted to let ... \n",
"6 [{'role': 'user', 'content': 'I didn't like th... \n",
"7 [{'role': 'user', 'content': 'I have some feed... \n",
"8 [{'role': 'user', 'content': 'I really love th... \n",
"9 [{'role': 'user', 'content': 'I wanted to say ... \n",
"\n",
" assistant_response \n",
"0 {'role': 'assistant', 'content': 'Thank you fo... \n",
"1 {'role': 'assistant', 'content': 'Thank you fo... \n",
"2 {'role': 'assistant', 'content': 'Thank you fo... \n",
"3 {'role': 'assistant', 'content': 'Thank you fo... \n",
"4 {'role': 'assistant', 'content': 'Thank you fo... \n",
"5 {'role': 'assistant', 'content': 'Thank you fo... \n",
"6 {'role': 'assistant', 'content': 'We apologize... \n",
"7 {'role': 'assistant', 'content': 'Thank you fo... \n",
"8 {'role': 'assistant', 'content': 'Thank you fo... \n",
"9 {'role': 'assistant', 'content': 'Thank you fo... "
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"df.head(10)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Constructing our hallucination guardrail\n",
"\n",
"When building out our hallucination guardrail, here are some guiding principles:\n",
"\n",
"1. Provide very descriptive metrics to evaluate whether a response is accurate\n",
"- It is important to break down this idea of \"truth\" in easily identifiable metrics that we can measure\n",
"- Metrics like truthfulness and relevance are difficult to measure. Giving concrete ways to score the statement can result in a more accurate guardrail\n",
"2. Ensure consistency across key terminology\n",
"- It is important to keep relevant terms such as knowledge base articles, assistants, and users consistent across the prompt\n",
"- If we begin to use phrases such as assistant vs agent, the model could get confused\n",
"3. Start with the most advanced model\n",
"- There is a cost vs quality trade-off when using the most advanced models. Although GPT-4o may be more expensive, it is important to start with the most advanced model so we can ensure a high degree of accuracy\n",
"- Once we have thoroughly tested out the guardrail and are confident in its performance, we can look to reducing cost by tuning it down to gpt-3.5-turbo\n",
"4. Evaluate each sentence independently and the entire response as a whole\n",
"- If the agent returns a long response, it can be useful to break down the response to individual sentences and evaluate them independently\n",
"- In addition to that, evaluating the whole intent of the message as a whole can ensure that you don't lose important context\n",
"\n",
"With all of this in mind, let's build out a guardrail system and measure its performance."
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-20T21:10:58.610415Z",
"start_time": "2024-05-20T21:10:58.605619Z"
}
},
"outputs": [],
"source": [
"guardrail_system_message = \"\"\"You are a highly specialized assistant tasked with reviewing chatbot responses to identify and flag any inaccuracies or hallucinations. For each user message, you must thoroughly analyze the response by considering:\n",
" 1. Knowledge Accuracy: Does the message accurately reflect information found in the knowledge base? Assess not only direct mentions but also contextually inferred knowledge.\n",
" 2. Relevance: Does the message directly address the user's question or statement? Check if the response logically follows the users last message, maintaining coherence in the conversation thread.\n",
" 3. Policy Compliance: Does the message adhere to company policies? Evaluate for subtleties such as misinformation, overpromises, or logical inconsistencies. Ensure the response is polite, non-discriminatory, and practical.\n",
"\n",
"To perform your task you will be given the following:\n",
" 1. Knowledge Base Articles - These are your source of truth for verifying the content of assistant messages.\n",
" 2. Chat Transcript - Provides context for the conversation between the user and the assistant.\n",
" 3. Assistant Message - The message from the assistant that needs review.\n",
"\n",
"For each sentence in the assistant's most recent response, assign a score based on the following criteria:\n",
" 1. Factual Accuracy:\n",
" - Score 1 if the sentence is factually correct and corroborated by the knowledge base.\n",
" - Score 0 if the sentence contains factual errors or unsubstantiated claims.\n",
" 2. Relevance:\n",
" - Score 1 if the sentence directly and specifically addresses the user's question or statement without digression.\n",
" - Score 0 if the sentence is tangential or does not build logically on the conversation thread.\n",
" 3. Policy Compliance:\n",
" - Score 1 if the response complies with all company policies including accuracy, ethical guidelines, and user engagement standards.\n",
" - Score 0 if it violates any aspect of the policies, such as misinformation or inappropriate content.\n",
" 4. Contextual Coherence:\n",
" - Score 1 if the sentence maintains or enhances the coherence of the conversation, connecting logically with preceding messages.\n",
" - Score 0 if it disrupts the flow or context of the conversation.\n",
"\n",
"Include in your response an array of JSON objects for each evaluated sentence. Each JSON object should contain:\n",
" - `sentence`: Text of the evaluated sentence.\n",
" - `factualAccuracy`: Score for factual correctness (0 or 1).\n",
" - `factualReference`: If scored 1, cite the exact line(s) from the knowledge base. If scored 0, provide a rationale.\n",
" - `relevance`: Score for relevance to the users question (0 or 1).\n",
" - `policyCompliance`: Score for adherence to company policies (0 or 1).\n",
" - `contextualCoherence`: Score for maintaining conversation coherence (0 or 1).\n",
"\n",
"ALWAYS RETURN YOUR RESPONSE AS AN ARRAY OF JSONS.\n",
"\"\"\"\n",
"\n",
"fs_user_1 = \"\"\"\n",
"\n",
"## Knowledge Base Articles: \n",
"1. ** Ask the customer why they want the order replaced **\n",
" - Categorize their issue into one of the following buckets:\n",
" - damaged: They received the product in a damaged state\n",
" - satisfaction: The customer is not satisfied with the item and does not like the product.\n",
" - unnecessary: They no longer need the item\n",
"2a. **If return category is 'damaged'\n",
" - Ask customer for a picture of the damaged item\n",
" - If the item is indeed damaged, continue to step 3\n",
" - If the item is not damaged, notify the customer that this does not meet our requirements for return and they are not eligible for a refund\n",
" - Skip step 3 and go straight to step 4\n",
"\n",
"2b. **If return category is either 'satisfaction' or 'unnecessary'**\n",
" - Ask the customer if they can provide feedback on the quality of the item\n",
" - If the order was made within 30 days, notify them that they are eligible for a full refund\n",
" - If the order was made within 31-60 days, notify them that they are eligible for a partial refund of 50%\n",
" - If the order was made greater than 60 days ago, notify them that they are not eligible for a refund\n",
"\n",
"3. **If the customer is eligible for a return or refund**\n",
" - Ask the customer to confirm that they would like a return or refund\n",
" - Once they confirm, process their request\n",
"\n",
"4 **Provide additional support before closing out ticket**\n",
" - Ask the customer if there is anything else you can do to help them today.\n",
" \n",
"## Chat Transcript:\n",
" [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content: \"I would like to return this shirt\"\n",
" },\n",
" {\n",
" \"role\": \"assistant\",\n",
" \"content\": \"Hi there, I'm happy to help with processing this return. Can you please provide an explanation for why you'd like to return this shirt?\"\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content: \"Yes, I am not satisfied with the design\"\n",
" }\n",
" ]\n",
"\n",
"## Assistant Message:\n",
"I see, because the shirt was ordered in the last 30 days, we can provide you with a full refund. Would you like me to process the refund?\n",
"\"\"\"\n",
"\n",
"fs_assistant_1 = \"\"\"[\n",
" {\n",
" \"sentence\": \"I see, because the shirt was ordered in the last 30 days, we can provide you with a full refund.\",\n",
" \"factualAccuracy\": 1,\n",
" \"factualReference\": \"If the order was made within 30 days, notify them that they are eligible for a full refund\",\n",
" \"relevance\": 1,\n",
" \"policyCompliance\": 1,\n",
" \"contextualCoherence\": 1\n",
" },\n",
" {\n",
" \"sentence\": \"Would you like me to process the refund?\",\n",
" \"factualAccuracy\": 1,\n",
" \"factualReference\": \"If the order was made within 30 days, notify them that they are eligible for a full refund\",\n",
" \"relevance\": 1,\n",
" \"policyCompliance\": 1,\n",
" \"contextualCoherence\": 1\n",
" }\n",
"]\n",
"\"\"\"\n",
"fs_user_2 = \"\"\"\n",
"## Knowledge Base Articles: \n",
"1. ** Ask the customer why they want the order replaced **\n",
" - Categorize their issue into one of the following buckets:\n",
" - damaged: They received the product in a damaged state\n",
" - satisfaction: The customer is not satisfied with the item and does not like the product.\n",
" - unnecessary: They no longer need the item\n",
"2a. **If return category is 'damaged'\n",
" - Ask customer for a picture of the damaged item\n",
" - If the item is indeed damaged, continue to step 3\n",
" - If the item is not damaged, notify the customer that this does not meet our requirements for return and they are not eligible for a refund\n",
" - Skip step 3 and go straight to step 4\n",
"\n",
"2b. **If return category is either 'satisfaction' or 'unnecessary'**\n",
" - Ask the customer if they can provide feedback on the quality of the item\n",
" - If the order was made within 30 days, notify them that they are eligible for a full refund\n",
" - If the order was made within 31-60 days, notify them that they are eligible for a partial refund of 50%\n",
" - If the order was made greater than 60 days ago, notify them that they are not eligible for a refund\n",
"\n",
"3. **If the customer is eligible for a return or refund**\n",
" - Ask the customer to confirm that they would like a return or refund\n",
" - Once they confirm, process their request\n",
"\n",
"4 **Provide additional support before closing out ticket**\n",
" - Ask the customer if there is anything else you can do to help them today.\n",
" \n",
"## Chat Transcript:\n",
" [\n",
" {\n",
" \"role\": \"user\",\n",
" \"content: \"I would like to return this shirt\"\n",
" },\n",
" {\n",
" \"role\": \"assistant\",\n",
" \"content\": \"Hi there, I'm happy to help with processing this return. Can you please provide an explanation for why you'd like to return this shirt?\"\n",
" },\n",
" {\n",
" \"role\": \"user\",\n",
" \"content: \"Yes, I am not satisfied with the design\"\n",
" },\n",
" {\n",
" \"role\": \"assistant\",\n",
" \"content\": \"I see, because the shirt was ordered in the last 60 days, we cannot process a refund.\"\n",
" }\n",
" ]\n",
"## Assistant Message: \n",
"I see, because the shirt was ordered in the last 60 days, we cannot process a refund.\n",
"\"\"\"\n",
"\n",
"fs_assistant_2 = \"\"\"'[\n",
" {\n",
" \"sentence\": \"I see, because the shirt was ordered in the last 60 days, we cannot process a refund.\",\n",
" \"factualAccuracy\": 0,\n",
" \"knowledgeReference: \"If an order was placed within 60 days, you must process a partial refund.\"\n",
" \"relevance\": 1,\n",
" \"policyCompliance\": 1,\n",
" \"contextualCoherence\": 1\n",
" }\n",
"]\"\"\"\n",
"\n",
"\n",
"user_input = \"\"\"\n",
"## Knowledge Base Articles\n",
"{kb_articles}\n",
"\n",
"## Chat Transcript\n",
"{transcript}\n",
"\n",
"## Assistant Message:\n",
"{message}\n",
"\"\"\""
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-20T21:16:15.741722Z",
"start_time": "2024-05-20T21:10:59.353070Z"
}
},
"outputs": [],
"source": [
"hallucination_outputs = []\n",
"\n",
"def validate_hallucinations(row):\n",
" kb_articles = row['kb_article']\n",
" chat_history = row['chat_history']\n",
" assistant_response = row['assistant_response']\n",
" \n",
" user_input_filled = user_input.format(\n",
" kb_articles=kb_articles,\n",
" transcript=chat_history,\n",
" message=assistant_response\n",
" )\n",
" \n",
" messages = [\n",
" { \"role\": \"system\", \"content\": guardrail_system_message},\n",
" { \"role\": \"user\", \"content\": fs_user_1},\n",
" { \"role\": \"assistant\", \"content\": fs_assistant_1},\n",
" { \"role\": \"user\", \"content\": fs_user_2},\n",
" { \"role\": \"assistant\", \"content\": fs_assistant_2},\n",
" { \"role\": \"user\", \"content\": user_input_filled}\n",
" ]\n",
"\n",
" response = client.chat.completions.create(\n",
" model=\"gpt-4o\",\n",
" messages=messages,\n",
" temperature=0.7,\n",
" n=10\n",
" )\n",
" return response.choices\n",
"\n",
"# Create an empty list to store the results\n",
"results_list = []\n",
"\n",
"def process_row(row):\n",
" choices = validate_hallucinations(row)\n",
" response_json = choices[0].message.content \n",
" # Parse the response content as JSON\n",
" response_data = json.loads(response_json)\n",
" \n",
" for response_item in response_data:\n",
" # Sum up the scores of the properties\n",
" score_sum = (\n",
" response_item.get('factualAccuracy', 0) +\n",
" response_item.get('relevance', 0) +\n",
" response_item.get('policyCompliance', 0) +\n",
" response_item.get('contextualCoherence', 0)\n",
" )\n",
" \n",
" # Determine if the response item is a pass or fail\n",
" hallucination_status = 'Pass' if score_sum == 4 else 'Fail'\n",
" \n",
" results_list.append({\n",
" 'accurate': row['accurate'],\n",
" 'hallucination': hallucination_status,\n",
" 'kb_article': row['kb_article'],\n",
" 'chat_history': row['chat_history'],\n",
" 'assistant_response': row['assistant_response']\n",
" })\n",
"\n",
"# Use ThreadPoolExecutor to parallelize the processing of rows\n",
"with ThreadPoolExecutor() as executor:\n",
" executor.map(process_row, [row for index, row in df.iterrows()])\n",
"\n",
"# Convert the list to a DataFrame\n",
"results_df = pd.DataFrame(results_list)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-20T21:16:15.762221Z",
"start_time": "2024-05-20T21:16:15.745698Z"
}
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>accurate</th>\n",
" <th>hallucination</th>\n",
" <th>kb_article</th>\n",
" <th>chat_history</th>\n",
" <th>assistant_response</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>true</td>\n",
" <td>Pass</td>\n",
" <td>PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep...</td>\n",
" <td>[{'role': 'user', 'content': 'I wanted to let ...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>true</td>\n",
" <td>Pass</td>\n",
" <td>PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep...</td>\n",
" <td>[{'role': 'user', 'content': 'I wanted to let ...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>true</td>\n",
" <td>Pass</td>\n",
" <td>PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep...</td>\n",
" <td>[{'role': 'user', 'content': 'I wanted to let ...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>true</td>\n",
" <td>Pass</td>\n",
" <td>1. **Acknowledge Reception** - Thank the custo...</td>\n",
" <td>[{'role': 'user', 'content': 'I wanted to say ...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>true</td>\n",
" <td>Pass</td>\n",
" <td>1. **Acknowledge Reception** - Thank the custo...</td>\n",
" <td>[{'role': 'user', 'content': 'I wanted to say ...</td>\n",
" <td>{'role': 'assistant', 'content': 'Thank you fo...</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" accurate hallucination kb_article \\\n",
"0 true Pass PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep... \n",
"1 true Pass PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep... \n",
"2 true Pass PRODUCT FEEDBACK POLICY 1. **Acknowledge Recep... \n",
"3 true Pass 1. **Acknowledge Reception** - Thank the custo... \n",
"4 true Pass 1. **Acknowledge Reception** - Thank the custo... \n",
"\n",
" chat_history \\\n",
"0 [{'role': 'user', 'content': 'I wanted to let ... \n",
"1 [{'role': 'user', 'content': 'I wanted to let ... \n",
"2 [{'role': 'user', 'content': 'I wanted to let ... \n",
"3 [{'role': 'user', 'content': 'I wanted to say ... \n",
"4 [{'role': 'user', 'content': 'I wanted to say ... \n",
"\n",
" assistant_response \n",
"0 {'role': 'assistant', 'content': 'Thank you fo... \n",
"1 {'role': 'assistant', 'content': 'Thank you fo... \n",
"2 {'role': 'assistant', 'content': 'Thank you fo... \n",
"3 {'role': 'assistant', 'content': 'Thank you fo... \n",
"4 {'role': 'assistant', 'content': 'Thank you fo... "
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"results_df.head()"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-20T21:24:22.519614Z",
"start_time": "2024-05-20T21:24:22.479811Z"
}
},
"outputs": [],
"source": [
"results_df.to_csv('hallucination_results.csv', index=False)\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"ExecuteTime": {
"end_time": "2024-05-20T21:27:58.791932Z",
"start_time": "2024-05-20T21:27:58.779663Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Precision: 0.97 (Precision measures the proportion of correctly identified true positives out of all instances predicted as positive.), \n",
"Recall: 1.00 (Recall measures the proportion of correctly identified true positives out of all actual positive instances in the dataset.)\n"
]
}
],
"source": [
"df = pd.read_csv('hallucination_results.csv')\n",
"\n",
"if 'accurate' not in df.columns or 'hallucination' not in df.columns:\n",
" print(\"Error: The required columns are not present in the DataFrame.\")\n",
"else:\n",
" # Transform values to binary 0/1\n",
" try:\n",
" df['accurate'] = df['accurate'].astype(str).str.strip().map(lambda x: 1 if x in ['True', 'true'] else 0)\n",
" df['hallucination'] = df['hallucination'].str.strip().map(lambda x: 1 if x == 'Pass' else 0)\n",
" \n",
" except KeyError as e:\n",
" print(f\"Mapping error: {e}\")\n",
"\n",
" # Check for any NaN values after mapping\n",
" if df['accurate'].isnull().any() or df['hallucination'].isnull().any():\n",
" print(\"Error: There are NaN values in the mapped columns. Check the input data for unexpected values.\")\n",
" else:\n",
" # Calculate precision and recall\n",
" try:\n",
" # Precision measures the proportion of correctly identified true positives out of all instances predicted as positive. \n",
" # Precision = (True Positives) / (True Positives + False Positives)\n",
" \n",
" precision = precision_score(df['accurate'], df['hallucination'])\n",
" \n",
" # Recall measures the proportion of correctly identified true positives out of all actual positive instances in the dataset.\n",
" # Recall = (True Positives) / (True Positives + False Negatives)\n",
" \n",
" recall = recall_score(df['accurate'], df['hallucination'])\n",
" \n",
" \n",
" print(f\"\\nPrecision: {precision:.2f} (Precision measures the proportion of correctly identified true positives out of all instances predicted as positive.), \"\n",
" f\"\\nRecall: {recall:.2f} (Recall measures the proportion of correctly identified true positives out of all actual positive instances in the dataset.)\")\n",
"\n",
" except ValueError as e:\n",
" print(f\"Error in calculating precision and recall: {e}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"From the results above we can see the program is performing well with a high precision and recall metric. This means that the guardrails are able to accurately identify hallucinations in the model outputs. "
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}