Introduction
A customer support agent architecture helps computers understand and answer customer questions quickly and correctly.
Jump into concepts and practice - no test required
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
agent = CustomerSupportAgent(knowledge_base=my_faq, nlp_model=my_nlp)
response = agent.respond('How do I reset my password?')class SimpleNLP: def process(self, text): return text.lower() class SimpleKB: def search(self, question): if 'password' in question: return 'To reset your password, click on Forgot Password.' return 'Sorry, I do not know.' agent = CustomerSupportAgent(SimpleKB(), SimpleNLP()) print(agent.respond('How do I reset my password?'))
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')
def respond(question):
if 'refund' in question.lower():
return 'Please provide your order ID for refund.'
elif 'shipping' in question.lower():
return 'Shipping takes 3-5 business days.'
else:
return 'Can you please clarify your question?'
print(respond('How long is shipping?'))def answer_question(text):
if 'price' in text:
return 'Our prices start at $10.'
elif 'delivery' in text:
return 'Delivery takes 5 days.'
else
return 'Sorry, I did not understand.'