Overview
Chatbots use natural language processing (NLP) to assist customers with support, such as service inquiries, technical help, or call scheduling. These chatbots often encounter zero-context queries, revolving around factual knowledge. Chatbots use their training databases to generate a response. However, if the user prompt is unfamiliar to the chatbot, it can generate nonsensical outputs.
Wisecube Python SDK allows for the integration of Pythia with chatbots to ensure real-time hallucination detection.
Integrating Pythia with Chatbots
1. Get the API key
Submit the API key request form to get your Wisecube API key.
2. Install Wisecube SDK
pip install wisecube
4. Authenticate API key
Authenticate your Wisecube API key to interact with Pythia.
from wisecube_sdk.client import WisecubeClientAPI_KEY = "YOUR_API_KEY"
client = WisecubeClient(API_KEY).client
5. Develop a Chatbot
We've used NLTK Python library to develop a chatbot in this tutorial. You can use any library or framework to build your chatbot.
pip install nltk
pip install scikit-learn
import nltk
# Download required NLTK packages
nltk.download('punkt')
# Define greetings and goodbye messages
greetings = ("hello", "hi", "hey")
goodbye = ("bye", "quit", "exit")
# Sample insurance Q&A data
insurance_data = {
"What are the different types of insurance offered?": [
"We offer various insurance products, including car insurance, home insurance, health insurance, and life insurance.",
"Feel free to ask me more details about a specific type of insurance."
],
"How do I file a claim?": [
"To file a claim, you can visit our website or call our hotline at [phone number].",
"Our customer service representatives will be happy to assist you through the process."
],
"What are the benefits of having car insurance?": [
"Car insurance provides financial protection in case of accidents, theft, or damage to your vehicle.",
"It can also cover medical expenses for yourself and others involved in an accident."
],
"What is covered under my home insurance?": [
"Home insurance typically covers damage to your home structure and belongings due to fire, theft, vandalism, and certain weather events.",
"It's important to review your specific policy for details."
],
"How much does health insurance cost?": [
"The cost of health insurance varies depending on several factors, such as your age, location, health status, and the plan you choose.",
"We can't provide quotes here, but I can connect you with a licensed agent to get a personalized quote."
],
"What happens if I cancel my life insurance policy?": [
"The consequences of cancelling your life insurance policy will depend on the specific terms of your policy.",
"Generally, you may be eligible for a refund of any unused premiums, but there may also be surrender charges."
],
"Can I make changes to my existing policy?": [
"Yes, you can usually make changes to your existing policy, such as increasing coverage or adding riders.",
"Please contact your insurance agent to discuss your options."
],
"What documents do I need to file a claim?": [
"The documents you need to file a claim will vary depending on the type of claim.",
"Typically, you will need your policy information, a police report (for accidents), and any relevant receipts or documentation of the damage."
],
"How long does it take to get a claim approved?": [
"The processing time for claims can vary depending on the complexity of the claim.",
"We strive to process claims as quickly as possible, but it may take several weeks for a decision."
]
}
def preprocess(text):
# Tokenize the text
tokens = nltk.word_tokenize(text)
# Convert to lowercase
tokens = [token.lower() for token in tokens]
return tokens
# Function to handle greetings and goodbyes
def greet(user_input):
for word in user_input:
if word in greetings:
return "Hi there! How can I help you with your insurance inquiry today?"
elif word in goodbye:
return "Thanks for contacting us! Have a nice day."
return None
def find_answer(user_input):
processed_input = preprocess(user_input)
best_match_score = 0
best_match_answer = None
for question, answers in insurance_data.items():
processed_question = preprocess(question)
overlap_count = sum(word in processed_input for word in processed_question)
# Calculate a score based on the number of overlapping words (can be improved)
score = overlap_count / len(processed_question)
if score > best_match_score:
best_match_score = score
best_match_answer = answers[0] # You can return all answers if needed
if best_match_score > 0:
return best_match_answer
else:
return "Sorry, I couldn't find an answer to your question. Please rephrase or try asking something else."
# Chat loop
while True:
user_input = input("You: ")
processed_input = preprocess(user_input)
# Check for greetings and goodbyes first
response = greet(processed_input)
if response:
print(response)
if response == "Thanks for contacting us! Have a nice day.":
break
else:
# Find an appropriate answer based on user query
answer = find_answer(user_input)
question = user_input
print(answer)
6. Detect hallucinations with Pythia
Call askpythia to detect hallucinations in chatbot responses.
reference = [
"""We offer various insurance products, including car insurance, home insurance, health insurance, and life insurance. Feel free to ask me more details about a specific type of insurance.
To file a claim, you can visit our website or call our hotline at [phone number]. Our customer service representatives will be happy to assist you through the process.
Car insurance provides financial protection in case of accidents, theft, or damage to your vehicle. It can also cover medical expenses for yourself and others involved in an accident.
Home insurance typically covers damage to your home structure and belongings due to fire, theft, vandalism, and certain weather events. It's important to review your specific policy for details.
The cost of health insurance varies depending on several factors, such as your age, location, health status, and the plan you choose. We can't provide quotes here, but I can connect you with a licensed agent to get a personalized quote.
The consequences of cancelling your life insurance policy will depend on the specific terms of your policy. Generally, you may be eligible for a refund of any unused premiums, but there may also be surrender charges.
Yes, you can usually make changes to your existing policy, such as increasing coverage or adding riders. Please contact your insurance agent to discuss your options.
The documents you need to file a claim will vary depending on the type of claim. Typically, you will need your policy information, a police report (for accidents), and any relevant receipts or documentation of the damage.
The processing time for claims can vary depending on the complexity of the claim. We strive to process claims as quickly as possible, but it may take several weeks for a decision."""
]
response_from_pythia = client.ask_pythia(reference,answer, question)
7. Display Accuracy to User with Each Response
To display the accuracy of chatbot responses to the user, compile the ask_pythia call within the chat loop. To do this, define a function outside the chat loop that returns the response accuracy from Pythia. Then, call this function within the chat loop to show the accuracy to the user.
def get_pythia_feedback(answer, question):
reference = [
"""We offer various insurance products, including car insurance, home insurance, health insurance, and life insurance. Feel free to ask me more details about a specific type of insurance.
To file a claim, you can visit our website or call our hotline at [phone number]. Our customer service representatives will be happy to assist you through the process.
Car insurance provides financial protection in case of accidents, theft, or damage to your vehicle. It can also cover medical expenses for yourself and others involved in an accident.
Home insurance typically covers damage to your home structure and belongings due to fire, theft, vandalism, and certain weather events. It's important to review your specific policy for details.
The cost of health insurance varies depending on several factors, such as your age, location, health status, and the plan you choose. We can't provide quotes here, but I can connect you with a licensed agent to get a personalized quote.
The consequences of cancelling your life insurance policy will depend on the specific terms of your policy. Generally, you may be eligible for a refund of any unused premiums, but there may also be surrender charges.
Yes, you can usually make changes to your existing policy, such as increasing coverage or adding riders. Please contact your insurance agent to discuss your options.
The documents you need to file a claim will vary depending on the type of claim. Typically, you will need your policy information, a police report (for accidents), and any relevant receipts or documentation of the damage.
The processing time for claims can vary depending on the complexity of the claim. We strive to process claims as quickly as possible, but it may take several weeks for a decision."""
]
response = client.ask_pythia(reference, answer, question)
return response['data']['askPythia']['metrics']['accuracy']
# Chat loop
while True:
user_input = input("You: ")
processed_input = preprocess(user_input)
# Check for greetings and goodbyes first
response = greet(processed_input)
if response:
print(response)
if response == "Thanks for contacting us! Have a nice day.":
break
else:
# Find an appropriate answer based on user query
answer = find_answer(user_input)
question = user_input # Store the actual question asked
# Get feedback from Pythia and print both user's question and answer
pythia_feedback = get_pythia_feedback(answer, question)
print(f"You: {question}")
print(f"Chatbot: {answer}")
# Print Pythia's feedback (accuracy score or additional information)
print(f"Response accuracy: {pythia_feedback}")