What You Will Learn
In this lesson, you will learn:
- What custom commands are
- Why chatbots use commands
- How to create your own custom commands in AI Chatbot
- How to make your chatbot easier to use
By the end of this lesson, your chatbot will support commands like /help, /clear, and /exit.
Quick Answer
A custom command is a special instruction that controls the chatbot instead of sending a message to the AI.
For example:
/help
shows available commands.
/exit
closes the chatbot.
These commands are handled by your Python code, not by the AI model.
Why Do We Need Commands?
Imagine using your chatbot every day. Sometimes you don’t want to ask the AI a question. Instead, you want to perform an action. For example:
- View available commands
- Start a new conversation
- Exit the application
Commands make your chatbot feel like a real application.
Our Current Chatbot
Right now, every input is sent to the AI.
You: Hello
The AI responds.
But what happens if you type:
/help
The chatbot sends it to the AI. That isn’t what we want. Instead, Python should recognize /help and handle it immediately.
Step 1: Create a Help Command
Inside your while loop, after reading the user’s input, add this code.
if user_message == "/help":
print("\nAvailable Commands")
print("/help Show all commands")
print("/clear Start a new conversation")
print("/personality Change AI personality")
print("/exit Exit the chatbot")
continue
If the user types /help, Python displays the command list and skips the API request. The AI is never called.
Step 2: Create an Exit Command
Replace your existing exit condition with this.
if user_message == "/exit":
print("Goodbye!")
break
Now your chatbot uses a professional command instead of requiring users to type exit.
Step 3: Create a Clear Command
To start a new conversation, reset the conversation ID.
if user_message == "/clear":
previous_response_id = None
print("\nConversation cleared.")
continue
The next question starts a completely new conversation.
How Does /clear Work?
Suppose you ask:
You: My name is Rahul.
AI: Nice to meet you, Rahul.
Now type:
/clear
Then ask:
What is my name?
The AI no longer remembers your earlier conversation because the conversation ID was reset.
Complete Example
Your while loop now begins like this.
while True:
user_message = input("\nYou: ")
if user_message == "/help":
print("\nAvailable Commands")
print("/help")
print("/clear")
print("/personality")
print("/exit")
continue
if user_message == "/clear":
previous_response_id = None
print("\nConversation cleared.")
continue
if user_message == "/exit":
print("Goodbye!")
break
try:
# API request goes here
Notice that all commands are checked before calling the OpenAI API.
Example Session
=====================================
AI Assistant
=====================================
You: /help
Available Commands
/help
/clear
/personality
/exit
You: Hello
AI:
Hello! How can I help you today?
You: /clear
Conversation cleared.
You: What is my name?
AI:
I don't know your name yet.
Why Is This Better?
Without commands:
- Every input goes to the AI.
- Simple actions require an API request.
With commands:
- Faster
- No unnecessary API calls
- Better user experience
- More professional chatbot
Common Mistakes
Commands Are Sent to the AI
Make sure you check commands before the API request.
Forgot continue
Without continue, Python will still call the AI after executing the command.
Reset Doesn’t Work
Verify that you reset:
previous_response_id = None
This starts a completely new conversation.
Key Takeaways
- Commands are handled by Python, not the AI.
- Check commands before sending messages to the API.
/helpdisplays available commands./clearstarts a new conversation./exitcloses the chatbot.
Mini Quiz
1. What is a custom command?
A. A question for the AI
B. A Python action handled before the API call
C. An API key
D. A model
Answer: B
2. Which command displays available commands?
A. /menu
B. /commands
C. /help
D. /list
Answer: C
3. What does /clear do?
A. Deletes Python
B. Starts a new conversation
C. Changes the model
D. Installs packages
Answer: B
4. Which command exits the chatbot?
A. /quit
B. /close
C. /exit
D. /stop
Answer: C
5. Should commands be checked before calling the API?
A. Yes
B. No
Answer: A
Practice Exercise
Add these commands to your chatbot.
/help/clear/exit
Test each one and make sure:
/helpshows the command list./clearstarts a fresh conversation./exitcloses the chatbot.
Lesson Summary
Excellent! Your chatbot is now much more interactive. Instead of sending every input to the AI, your Python program can recognize special commands and perform actions immediately. This is how many professional command-line applications work.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
Shubhranshu Shekhar is a coding instructor, mentor, and founder of VSIT Delhi with 20+ years of teaching experience (since 2004). He has guided many students who are now working in multinational companies and specializes in Full Stack Development, Python, Digital Marketing, and Data Analytics.
