Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Introducing Role-Based Messages

00:00 In this lesson, you’re going to improve your bot in two ways. First, you’re going to introduce user input, because as you can see at the moment on line nine, the input into your chatbot is just hardcoded.

00:15 It’s a hardcoded question, it’s the same question every time you run your program, which is of course not what you want. You’d like the user to ask a question of your bot.

00:26 So that’s the first change. And then secondly, we’re going to introduce roles to build some sort of control around the bot’s behavior. Because if the question is no longer hardcoded and the user can start asking questions, we should probably build in some sort of control.

00:44 Okay, so first thing, line four, make some space and type user_input equals input(). And then you’ll need a prompt. And just to save you some time, I’ll copy the prompt.

00:58 And it reads “Describe the Python function that you want help with.” Okay, secondly, you’re going to introduce roles into your chatbot. So there’s two roles to introduce, the developer and the user.

01:14 And you’re going to need a dictionary for each of those roles. And those dictionaries, they sit in a list in input. So line eleven, instead of this hardcoded prompt, delete that, open up a list, hit enter, and then open up your first dictionary using the curly brackets.

01:35 And the first key of the first dictionary is a string and the string is "role". And that role is "developer", also a string.

01:46 And then comma, the second key is "content". That’s also a string. And what goes in here, again, I’m going to copy and paste just to save you some time.

01:56 So this is the content of "content". So you’re defining the bot here, you’re giving it a context. So the developer gives the bot context saying you are a Python coding assistant, only accept Python-related questions.

02:12 Please do note the brackets here, or the parentheses if you like, and then the comma behind that closing parenthesis there. That takes you to the end of that dictionary, type a comma to go to your second dictionary.

02:28 So again, curly brackets. Again, you’re going to need the first key as "role", but this time the value is not "developer", it is "user".

02:37 Again, a comma, and the second key again is "content". And here is where you’re going to use an f-string to link it back to the user_input you have from line five.

02:51 So an f-string with curly brackets and then type user_input. So that is how the user input will be input, I guess, into the chatbot. Okay, great. That’s the code done.

03:07 In the next lesson, you’ll then use it.

Become a Member to join the conversation.