File tree Expand file tree Collapse file tree
examples/django_example/django_example Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -15,7 +15,7 @@ Begin by making sure that you have installed both ``django`` and ``chatterbot``.
1515
1616 pip install django chatterbot
1717
18- For more details on installing Django, see the `Django documentation `_.
18+ For more details on installing and using Django, see the `Django documentation `_.
1919
2020Installed Apps
2121--------------
Original file line number Diff line number Diff line change 1- =======================
2- ChatterBot Django Views
3- =======================
1+ =============================
2+ ChatterBot Django Sample Code
3+ =============================
44
55.. note ::
66
@@ -25,3 +25,16 @@ The endpoint expects a JSON request in the following format:
2525 :caption: examples/django_example/django_example/views.py
2626 :language: python
2727 :pyobject: ChatterBotApiView
28+
29+
30+ Example Django Management Commands
31+ ==================================
32+
33+ ChatterBot's Django example includes a management command that
34+ demonstrates a simple example of training. This can be used as
35+ a basis for other custom management commands used with other
36+ :ref: `training options <Training >`.
37+
38+ .. literalinclude :: ../../examples/django_example/django_example/management/commands/train.py
39+ :caption: examples/django_example/django_example/management/commands/train.py
40+ :language: python
Original file line number Diff line number Diff line change 1+ """
2+ This is an example of a custom Django management command that
3+ trains a ChatterBot instance with specified data.
4+
5+ For more information on how to create custom management commands,
6+ see the Django documentation:
7+ https://docs.djangoproject.com/en/4.2/howto/custom-management-commands/
8+
9+ For details on the available training options for ChatterBot see:
10+ http://docs.chatterbot.us/training/
11+ """
12+
13+ from django .core .management .base import BaseCommand
14+ from django .conf import settings
15+
16+ from chatterbot import ChatBot
17+ from chatterbot .trainers import ListTrainer
18+
19+
20+ class Command (BaseCommand ):
21+ help = 'Train a ChatterBot instance with specified data.'
22+
23+ def handle (self , * args , ** options ):
24+ chatbot = ChatBot (** settings .CHATTERBOT )
25+
26+ trainer = ListTrainer (chatbot )
27+
28+ trainer .train ([
29+ 'Hello, how are you?' ,
30+ 'I am good.' ,
31+ 'That is good to hear.' ,
32+ 'I am glad to hear that.' ,
33+ 'Thank you.' ,
34+ 'You are welcome.' ,
35+ ])
36+
37+ self .stdout .write (
38+ self .style .SUCCESS ('Training completed successfully' )
39+ )
Original file line number Diff line number Diff line change 3939 'django.contrib.staticfiles' ,
4040
4141 'chatterbot.ext.django_chatterbot' ,
42+
43+ # Our example app:
44+ 'django_example' ,
4245]
4346
4447# ChatterBot settings
Original file line number Diff line number Diff line change 22from django .views .generic .base import TemplateView
33from django .views .generic import View
44from django .http import JsonResponse
5+ from django .conf import settings
6+
57from chatterbot import ChatBot
6- from chatterbot .ext .django_chatterbot import settings
78
89
910class ChatterBotAppView (TemplateView ):
You can’t perform that action at this time.
0 commit comments