Creating an Engaging Personality in Chatbot Development
Creating an engaging personality is a fun and absolutely crucial part of chatbot development. It's one of the key differences compared to any other kind of software. But why bother with the personality? Well, if all you could do was type precise instructions to your bot, you would actually just have a command-line application not a chat bot.
Most chat bots are embedded in a messaging app that people are comfortable using to talk to their friends and can be expected to make a bit of small talk often before trying out any of the functionality they came for. It's not much effort to code up some responses to common questions, and it's worth it for the improved user experience.
The simplest thing we can do is use a Python dictionary with user messages as the keys and responses as the values. For example, here we define a dictionary called responses with the messages "what's your name" and "what's today's weather" as keys and suitable responses as values. Next, we define a function called respond which accepts a message as a single argument.
This function tests if the message has a defined response by using the in keyword. That is, if message in responses. This statement only returns true if the message corresponds to one of the keys in the dictionary. Notice that this will only work if the user's message exactly matches a key in the dictionary. In later chapters, you'll build much more robust solutions.
Notice that if there isn't a matching message, the return keyword will never be reached, so the function will return None. Since the world outside is always changing, your bot's answers have to be able to change as well. The first thing we can do is add some placeholders to the responses. For example, instead of the weather being sunny, we can have a template string like "it's [weather today]". Later, we can insert a variable "weather today" by calling format(weather today).
It quickly gets dull hearing the same responses over and over again. So, it's a very good idea to add a little variety to return completely different responses. We can replace the values in the responses dictionary with lists then when you're choosing a response, you can randomly select an answer from the appropriate list. To do this, we import random and use the random.choice function passing the list of options as an argument.
For now, we're still relying on the user message exactly matching one of our predefined messages but will soon move to a more flexible approach. A great way to keep users engaged is to ask them questions or invite them to go into more detail. This was actually one of the things which made the Eliza bots so fun to talk to instead of a bland default message like "I'm sorry I didn't understand you". You can use some phrases that invite further conversation, such as "why do you think that" and "how long have you felt this way". These are all appropriate responses to many different kinds of messages and even when they don't quite match are more entertaining than a boring fallback.
"WEBVTTKind: captionsLanguage: encreating an engaging personality is a fun and absolutely crucial part of chatbot development it's one of the key differences compared to any other kind of software but why bother with the personality well if all you could do was type precise instructions to your bot you would actually just have a command-line application not a chat bot most chat bots are embedded in a messaging app that people are comfortable using to talk to their friends and you can expect it straight away your users will want to make a bit of small talk often before trying out any of the functionality that they came for it's not much effort to code up some responses to common questions and it's worth it for the improved user experience the simplest thing we can do is use a Python dictionary with user messages as the keys and responses as the values for example here we define a dictionary called responses with the messages what's your name and what's today's weather as keys and suitable responses as values next we define a function called respond which accepts a message as a single argument this function tests if the message has a defined response by using the in keyword that is if message in responses this statement only returns true if the message corresponds to one of the keys in the dictionary notice that this will only work if the users message exactly matches a key in the dictionary in later chapters you will build much more robust solutions notice that if there isn't a matching message the return keyword will never be reached so the function will return none since the world outside is always changing your BOTS answers have to be able to as well the first thing you can do is add some placeholders to the responses for example instead of the weather is sunny you can have a template string like it's brackets today then later you can insert a variable weather today by calling format weather today now it quickly gets dull hearing the same responses over and over again so it's a very good idea to add a little variety to return completely different responses we can replace the values in the responses dictionary with lists then when you're choosing a response you can randomly select an answer from the appropriate list to do this import random and use the random dot choice function passing the list of options as an argument for now we're still relying on the user message exactly matching one of our predefined messages but will soon move to a more flexible approach a great way to keep users engaged is to ask them questions or invite them to go into more detail this was actually one of the things which made the Eliza BOTS so fun to talk to instead of a bland default message like I'm sorry I didn't understand you you can use some phrases that invite further conversation questions are a great way to achieve this why do you think that how long have you felt this way and tell me more are all appropriate responses to many different kinds of message and even when they don't quite match are more entertaining than a boring fallbackcreating an engaging personality is a fun and absolutely crucial part of chatbot development it's one of the key differences compared to any other kind of software but why bother with the personality well if all you could do was type precise instructions to your bot you would actually just have a command-line application not a chat bot most chat bots are embedded in a messaging app that people are comfortable using to talk to their friends and you can expect it straight away your users will want to make a bit of small talk often before trying out any of the functionality that they came for it's not much effort to code up some responses to common questions and it's worth it for the improved user experience the simplest thing we can do is use a Python dictionary with user messages as the keys and responses as the values for example here we define a dictionary called responses with the messages what's your name and what's today's weather as keys and suitable responses as values next we define a function called respond which accepts a message as a single argument this function tests if the message has a defined response by using the in keyword that is if message in responses this statement only returns true if the message corresponds to one of the keys in the dictionary notice that this will only work if the users message exactly matches a key in the dictionary in later chapters you will build much more robust solutions notice that if there isn't a matching message the return keyword will never be reached so the function will return none since the world outside is always changing your BOTS answers have to be able to as well the first thing you can do is add some placeholders to the responses for example instead of the weather is sunny you can have a template string like it's brackets today then later you can insert a variable weather today by calling format weather today now it quickly gets dull hearing the same responses over and over again so it's a very good idea to add a little variety to return completely different responses we can replace the values in the responses dictionary with lists then when you're choosing a response you can randomly select an answer from the appropriate list to do this import random and use the random dot choice function passing the list of options as an argument for now we're still relying on the user message exactly matching one of our predefined messages but will soon move to a more flexible approach a great way to keep users engaged is to ask them questions or invite them to go into more detail this was actually one of the things which made the Eliza BOTS so fun to talk to instead of a bland default message like I'm sorry I didn't understand you you can use some phrases that invite further conversation questions are a great way to achieve this why do you think that how long have you felt this way and tell me more are all appropriate responses to many different kinds of message and even when they don't quite match are more entertaining than a boring fallback\n"