ChatterBot
ChatterBot copied to clipboard
Training Django ChatterBot
Hi,
since the command (with the new version of ChatterBot) python3 manage.py train is no more supported, how can I train my online bot implemented with Django? I read that I have to create a new python file train.py, but I tried and I didn't solve the problem.
This is part of my settings.py file:
CHATTERBOT = {
'name': 'My Online Bot',
'django_app_name': 'django_chatterbot',
'storage_adapter' : 'chatterbot.storage.SQLStorageAdapter',
'database_uri' : 'sqlite:///online_bot_database.sqlite3',
'logic_adapters' : [
{
'import_path': 'chatterbot.logic.BestMatch',
'statement_comparison_function': 'chatterbot.comparisons.levenshtein_distance',
'response_selection_method': get_first_response
},
]
}
How to properly set up the train.py file for this bot?
Thanks in advance,
Alan
I have the same problem. I can not figure out how to train chatterbot with django.
Me too
Me neither, I can't train it. The command is deprecated.
> python manage.py train
Unknown command: 'train'
Type 'manage.py help' for usage.

I checked the requirements
django>=2.1,<2.2
# chatterbot>=0.8,<1.1
> python -m chatterbot --version
1.0.4
> python -m django --version
2.1.7
I tried to run the migration command but of course their is nothing to migrate
> python manage.py migrate django_chatterbot
[nltk_data] Downloading package averaged_perceptron_tagger to
[nltk_data] C:\Users\Kim NOEL\AppData\Roaming\nltk_data...
[nltk_data] Package averaged_perceptron_tagger is already up-to-
[nltk_data] date!
[nltk_data] Downloading package punkt to C:\Users\Kim
[nltk_data] NOEL\AppData\Roaming\nltk_data...
[nltk_data] Package punkt is already up-to-date!
[nltk_data] Downloading package stopwords to C:\Users\Kim
[nltk_data] NOEL\AppData\Roaming\nltk_data...
[nltk_data] Package stopwords is already up-to-date!
Operations to perform:
Apply all migrations: django_chatterbot
Running migrations:
No migrations to apply.
Please help ^^' @gunthercox
Try this:
train.py
**
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
chatbot = ChatBot('Chatterbot',trainer='chatterbot.trainers.CorpusTrainer', storage_adapter='chatterbot.storage.MongoDatabaseAdapter', database_uri='mongodb://localhost:27017/chatbot' )
trainer = ChatterBotCorpusTrainer(chatbot)
trainer.train( './file.yml' )
**
settings.py
**
CHATTERBOT = {
'name': 'Chatterbot',
'trainer': 'chatterbot.trainers.ChatterBotCorpusTrainer',
'storage_adapter' : 'chatterbot.storage.MongoDatabaseAdapter',
'database_uri' : 'mongodb://localhost:27017/chatbot',
'database': 'chatbot',
'django_app_name': 'django_chatterbot',
'preprocessors': [
'chatterbot.preprocessors.clean_whitespace',
'chatterbot.preprocessors.convert_to_ascii'
],
'read_only' : 'TRUE' }
**
Then run:
1. python train.py
2. python manage.py migrate
3. python manage.py runserver 0.0.0.0:8000
Thank you @haycarlitos, but it still doesn't work. I am in the same initial situation. The bot continues to reply with the input it receives ("Hello" --> "Hello"). It also seems to learn from the user's input: so, though the bot was trained with the corpus, it behaves like its knowledge base is empty. I also try with a SQL database and by deleting the "read_only" setting, same problem.
@haycarlitos did it work for you? @QuAlan996 hasn't had much luck with it, it seems.
I've only managed to get the Django 'congratulations' page with the rocket taking off. Can anyone point me in the right direction to set it up correctly?
Kindly let us know if anyone gets this to work
Working example for train command - https://bitbucket.org/voron-raven/chat/src/master/core/management/commands/train.py
@haycarlitos's solution worked for me.
I used sqlite, Here is my code
In Settings.py file
CHATTERBOT = {
'name': 'Chatterbot',
'trainer': 'chatterbot.trainers.ChatterBotCorpusTrainer',
'storage_adapter' : 'chatterbot.storage.SQLStorageAdapter',
'database_uri' : 'sqlite:///database.db',
'database': 'chatbot',
'django_app_name': 'django_chatterbot',
'preprocessors': [
'chatterbot.preprocessors.clean_whitespace',
'chatterbot.preprocessors.convert_to_ascii'
],
'read_only' : 'TRUE'
}
In train.py file
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
chatbot = ChatBot('Chatterbot',
trainer='chatterbot.trainers.CorpusTrainer',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database_uri='sqlite:///database.db')
trainer = ChatterBotCorpusTrainer(chatbot)
trainer.train(
"chatterbot.corpus.english.greetings",
"chatterbot.corpus.bangla.botprofile",
"chatterbot.corpus.buses.route3",
"chatterbot.corpus.buses.broute",
"chatterbot.corpus.english.conversations",
)
Since I used django 3.0.2, I changed {% load staticfiles %} to {% load static %} in templates/app.html & templates/nav.html file.
I found a way to train Django chatterbot. I train it using ChatterBotCorpusTrainer but haven't integrated it into Django yet. So the idea is to create a database to store conversations and categories, get that data and transform it into a YAML file then use it to train ChatterBotCorpusTrainer after you had done that you can ask ChatterBot anything, you can add more data to your training data. :), this is not a solution, this is a workaround. I hope ChatterBot give more example documentation about integrating to Django. Note: There is a bug in chatterbot, somehow when you train your YAML file, the last YAML file will be deleted (it's content) and it will give you an error of empty [ ] and it will not be used to train your ChatterBot. I had checked so many times if I had made a mistake, but can't find it. Then again, I do some workaround by creating a fake YAML file in the last data to train ChatterBot. Hope it helps.
This is working
Add this code in settings.py file
CHATTERBOT = {
'name': 'Tech Bot',
'trainer': 'chatterbot.trainers.ChatterBotCorpusTrainer',
'storage_adapter':'chatterbot.storage.SQLStorageAdapter',
'database_uri':'sqlite:///database.sqlite3',
'database': 'chatbot',
'django_app_name': 'django_chatterbot',
'preprocessors': [
'chatterbot.preprocessors.clean_whitespace',
],
'read_only' : 'TRUE',
'logic_adapters': [
'chatterbot.logic.MathematicalEvaluation',
'chatterbot.logic.TimeLogicAdapter',
'chatterbot.logic.BestMatch'
],
}
Add this code in train.py file
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
chatbot = ChatBot(
"Tech Bot",
trainer = 'chatterbot.trainers.CorpusTrainer',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database_uri='sqlite:///database.sqlite3',
)
trainer = ChatterBotCorpusTrainer(chatbot)
trainer.train(
"chatterbot.corpus.english"
)
trainer.export_for_training('./my_export.json') ##to show training data in json file
Run Following commands:
python train.py python manage.py migrate python manage.py runserver
For me, using the SQLStorageAdapter was not good enough because then the data would not show up in Django admin, but using a separate train.py with the chatterbot.storage.DjangoStorageAdapter that the chatterbot.ext.django_chatterbot app uses resulted in a django.core.exceptions.AppRegistryNotReady: Models aren't loaded yet. error.
I did get it working by creating a custom manage.py command in my own Django application using the folder structure described in https://docs.djangoproject.com/en/3.2/howto/custom-management-commands/
and with the following management/commands/train.py
from django.core.management.base import BaseCommand, CommandError
from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer
class Command(BaseCommand):
help = 'Train the chatbot with a corpus'
def handle(self, *args, **options):
chatbot = ChatBot(
'ChatBot name from settings.py CHATTERBOT',
storage_adapter='chatterbot.storage.DjangoStorageAdapter',
)
trainer = ChatterBotCorpusTrainer(chatbot)
trainer.train(
"chatterbot.corpus.english"
)
self.stdout.write(self.style.SUCCESS('Successfully trained!'))
Edit: and then finally you can use python3 manage.py train again :)
For me, using the SQLStorageAdapter was not good enough because then the data would not show up in Django admin, but using a separate train.py with the
chatterbot.storage.DjangoStorageAdapterthat thechatterbot.ext.django_chatterbotapp uses resulted in adjango.core.exceptions.AppRegistryNotReady: Models aren't loaded yet.error.I did get it working by creating a custom manage.py command in my own Django application using the folder structure described in https://docs.djangoproject.com/en/3.2/howto/custom-management-commands/
and with the following management/commands/train.py
from django.core.management.base import BaseCommand, CommandError from chatterbot import ChatBot from chatterbot.trainers import ChatterBotCorpusTrainer class Command(BaseCommand): help = 'Train the chatbot with a corpus' def handle(self, *args, **options): chatbot = ChatBot( 'ChatBot name from settings.py CHATTERBOT', storage_adapter='chatterbot.storage.DjangoStorageAdapter', ) trainer = ChatterBotCorpusTrainer(chatbot) trainer.train( "chatterbot.corpus.english" ) self.stdout.write(self.style.SUCCESS('Successfully trained!'))Edit: and then finally you can use
python3 manage.py trainagain :)
Thanks, that really helpful to me. This way it train data directly into django_chatterbot.