This repository contains code related to the tutorial on building a Python chatbot with ChatterBot.
Create and activate a virtual environment, then install the necessary dependencies:
$ python -m venv venv
$ source venv/bin/activate
(venv) $ python -m pip install -r requirements.txt
(venv) $ python -m spacy download en_core_web_smStarting 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:
(venv) $ cd source_code_final
(venv) $ python trainer.py # (optional) retrain from chat.txt
(venv) $ python bot.pyA 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:
> hi
🪴 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.
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 trainingsource_code_step_2/— addsListTrainerwith a couple of sample exchangessource_code_step_3/— includes the WhatsAppchat.txtexportsource_code_step_4/— addscleaner.pyfor preprocessing the chat exportsource_code_step_5/— splits training intotrainer.pywhich trains on the cleaned chat datasource_code_step_6/— adds a local LLM viaOllamaLogicAdapter(requires Ollama)source_code_final/— same as step 6
Step 6 and the source_code_final/ folder use ChatterBot's experimental Ollama integration. To try it, install Ollama on your system and pull a small model:
$ ollama pull llama3.2:latestThen 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.