Skip to content
Home Tutorials Roadmaps Courses
Log in Join free
Tutorials AI Build a Chatbot
AI Intermediate FREE

Build a Chatbot

Lesson 1 of 17 Intermediate Interactive

Build a simple chatbot using Python and NLP techniques.

Syntax

AI
from transformers import pipeline
import random

def chatbot(message):
    classifier = pipeline("zero-shot-classification")
    result = classifier(message, ["greeting", "question", "goodbye"])
    return responses[result["labels"][0]]
Simple Chatbot
PYTHON
import random

responses = {
    "greeting": ["Hello!", "Hi there!", "Hey!"],
    "goodbye": ["Bye!", "See you!", "Goodbye!"],
    "thanks": ["You're welcome!", "Happy to help!"],
    "default": ["Tell me more!", "Interesting!", "I see."]
}

def get_intent(text):
    text = text.lower()
    if any(w in text for w in ["hi", "hello", "hey"]):
        return "greeting"
    if any(w in text for w in ["bye", "goodbye"]):
        return "goodbye"
    if any(w in text for w in ["thanks", "thank"]):
        return "thanks"
    return "default"

def chatbot(text):
    intent = get_intent(text)
    return random.choice(responses[intent])

print(chatbot("Hello!"))
print(chatbot("Thanks for help"))
print(chatbot("Goodbye"))