Most chatbot tutorials stop at one of two extremes. Either they build a rule-based bot that breaks the moment a user types something unexpected, or they wrap an LLM API in ten lines and call it a project. Neither gives you much to explain in a viva or an interview.
This guide builds the version that sits in between, and it is how many real support bots work: an intent classifier answers the common questions from approved replies, and an LLM handles the long tail. The example is a college helpdesk bot that answers questions about fees, admissions, timings and placements, but you can swap in any domain by editing one JSON file.
You will write about 100 lines of Python across a few small files, run it in the terminal first, then put a Streamlit chat interface on top. Every step has code you can copy, run and change.
What is a chatbot project in Python?
A chatbot project in Python is a program that reads a user's message, works out what they want, and sends back a useful reply. The interesting part is the "works out what they want" step, which is where machine learning comes in.
The bot in this guide uses intent classification. You define a handful of intents (greeting, fees, admissions, timings, placements, goodbye), give each one 6 to 10 example phrases and a few approved replies, and train a small text classifier on those phrases. When a new message arrives, the classifier predicts the intent and a confidence score.
High confidence means the bot replies from your JSON file: fast, free and exactly the wording you approved. Low confidence means the message is outside what you trained for, so the bot hands it to a large language model through an API.
What do you need to build a chatbot in Python?
You need Python basics, any laptop, and (for the second stage only) an API key for a chat model. No GPU, no big dataset.
| Part | What we use | Why |
|---|---|---|
| Language | Python 3.11 or newer | Current scikit-learn releases require it |
| Training data | A small intents.json file you write | You control every intent and reply |
| Intent model | scikit-learn: TF-IDF plus Logistic Regression | Trains in under a second, gives probabilities |
| Fallback | Any chat model API | The example uses the OpenAI Python SDK; Gemini, Groq or a local model work the same way |
| Interface | Terminal first, then Streamlit | Test fast, then demo in a browser |
You should be comfortable with dictionaries, lists, functions and reading JSON in Python. If you are preparing for placements as well, our Python for AI interview questions cover the same basics this project uses.
How does an intent-based chatbot with an LLM fallback work?
The bot runs the same four steps for every message:
- Clean the text: lowercase it and strip extra spaces.
- Classify the intent: the TF-IDF vectorizer turns the message into numbers, and Logistic Regression returns a probability for each intent.
- Decide the route: if the top probability is above a threshold (we start at 0.5), reply with a random approved response for that intent.
- Fall back: otherwise, send the message plus the last few turns of conversation to an LLM with a short system prompt, and return its reply.
The threshold is the single most important design choice in the project. Set it too low and the bot gives confident wrong answers from the JSON file. Set it too high and almost everything goes to the LLM, which costs money and loses your approved wording. Step 8 shows how to tune it with numbers instead of guessing.