Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 33 additions & 16 deletions chatterbot/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,53 @@ Create and activate a [virtual environment](https://realpython.com/python-virtua
$ python -m venv venv
$ source venv/bin/activate
(venv) $ python -m pip install -r requirements.txt
(venv) $ python -m spacy download en_core_web_sm
```

Then you can navigate into the folder `source_code_final/` and train and start the interactive command-line interface chatbot by running `bot.py`:
Starting in `source_code_step_5/`, training is handled by a dedicated script, `trainer.py`, that reads from `chat.txt` and populates `db.sqlite3`. The interactive chatbot lives in `bot.py`:

```sh
(venv) $ cd source_code_final
(venv) $ python trainer.py # (optional) retrain from chat.txt
(venv) $ python bot.py
```

A pretrained `db.sqlite3` is included in each step folder, so you can run `bot.py` right away without retraining.

After training, you'll see an interactive prompt that you can chat with:

```text
> hi
🪴 Welcome, friend 🤗
> thanks for the green welcome
🪴 I let you
> you let me be here?
🪴 It's a monsters!
> did you mean monstera?
🪴 The leafs that she had are getting dryer and dryer. But she’s also growing plenty of new ones
> who?
🪴 Do raindrops touch their leaves?
> very philosophical!
🪴 Lol
> ;)
🪴 I don't grow any crop at home
> no crops in pots
🪴 Ah, gotcha!
🪴 Hi! How's your day going so far?
> Do you talk to your plants?
🪴 I do!
> What do they like to hear?
🪴 Motivational speeches
> Do plants need sunlight?
🪴 Yes, most plants need sunlight to undergo photosynthesis,
which is how they make their own food. How's your indoor garden doing?
```

The bot will learn from the replies you give and improve its accuracy. You can quit the interactive prompt by typing any of the `exit_conditions` defined in `bot.py`.

## Folder Structure

You'll find the code for each step of the tutorial in a separate folder. The folders also include a SQLite database that contains the different phases of training at each step. Because of this, you can inspect the project at different stages and notice how it evolves when you add more data and interactions.

- `source_code_step_1/` — minimal chatbot with no training
- `source_code_step_2/` — adds `ListTrainer` with a couple of sample exchanges
- `source_code_step_3/` — includes the WhatsApp `chat.txt` export
- `source_code_step_4/` — adds `cleaner.py` for preprocessing the chat export
- `source_code_step_5/` — splits training into `trainer.py` which trains on the cleaned chat data
- `source_code_step_6/` — adds a local LLM via `OllamaLogicAdapter` (requires Ollama)
- `source_code_final/` — same as step 6

## Using the Ollama integration (step 6 and final)

Step 6 and the `source_code_final/` folder use ChatterBot's experimental [Ollama](https://ollama.com/) integration. To try it, [install Ollama](https://realpython.com/ollama/) on your system and pull a small model:

```sh
$ ollama pull llama3.2:latest
```

Then run `bot.py` as usual. If you don't want to use Ollama, remove the `OllamaLogicAdapter` entry from the `logic_adapters` list in `bot.py`.
65 changes: 50 additions & 15 deletions chatterbot/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,50 @@
ChatterBot==1.0.4
chatterbot-corpus==1.2.0
click==8.1.3
joblib==1.1.0
mathparse==0.1.2
nltk==3.7
Pint==0.19.2
pymongo==3.12.3
python-dateutil==2.7.5
pytz==2022.2.1
PyYAML==3.13
regex==2022.9.11
six==1.16.0
SQLAlchemy==1.2.19
tqdm==4.64.1
annotated-doc==0.0.4
annotated-types==0.7.0
anyio==4.13.0
blis==1.3.3
catalogue==2.0.10
certifi==2026.2.25
charset-normalizer==3.4.7
chatterbot==1.2.13
click==8.3.2
cloudpathlib==0.23.0
confection==1.3.3
cymem==2.0.13
h11==0.16.0
httpcore==1.0.9
httpx==0.28.1
idna==3.11
jinja2==3.1.6
markdown-it-py==4.0.0
markupsafe==3.0.3
mathparse==0.2.8
mdurl==0.1.2
murmurhash==1.0.15
numpy==2.4.4
ollama==0.6.1
packaging==26.1
preshed==3.0.13
pydantic==2.13.2
pydantic-core==2.46.2
pygments==2.20.0
python-dateutil==2.9.0.post0
requests==2.33.1
rich==15.0.0
setuptools==82.0.1
shellingham==1.5.4
six==1.17.0
smart-open==7.6.0
spacy==3.8.13
spacy-legacy==3.0.12
spacy-loggers==1.0.5
sqlalchemy==2.0.49
srsly==2.5.3
thinc==8.3.13
tqdm==4.67.3
typer==0.24.1
typing-extensions==4.15.0
typing-inspection==0.4.2
urllib3==2.6.3
wasabi==1.1.3
weasel==1.0.0
wrapt==2.1.2
23 changes: 13 additions & 10 deletions chatterbot/source_code_final/bot.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
from chatterbot.trainers import ListTrainer
from cleaner import clean_corpus

from chatterbot import ChatBot

CORPUS_FILE = "chat.txt"

chatbot = ChatBot("Chatpot")

trainer = ListTrainer(chatbot)
cleaned_corpus = clean_corpus(CORPUS_FILE)
trainer.train(cleaned_corpus)
chatbot = ChatBot(
"Chatpot",
logic_adapters=[
{
"import_path": "chatterbot.logic.BestMatch",
},
{
"import_path": "chatterbot.logic.OllamaLogicAdapter",
"model": "llama3.2:latest",
"host": "http://localhost:11434",
},
],
)

exit_conditions = (":q", "quit", "exit")
while True:
Expand Down
Loading
Loading