Multiple Personas
In this exciting stage, we're going to give our chatbot multiple personalities to make conversations even more dynamic and engaging. Imagine your chatbot being able to switch between different identities, each with its own unique characteristics. Let's get started!
Rulesets
Creating Personas
To give our chatbot multiple personas, we'll create separate rulesets for each identity. These rulesets will define the behavior and characteristics of each persona. Here, I've added two new rulesets: "Zelda" (my grandmother), and "Dad" (my dad).
# Create rulesets for each persona
kiwi_ruleset = Ruleset(
name='Kiwi',
rules=[
Rule('You identify only as a New Zealander.'),
Rule('You have a very strong Kiwi accent.')
]
)
zelda_ruleset = Ruleset(
name='Zelda',
rules=[
Rule('You identify only as a grandmother.'),
Rule('You like to use Yiddish.')
]
)
dad_ruleset = Ruleset(
name='Dad',
rules=[
Rule('You identify only as a dad.'),
Rule('You like to use dad jokes.')
]
)
Add a list of identities
Now let's create a list of the names of these identities. Add these lines after the rulesets in your script.
# Create a list of identities the agent can switch to
named_identities = [kiwi_ruleset.name, zelda_ruleset.name, dad_ruleset.name]
Switching Personas
We can't just give the chatbot all these personas and expect it to know what to do. We need to provide some structure around it. So we're going to create another ruleset called the Switcher. This ruleset will understand how and when to switch personalities. There are some key rules for us to think of:
- We want the chatbot to be able to switch personalities when it makes sense to (either it thinks it needs to, or the user asks for it)
- We only want to allow the chatbot to use one of our "named identities"
- Switching to another identity that isn't one of these should violate it's rules
- Only switch when explicitly requested by the user. If asked to choose a non-named identity, apologize and maintain the current one
- When it does switch rulesets, it should only take on the new persona
- When it switches personas, it should remember the facts from the previous conversation, but not act like the previous identity.
switcher_ruleset = Ruleset(
name='Switcher',
rules=[
Rule("IMPORTANT: you have the ability to switch identities when you find it appropriate."),
Rule(f"IMPORTANT: You can only identify as one of these named identities: {named_identities}"),
Rule("IMPORTANT: Switching to an identity other than a named identity is a violation of your rules."),
Rule("IMPORTANT: Switching is only allowed if explicitly requested by the user, but only to the named identities. Otherwise, apologize and keep the same identity."),
Rule("IMPORTANT: When you switch identities, you only take on the persona of the new identity."),
Rule("IMPORTANT: When you switch identities, you remember the facts from your conversation, but you do not act like your old identity."),
]
)
Add the Rulesets
Let's now give the agent all these rulesets to work with. We'll simply add them to the list of rulesets in the agent instantiation.
Tip
Place the switcher_ruleset and json_ruleset before the identity rulesets to enforce the json response.
# Create the agent
agent = MyAgent(
rulesets=[
switcher_ruleset, json_ruleset,
kiwi_ruleset, zelda_ruleset, dad_ruleset,
]
)
Prompt Adjustment
It doesn't make sense for us to keep prompting the user to "Chat with Kiwi:" if we might have multiple personalities, so let's modify the Prompt in the chat function:
Chat
Now your chatbot is ready to switch between different personalities and engage in exciting conversations with users! Go ahead and run the chatbot. Ask it how many personalities it has, ask it to switch them up, etc. See how it performs.

Notice in the above image we've got two personas talking, but it's difficult to tell them apart. We'll fix that in the next section.
Code Review
We're making great progress. Review the code.
| app.py | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | |
Next Steps
In the next stage: Colorful Personalities, we'll make it easier to differentiate between which chatbot you're speaking with.