This program creates a simple customer support agent that answers questions based on keywords in a small FAQ.
class SimpleNLP:
def process(self, text):
return text.lower()
class SimpleKB:
def __init__(self):
self.faq = {
'reset password': 'To reset your password, click on Forgot Password link.',
'refund policy': 'You can request a refund within 30 days of purchase.',
'shipping time': 'Shipping usually takes 3-5 business days.'
}
def search(self, question):
for key in self.faq:
if key in question:
return self.faq[key]
return 'Sorry, I do not have an answer for that.'
class CustomerSupportAgent:
def __init__(self, knowledge_base, nlp_model):
self.knowledge_base = knowledge_base
self.nlp_model = nlp_model
def understand_question(self, question):
return self.nlp_model.process(question)
def find_answer(self, processed_question):
return self.knowledge_base.search(processed_question)
def respond(self, question):
processed = self.understand_question(question)
answer = self.find_answer(processed)
return answer
# Create agent
agent = CustomerSupportAgent(SimpleKB(), SimpleNLP())
# Test questions
questions = [
'How do I reset my password?',
'What is your refund policy?',
'Tell me about shipping time.',
'Can I change my order?'
]
for q in questions:
print(f'Q: {q}')
print(f'A: {agent.respond(q)}\n')