
A chatbot is a program that talks with people in natural language. Building one is a great learning project that combines input and output, decision logic and external APIs. In this guide you will build two versions in Python: a rule-based bot that you control completely, and an LLM-powered bot that sends the conversation to a large language model through an API. Seeing both shows why modern assistants feel so different.
The first famous chatbot, ELIZA, was written by Joseph Weizenbaum at MIT in 1966. It imitated a therapist by spotting keywords and turning your sentences into questions. For decades, chatbots worked the same way, with hand-written rules and decision trees. Since the public launch of chat assistants built on large language models in late 2022, developers can instead send text to a model that generates fluent answers to almost any question.
Many real products combine the two: rules for fixed tasks such as opening hours, and a model for everything else.
Install a current version of Python 3, create a project folder and set up a virtual environment. Run python -m venv .venv, then activate it with source .venv/bin/activate on macOS and Linux or .venv\Scripts\activate on Windows. Create a file called bot.py.
The heart of every chatbot is a loop that reads a message, chooses a reply and prints it. A simple version follows these steps:
Test unusual wording and its limits appear quickly: "When do you close?" does not contain "hours". Synonyms and regular expressions from the re module help, but every improvement means more rules to maintain.
Most AI providers offer a similar HTTP API and an official Python library. Details differ, but the pattern is the same:
Each message is a small dictionary with a role and content, such as {"role": "user", "content": "Hi!"}. The model's answers use the role assistant.
The API does not remember earlier requests. To let the bot follow a conversation, keep a list of messages, append each user message and each reply, and send the whole list every time. Long chats grow expensive, so many bots keep only the recent messages.
A system prompt, sent with the role system or as a separate instruction field depending on the provider, describes who the bot is and how it should answer: "You are a friendly assistant for a bakery. Answer in two sentences. If you do not know, say so." Clear instructions matter more than any other setting. Optional parameters such as a maximum length for the reply and a temperature, which controls how varied the wording is, let you fine-tune it.
Everything users type is sent to the provider, so tell them, and avoid sending passwords, health data or other personal information. Check the provider's terms on data use and retention. Users may also try to override your instructions with tricks called prompt injection, so never give a public bot access to secrets or actions it should not perform. Usage is billed by tokens, small chunks of text, so monitor your dashboard and set limits.
Next, give your bot a web interface with Flask or Streamlit, or let it answer questions from your own documents. Keep the rule-based version too: it shows clearly what the model is doing for you.
Grade 10 Math: Functions and Reasoning - Quiz
1170 questions