Introduction
Personal assistant agent patterns help design smart helpers that understand and assist with daily tasks easily.
Jump into concepts and practice - no test required
class PersonalAssistantAgent: def __init__(self, skills): self.skills = skills def listen(self, user_input): # Understand user request pass def decide(self, parsed_input): # Choose the right skill to use pass def act(self, decision): # Perform the action pass def run(self, user_input): parsed = self.listen(user_input) decision = self.decide(parsed) return self.act(decision)
skills = ['calendar', 'weather', 'email'] agent = PersonalAssistantAgent(skills)
def listen(self, user_input): return user_input.lower().split()
def decide(self, parsed_input): if 'meeting' in parsed_input: return 'calendar' elif 'weather' in parsed_input: return 'weather' else: return 'default'
def act(self, decision): if decision == 'calendar': return 'Scheduling your meeting.' elif decision == 'weather': return 'Fetching weather info.' else: return 'Sorry, I cannot help with that.'
class PersonalAssistantAgent: def __init__(self, skills): self.skills = skills def listen(self, user_input): return user_input.lower().split() def decide(self, parsed_input): if 'meeting' in parsed_input: return 'calendar' elif 'weather' in parsed_input: return 'weather' else: return 'default' def act(self, decision): if decision == 'calendar': return 'Scheduling your meeting.' elif decision == 'weather': return 'Fetching weather info.' else: return 'Sorry, I cannot help with that.' def run(self, user_input): parsed = self.listen(user_input) decision = self.decide(parsed) return self.act(decision) agent = PersonalAssistantAgent(['calendar', 'weather']) print(agent.run('Can you set a meeting for tomorrow?')) print(agent.run('What is the weather today?')) print(agent.run('Play some music'))
skills = {'greet': lambda: 'Hello!'}
response = skills['greet']()
print(response)skills = {'time': lambda: '12:00 PM'}
response = skills.time()
print(response)