Examples¶
The following examples are available to help you get started with ChatterBot.
Note
Before you run any example, you will need to install ChatterBot on your system. See the Setup guide for instructions.
All of these examples and more are available in the examples directory of the ChatterBot repository on GitHub.
Simple Example¶
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
# Create a new chat bot named Charlie
chatbot = ChatBot('Charlie')
trainer = ListTrainer(chatbot)
trainer.train([
"Hi, can I help you?",
"Sure, I'd like to book a flight to Iceland.",
"Your flight has been booked."
])
# Get a response to the input text 'I would like to book a flight.'
response = chatbot.get_response('I would like to book a flight.')
print(response)
Terminal Example¶
This example program shows how to create a simple terminal client that allows you to communicate with your chat bot by typing into your terminal.
![ChatterBot terminal example running in Python console](_images/terminal-example.gif)
from chatterbot import ChatBot
# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)
# NOTE: The order of logic adapters is important
# because the first logic adapter takes precedence
# if a good response cannot be determined.
# Create a new instance of a ChatBot
bot = ChatBot(
'Terminal',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
logic_adapters=[
'chatterbot.logic.BestMatch',
'chatterbot.logic.TimeLogicAdapter',
'chatterbot.logic.MathematicalEvaluation'
],
database_uri='sqlite:///database.sqlite3'
)
print('Type something to begin...')
# The following loop will execute each time the user enters input
while True:
try:
user_input = input()
bot_response = bot.get_response(user_input)
print(bot_response)
# Press ctrl-c or ctrl-d on the keyboard to exit
except (KeyboardInterrupt, EOFError, SystemExit):
break
Using MongoDB¶
Before you can use ChatterBot’s built in adapter for MongoDB, you will need to install MongoDB. Make sure MongoDB is running in your environment before you execute your program. To tell ChatterBot to use this adapter, you will need to set the storage_adapter parameter.
storage_adapter="chatterbot.storage.MongoDatabaseAdapter"
from chatterbot import ChatBot
# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)
# Create a new ChatBot instance
bot = ChatBot(
'Terminal',
storage_adapter='chatterbot.storage.MongoDatabaseAdapter',
logic_adapters=[
'chatterbot.logic.BestMatch'
],
database_uri='mongodb://localhost:27017/chatterbot-database'
)
print('Type something to begin...')
while True:
try:
user_input = input()
bot_response = bot.get_response(user_input)
print(bot_response)
# Press ctrl-c or ctrl-d on the keyboard to exit
except (KeyboardInterrupt, EOFError, SystemExit):
break
Time and Mathematics Example¶
ChatterBot has natural language evaluation capabilities that allow it to process and evaluate mathematical and time-based inputs.
from chatterbot import ChatBot
bot = ChatBot(
'Math & Time Bot',
logic_adapters=[
'chatterbot.logic.MathematicalEvaluation',
'chatterbot.logic.TimeLogicAdapter'
]
)
# Print an example of getting one math based response
response = bot.get_response('What is 4 + 9?')
print(response)
# Print an example of getting one time based response
response = bot.get_response('What time is it?')
print(response)
Using SQL Adapter¶
ChatterBot data can be saved and retrieved from SQL databases.
from chatterbot import ChatBot
# Uncomment the following lines to enable verbose logging
# import logging
# logging.basicConfig(level=logging.INFO)
# Create a new instance of a ChatBot
bot = ChatBot(
'SQLMemoryTerminal',
storage_adapter='chatterbot.storage.SQLStorageAdapter',
database_uri=None,
logic_adapters=[
'chatterbot.logic.MathematicalEvaluation',
'chatterbot.logic.TimeLogicAdapter',
'chatterbot.logic.BestMatch'
]
)
# Get a few responses from the bot
bot.get_response('What time is it?')
bot.get_response('What is 7 plus 7?')
Read only mode¶
Your chat bot will learn based on each new input statement it receives. If you want to disable this learning feature after your bot has been trained, you can set read_only=True as a parameter when initializing the bot.
chatbot = ChatBot("Johnny Five", read_only=True)
Django and Flask¶
Django: A number of example views are documented, as well as full example Django app.
Flask: There is a separately maintained example Flask project using ChatterBot.