Introduction to AI and Simple
Chatbots
Week 1, Class 1
Learning Objectives
• Understand what Artificial Intelligence (AI) is and its basic
concepts
• Explore how chatbots work as a simple form of AI
• Learn fundamental programming concepts in Python
• Create a simple chatbot that can respond to user input
• Identify how pattern matching is used in AI applications
What is Artificial Intelligence?
• Computer systems that can perform • [Image: Examples of AI in everyday
tasks normally requiring human life]
intelligence
• Machines that can learn, reason, and
solve problems • Think-Pair-Share:
• Programs that can recognize patterns • Which of these AI applications do you
and make decisions use most often?
• A) Virtual assistants
• AI is all around us in: • B) Recommendation systems
• • Virtual assistants (Siri, Alexa) • C) Smart home devices
• • Recommendation systems (Netflix, • D) Games with AI opponents
YouTube)
• • Smart devices and applications
Types of AI: From Simple to
Complex
• Rule-Based AI: Follows pre-programmed rules to make
decisions
• • Example: Simple chatbots, basic game AI
• Machine Learning: Systems that learn from data and improve
over time
• • Example: Recommendation systems, image recognition
• Natural Language Processing: AI that understands and
generates human language
• • Example: Advanced chatbots, translation services
What is a Chatbot?
• A chatbot is: • [Image: Chatbot Diagram]
• • A computer program that simulates
conversation
• • One of the simplest forms of AI that • Discussion Question:
interacts with humans • Have you ever been frustrated by a
• • A system that processes user input chatbot? What happened? What
and generates appropriate responses could have made the experience
better?
• Types of chatbots:
• • Rule-based: Uses pattern matching
to find keywords (what we'll build
today)
• • AI-powered: Uses machine learning
to understand context and generate
responses
How Chatbots Work: Basic Chatbot
Architecture
• 1. Input Processing: Receive and clean user input
• 2. Pattern Matching: Identify keywords or patterns
• 3. Response Selection: Choose an appropriate response
• 4. Output Generation: Display the response to the user
• [Image: Chatbot Process Flowchart]
• Hands-on Demo: Let's trace through this process with an
example input
Pattern Matching in Chatbots
• Pattern Matching Concept • Python Implementation
• Pattern matching involves: • Code Example:
• • Looking for specific words or
phrases in user input • # Check for greeting keywords
• • Categorizing input based on • greeting_keywords = ["hello", "hi",
detected patterns "hey"]
• • Selecting responses based on the • for keyword in greeting_keywords:
category • if keyword in user_input:
• return
• Example: random.choice(greeting_responses)
• • User: "Hello there, how are you?"
• • Bot detects: "hello" → greeting • Quick Check: What category would
category this input match?
• • Bot responds: "Hi! How can I help • "Can you tell me what artificial
you today?" intelligence is?"
Python Basics for Our Chatbot
• Python Concepts • Code Examples
• Key Python Concepts We'll Use: • Code Example:
• • Variables: Store data like user input
• • Strings: Text data for messages • # Variable
• • Lists: Store multiple responses • user_input = "hello there"
• • Dictionaries: Organize responses by
category • # List
• • Functions: Organize code into • responses = ["Hi!", "Hello!", "Hey
reusable blocks there!"]
• • Loops: Repeat actions (like our
conversation loop) • # Dictionary
• • Conditionals: Make decisions based • categories = {
on input
• "greeting": ["Hi!", "Hello!"],
• "question": ["I don't know", "Good
question"]
Building Our Chatbot: Step-by-Step
Approach
• 1. Set Up Response Categories: Create a dictionary with
different types of responses
• 2. Create Input Processing: Write a function to get and
process user input
• 3. Implement Pattern Matching: Write a function to find
keywords and select responses
• 4. Display Responses: Create a function to show the chatbot's
responses
• 5. Build the Main Loop: Create the conversation cycle that
continues until the user exits
Step 1: Set Up Response Categories
• # Dictionary of response categories
responses = {
# Greetings
"greeting": [
"Hello! How can I help you today?",
"Hi there! I'm your friendly AI chatbot.",
"Greetings! What would you like to talk about?"
],
# Questions about the bot
"bot_question": [
"I'm a simple AI chatbot created for learning purposes.",
"I'm just a program designed to demonstrate basic AI concepts."
],
# Default responses
"default": [
"I'm not sure I understand. Could you rephrase that?",
"Interesting! Tell me more about that."
]
}
Step 2: Create Input Processing
• Function Purpose • Python Implementation
• Function to Get User Input: • Code Example:
• • Prompt the user for input
• • Convert input to lowercase • def get_user_input():
• • Return the processed input • # Prompt the user and get their
input
• Why convert to lowercase? • user_input = input("You: ")
• • Makes pattern matching easier •
• • "Hello" and "hello" will be treated • # Convert to lowercase
the same • user_input = user_input.lower()
• • Simplifies our keyword checking •
• # Return the processed input
• return user_input
Step 3: Implement Pattern
Matching
• def get_response(user_input):
# Check for greeting keywords
greeting_keywords = ["hello", "hi", "hey", "greetings"]
for keyword in greeting_keywords:
if keyword in user_input:
return random.choice(responses["greeting"])
# Check for questions about the bot
bot_keywords = ["who are you", "what are you", "your name"]
for keyword in bot_keywords:
if keyword in user_input:
return random.choice(responses["bot_question"])
# If no keywords match, use a default response
return random.choice(responses["default"])
Step 4: Display Responses
• Function Purpose • Python Implementation
• Function to Display Responses: • Code Example:
• • Format the response with a "Bot: "
prefix • def display_response(response):
• • Print the formatted response • # Print the response with a "Bot: "
• • Add a blank line for better prefix
readability • print(f"Bot: {response}")
•
• This function helps create a clear • # Add a blank line for better
conversation interface where the readability
user can distinguish between their • print()
input and the chatbot's responses.
Step 5: Build the Main Loop
• def main():
# Display welcome message
print("Welcome to the Simple AI Chatbot!")
print("Type 'exit', 'quit', or 'bye' to end the conversation.")
print()
# Main conversation loop
while True:
# Get input from the user
user_input = get_user_input()
# Check if the user wants to exit
exit_commands = ["exit", "quit", "bye"]
if user_input in exit_commands:
break
# Get an appropriate response
response = get_response(user_input)
# Display the response
display_response(response)
Running Our Chatbot
• Program Flow • Python Implementation
• When we run our program, it will: • Code Example:
• 1. Display a welcome message
• 2. Wait for user input • # This is the standard way to make a
• 3. Process the input and select a Python script executable
response • if __name__ == "__main__":
• 4. Display the response • main()
• 5. Repeat until the user types an exit
command • [Image: Chatbot Conversation
• 6. Display a goodbye message Example]
Limitations of Our Simple Chatbot
• Our Simple Chatbot • Advanced AI Chatbots
• What Our Chatbot Can't Do: • How Advanced Chatbots Improve:
• • Understand the meaning or context • • Use Natural Language Processing to
of words understand meaning
• • Remember previous parts of the • • Maintain conversation history
conversation • • Learn from millions of examples
• • Learn from interactions • • Generate new, unique responses
• • Generate new responses it wasn't • • Handle complex queries and follow-
programmed with up questions
• • Handle complex or ambiguous
questions
Ways to Improve Our Chatbot:
Extension Ideas
• 1. Add more response categories and keywords
• • Add responses for questions about weather, sports, or
movies
• 2. Improve the keyword matching
• • Check for partial matches or word variations
• 3. Add a memory feature
• • Remember the user's name if they provide it
• • Refer to previous topics in the conversation
Real-World Applications of
Chatbots
• Common Applications • Benefits
• Where Chatbots Are Used: • [Image: Chatbot Applications]
• • Customer Service: Answering
common questions and resolving • Chatbots can handle routine tasks,
simple issues allowing humans to focus on more
• • Virtual Assistants: Helping users complex problems that require
with tasks and information creativity and empathy.
• • Education: Tutoring and answering
student questions
• • Healthcare: Providing basic medical
information and scheduling
• • E-commerce: Helping customers
find products and complete
purchases
Ethical Considerations: Important
Questions About Chatbots and AI
• • Transparency: Should chatbots identify themselves as non-
human?
• • Privacy: How should chatbots handle personal information?
• • Accuracy: What happens when chatbots provide incorrect
information?
• • Bias: How do we ensure chatbots treat all users fairly?
• • Dependency: How might reliance on AI assistants affect
Summary: What We've Learned
Today
• • Artificial Intelligence is about creating systems that can
perform tasks requiring human intelligence
• • Chatbots are a simple form of AI that can process user input
and generate responses
• • Pattern matching is a basic technique used to identify
keywords in user input
• • Python provides tools for creating simple AI applications
Hands-On Activity: Create Your
Own Chatbot
• 1. Open the starter code in your Python editor
• 2. Run the code to see how the basic chatbot works
• 3. Add at least one new response category with appropriate
keywords
• 4. Add at least three new responses to each category
• 5. Test your chatbot with different inputs
• 6. Challenge: Can you make your chatbot remember the
user's name?
Questions?
• [Image: Questions?]
• Final Activity: Write one question you have on a sticky note
and post it on the board. We'll address these questions at the
beginning of our next class.