How to Build an AI Chatbot in Python

Build an AI chatbot in Python step by step: a chat loop with memory, a web chat UI in Streamlit, answers from your own documents, and the upgrade that turns a chatbot into an AI agent. Runnable code at every step, written for beginners in Pakistan and India who know basic Python.

By Abdul Rahman Azam, founder of AI Season · Updated

Build chatbots & agents live →

Key facts

You need Basic Python and an API key for a chat model (OpenAI, Gemini, Groq or a local model)
You will build A terminal chatbot, then a web chatbot with memory that answers from your own notes
Libraries openai, streamlit, python-dotenv
Time About 45 minutes
Next Turn it into an AI agent

What an AI chatbot is (and how it differs from an AI agent)

An AI chatbot is a program that holds a conversation using a large language model (LLM). Every time you send a message, the chatbot sends the whole conversation so far to the model and shows you its reply. That is all "memory" is at this level: a list of messages.

Rule-based chatbotAI (LLM) chatbotAI agent
Understands free textNo — keywords and buttonsYesYes
Answers from your dataOnly scripted answersWith RAGWith RAG
Takes actions (book, send, update)Only scripted flowsNoYes — calls tools in a loop
ExampleOld bank menu botsA WhatsApp FAQ botA bot that checks stock and places the order

This guide builds the middle column. The last step shows the one change that moves it to the right-hand column — see what are AI agents for the full picture.

Step 1: Set up

mkdir ai-chatbot && cd ai-chatbot
python -m venv .venv
# Windows: .venv\Scripts\activate   ·   macOS/Linux: source .venv/bin/activate
pip install -U openai streamlit python-dotenv
OPENAI_API_KEY=your-key-here
.env

Any provider with an OpenAI-compatible API works with the same code: set base_url and api_key on the client and change the model name. That includes Groq, Gemini's OpenAI-compatible endpoint and local models via Ollama (http://localhost:11434/v1).

Step 2: A terminal chatbot with memory

The system message sets the bot's personality and rules. The history list is the memory: append the user's message, call the model, append its reply, repeat.

from openai import OpenAI
from dotenv import load_dotenv

load_dotenv()
client = OpenAI()

history = [{
    "role": "system",
    "content": "You are a helpful study buddy for computer science students. "
               "Explain simply, with a short example. If you don't know, say so.",
}]

while True:
    user = input("You: ").strip()
    if user.lower() in {"exit", "quit"}:
        break
    history.append({"role": "user", "content": user})
    reply = client.chat.completions.create(model="gpt-4o-mini", messages=history)
    answer = reply.choices[0].message.content
    history.append({"role": "assistant", "content": answer})
    print("Bot:", answer)
chatbot.py

Ask "What is recursion?" then "Give me another example" — the second answer works because the first exchange is still in history.

Step 3: A web chatbot with Streamlit

Streamlit gives you a chat interface in a few lines. st.session_state keeps the history between messages, and stream=True makes the reply appear word by word.

import streamlit as st
from openai import OpenAI
from dotenv import load_dotenv

load_dotenv()
client = OpenAI()
st.title("Study Buddy 🤖")

if "history" not in st.session_state:
    st.session_state.history = [{"role": "system", "content": "You are a helpful, concise study buddy."}]

for msg in st.session_state.history[1:]:
    st.chat_message(msg["role"]).write(msg["content"])

if prompt := st.chat_input("Ask anything…"):
    st.session_state.history.append({"role": "user", "content": prompt})
    st.chat_message("user").write(prompt)
    stream = client.chat.completions.create(
        model="gpt-4o-mini", messages=st.session_state.history, stream=True
    )
    with st.chat_message("assistant"):
        answer = st.write_stream(
            chunk.choices[0].delta.content or "" for chunk in stream if chunk.choices
        )
    st.session_state.history.append({"role": "assistant", "content": answer})
app.py
streamlit run app.py

Step 4: Answer from your own documents

A general chatbot does not know your college's fee structure or your company's return policy. The simplest fix is to put a small knowledge file into the system message and tell the bot to answer only from it.

from pathlib import Path

knowledge = Path("faq.txt").read_text(encoding="utf-8")
SYSTEM = (
    "You answer questions for our shop using ONLY the FAQ below. "
    "If the answer is not in it, say you don't know and give the WhatsApp number.\n\n"
    f"FAQ:\n{knowledge}"
)

This works for a few pages. For hundreds of pages or PDFs, you need retrieval-augmented generation: split documents into chunks, store their embeddings in a vector database, and pass only the most relevant chunks to the model. That is covered step by step in RAG for AI agents.

Step 5: From chatbot to AI agent

A chatbot only talks. Give it tools — Python functions the model may call, like check_order(order_id) or book_slot(day, time) — and run a loop until it has an answer. Now it acts. That single change is what makes it an AI agent, and it is the subject of how to build an AI agent in Python.

Then, as the agent grows: LangChain to swap models and tools easily, LangGraph for memory, branching and human approval, MCP to plug into real systems, and a voice interface if your users would rather call than type.

Deploying your chatbot (web and WhatsApp)

  • Web: Streamlit Community Cloud, Hugging Face Spaces or any server that runs Python. Keep your API key in the host's secrets, never in the code.
  • WhatsApp: the WhatsApp Business Platform (Cloud API) sends each incoming message to your webhook; your server calls the chatbot and replies through the API.
  • Cost control: cap max_tokens, trim old history, and rate-limit users so one person cannot run up your bill.
  • Safety: users will try to make the bot ignore its rules (prompt injection). Never put secrets in the system message and never let a chatbot take actions without checks.

Learn to build chatbots and AI agents live

Chatbots are where AI Season starts, not where it stops. The 6 weeks AI agents bootcamp takes you from your first LLM call to RAG chatbots over your own documents, tool-using agents in LangGraph, MCP integrations, a voice calling agent and a deployed portfolio project — 12 live sessions in Urdu and English, recorded.

Cohort 02 starts 1st January 2027. Early-bird fee: PKR 3,000 (about ₹1,040) until 30th November 2026 — compared with tens of thousands of rupees for most chatbot and AI development courses. The top three scorers get the full fee back. See the full curriculum.

Frequently asked questions

How do I build an AI chatbot in Python?

Install the openai package, keep a list of messages that starts with a system message, and in a loop append the user's message, call the chat model with the whole list, and append the reply. Add Streamlit for a web interface and RAG to answer from your own documents.

Can I build an AI chatbot for free?

Yes. Python, Streamlit and LangChain are free, and you can run open models locally with Ollama or use free tiers from some API providers. Hosting a small Streamlit app on Streamlit Community Cloud or Hugging Face Spaces is also free.

What is the difference between a chatbot and an AI agent?

A chatbot answers in conversation. An AI agent also takes actions: it can call tools such as APIs, databases or calendars in a loop until a goal is done. Adding tool calling to a chatbot is what turns it into an agent.

How do I make a chatbot answer from my own documents?

For a few pages, include the text in the system message. For larger collections, use retrieval-augmented generation (RAG): split documents into chunks, embed them into a vector database, and pass only the most relevant chunks to the model with each question.

Is there an AI chatbot development course in Urdu or Hindi?

AI Season's live AI agents bootcamp teaches chatbots, RAG and full AI agents in Urdu (easy for Hindi speakers) with English code. Cohort 02 starts 1st January 2027; early-bird fee PKR 3,000.