Change AI Personality Without Restarting the Chatbot (Lesson 21)

Series: Build AI Assistant with Python (2026 Edition)

Primary Focus Keyword: Change AI Personality in Python Chatbot

Secondary Keywords: Python AI Personality Switch, AI Chatbot Commands, Dynamic System Prompt, Python AI Assistant


What You Will Learn

In this lesson, you will learn:

  • How to switch AI personalities while the chatbot is running
  • Why changing personalities should start a new conversation
  • How to update the active personality using a command
  • How to reuse the personalities from personalities.py

By the end of this lesson, users can type /personality at any time and instantly switch to a different AI assistant.


Quick Answer

Instead of closing and restarting your chatbot, you can create a command that lets users choose a new AI personality. When the personality changes, the chatbot also starts a new conversation so the previous context does not affect the new assistant.


Why Do We Need This?

Imagine you’re using your chatbot as a Python teacher. Later, you want career advice. Right now, you must:

  1. Close the chatbot.
  2. Run the program again.
  3. Choose another personality.

That isn’t convenient. A better solution is to change the personality while the chatbot is running.


Step 1: Create a Function

Open app.py.

Above the while loop, add this function.

def choose_personality():

    print("\nChoose an AI Personality")

    print("1. Python Teacher")
    print("2. Career Coach")
    print("3. Travel Guide")
    print("4. Interview Coach")

    choice = input("\nEnter your choice: ")

    if choice == "1":
        return PYTHON_TEACHER

    elif choice == "2":
        return CAREER_COACH

    elif choice == "3":
        return TRAVEL_GUIDE

    elif choice == "4":
        return INTERVIEW_COACH

    else:
        print("\nInvalid choice.")
        print("Using Python Teacher.")

        return PYTHON_TEACHER

This function displays the menu and returns the selected personality.


Step 2: Select the First Personality

Before the chatbot starts, call the function.

personality = choose_personality()

Now your chatbot begins with the user’s selected personality.


Step 3: Create the /personality Command

Inside your while loop, add:

if user_message == "/personality":

    personality = choose_personality()

    previous_response_id = None

    print("\nAI personality changed.")

    continue

Whenever the user types /personality, the chatbot displays the menu again.


Why Reset the Conversation?

Suppose you start with the Python Teacher.

You: Explain Python classes.

AI:
...

Now switch to the Travel Guide. If we keep the old conversation, the AI may still remember the earlier programming discussion. That can produce confusing answers. Instead, reset the conversation.

previous_response_id = None

Now the new personality starts with a fresh conversation.


Complete Example

Your command section now looks like this.

if user_message == "/help":

    ...

if user_message == "/clear":

    previous_response_id = None

    print("\nConversation cleared.")

    continue

if user_message == "/personality":

    personality = choose_personality()

    previous_response_id = None

    print("\nAI personality changed.")

    continue

if user_message == "/exit":

    print("Goodbye!")

    break


Example Session

Choose an AI Personality

1. Python Teacher
2. Career Coach
3. Travel Guide
4. Interview Coach

Enter your choice: 1

You: Explain Python loops.

AI:
...

You: /personality

Choose an AI Personality

1. Python Teacher
2. Career Coach
3. Travel Guide
4. Interview Coach

Enter your choice: 3

AI personality changed.

You: Suggest places to visit in Kerala.

AI:
...

Notice that the chatbot immediately behaves like a travel guide.


Why Is This Better?

Without this feature:

  • Restart the application
  • Select another personality
  • Lose your workflow

With this feature:

  • Switch personalities instantly
  • Continue using the same application
  • Better user experience

Common Mistakes

Forgot to Reset the Conversation

Always reset:

previous_response_id = None

This prevents one personality from using another personality’s conversation.


Forgot continue

Without continue, Python will still send /personality to the AI.


Menu Doesn’t Appear

Check that you called:

choose_personality()

inside the /personality command.


Key Takeaways

  • Create reusable functions to avoid duplicate code.
  • Let users change AI personalities at any time.
  • Reset the conversation after changing personalities.
  • Reuse the same menu for startup and later changes.

Mini Quiz

1. Which command changes the AI personality?

A. /change

B. /switch

C. /personality

D. /menu

Answer: C


2. Why do we reset the conversation after changing personalities?

A. To save memory

B. To avoid mixing different conversations

C. To install packages

D. To restart Python

Answer: B


3. Which function displays the personality menu?

A. select_ai()

B. choose_personality()

C. personality_menu()

D. change_ai()

Answer: B


4. Should the chatbot be restarted to change personalities?

A. Yes

B. No

Answer: B


5. Why is using a function a good idea?

A. It avoids writing the same code multiple times.

B. It makes Python faster.

C. It changes the AI model.

D. It stores API keys.

Answer: A


Practice Exercise

Complete these tasks.

  1. Create the choose_personality() function.
  2. Use it when the chatbot starts.
  3. Add the /personality command.
  4. Test all four personalities.
  5. Verify that the conversation resets after switching.

Lesson Summary

Excellent!

Your chatbot is becoming much more user-friendly. Users can now switch between different AI personalities without restarting the application, making the chatbot feel like a real AI assistant.


Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top