Login With Github

ChatterBot Supports For Integration With Django

ChatterBot has direct support for integration with Django. ChatterBot provides out of the box models and endpoints that allow you build ChatterBot powered Django applications.

Chatterbot Django Settings

You can edit the ChatterBot configuration through your Django settings.py file.

CHATTERBOT = {
    'name': 'Tech Support Bot',
    'logic_adapters': [
        'chatterbot.logic.MathematicalEvaluation',
        'chatterbot.logic.TimeLogicAdapter',
        'chatterbot.logic.BestMatch'
    ],
    'trainer': 'chatterbot.trainers.ChatterBotCorpusTrainer',
    'training_data': [
         'chatterbot.corpus.english.greetings'
    ]
}

Any setting that gets set in the CHATTERBOT dictionary will be passed to the chat bot that powers your django app.

Additional Django settings

django_app_name [default: 'django_chatterbot'] The Django app name to look up the models from.

Django Training

Management command

When using ChatterBot with Django, the training process can be executed by running the training management command.

python manage.py train

Training settings

You can specify any data that you want to be passed to the chat bot trainer in the training_dataparameter in your CHATTERBOT Django settings.

CHATTERBOT = {
    # ...
    'trainer': 'chatterbot.trainers.ChatterBotCorpusTrainer',
    'training_data': [
         'chatterbot.corpus.english.greetings'
    ]
}

Note:You can also specify paths to corpus files or directories of corpus files in the training_data list.

See the documentation for the Training classes for other training class options that can be used here.

ChatterBot Django Views

API Views

ChatterBot's django module comes with a pre-built API view that you can make requests against to communicate with your bot from your web application.

The endpoint expects a JSON request with the following data:

{"text": "My input statement"}

Note:You will need to include ChatterBot's urls in your django url configuration before you can make requests to these views. See the setup instructions for more details.

Webservices

If you want to host your Django app, you need to choose a method through which it will be hosted. There are a few free services that you can use to do this such as Heroku and PythonAnyWhere.

WSGI

A common method for serving Python web applications involves using a Web Server Gateway Interface (WSGI) package.

Gunicorn is a great choice for a WSGI server. They have detailed documentation and installation instructions on their website.

Hosting static files

There are numerous ways to host static files for your Django application. One extreemly easy way to do this is by using WhiteNoise, a python package designed to make it possible to serve static files from just about any web application.

Install packages

Begin by making sure that you have installed both django and chatterbot.

pip install django chatterbot

For more details on installing Django, see the Django documentation.

Installed Apps

Add chatterbot.ext.django_chatterbot to your INSTALLED_APPS

INSTALLED_APPS = (
    # ...
    'chatterbot.ext.django_chatterbot',
)

API view

If you need an API endpoint for your chat bot you can add the following to your Django urls.py file. You can also choose to create your own views and end endpoints as needed.

urlpatterns = patterns(
    ...
    url(
        r'^chatterbot/',
        include('chatterbot.ext.django_chatterbot.urls',
        namespace='chatterbot')
    ),
)

Migrations

You can run the Django database migrations for your chat bot with the following command.

python manage.py migrate django_chatterbot

Note:Looking for a working example? Check our the example Django app using ChatterBot on GitHub: https://github.com/gunthercox/ChatterBot/tree/master/examples/django_app

MongoDB and Django

ChatterBot has a storage adapter for MongoDB but it does not work with Django. If you want to use MongoDB as your database for Django and your chat bot then you will need to install a Django storage backend such as Django MongoDB Engine.

The reason this is required is because Django's storage backends are different and completely separate from ChatterBot's storage adapters.

0 Comment

temp