# How to Build an AI Chatbot in Python

URL: https://aiseason.tech/how-to-build-an-ai-chatbot

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.

| Fact | Value |
| --- | --- |
| 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](https://aiseason.tech/how-to-build-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](https://aiseason.tech/what-is-an-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 chatbot | AI (LLM) chatbot | AI agent |
| --- | --- | --- | --- |
| Understands free text | No — keywords and buttons | Yes | Yes |
| Answers from your data | Only scripted answers | With RAG | With RAG |
| Takes actions (book, send, update) | Only scripted flows | No | Yes — calls tools in a loop |
| Example | Old bank menu bots | A WhatsApp FAQ bot | A 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](https://aiseason.tech/what-are-ai-agents) for the full picture.

### Step 1: Set up

```bash
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
```

```bash
OPENAI_API_KEY=your-key-here
```

> 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.

```python
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)
```

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.

```python
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})
```

```bash
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.

```python
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](https://aiseason.tech/vector-database), and pass only the most relevant chunks to the model. That is covered step by step in [RAG for AI agents](https://aiseason.tech/rag-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](https://aiseason.tech/how-to-build-an-ai-agent).

Then, as the agent grows: [LangChain](https://aiseason.tech/langchain-tutorial) to swap models and tools easily, [LangGraph](https://aiseason.tech/langgraph-tutorial) for memory, branching and human approval, [MCP](https://aiseason.tech/mcp-course) to plug into real systems, and a [voice interface](https://aiseason.tech/voice-ai-agent) 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](https://aiseason.tech/) 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](https://aiseason.tech/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.

---

_Last updated: 2026-10-23. Canonical page: https://aiseason.tech/how-to-build-an-ai-chatbot. Source: AI Season (https://aiseason.tech/)._
