ChatterBot supports the ability to have multiple concurrent conversations. A conversations is where the chat bot interacts with a person, and supporting multiple concurrent conversations means that the chat bot can have multiple different conversations with different people at the same time.
ChatBot instances are created, each will have conversations separate from each other.
An adapter can access any conversation as long as the unique identifier for the conversation is provided.
The following example is taken from the Django
ChatterBotView built into ChatterBot.
In this method, the unique identifiers for each chat session are being stored in Django’s
session objects. This allows different users who interact with the bot through different
web browsers to have separate conversations with the chat bot.
def post(self, request, *args, **kwargs): """ Return a response to the statement in the posted data. """ input_data = json.loads(request.read().decode('utf-8')) self.validate(input_data) conversation = self.get_conversation(request) response = self.chatterbot.get_response(input_data, conversation.id) response_data = response.serialize() return JsonResponse(response_data, status=200)
ChatterBot’s statement objects represent either an input statement that the chat bot has received from a user, or an output statement that the chat bot has returned based on some input.
A statement represents a single spoken entity, sentence or phrase that someone can say.
ChatterBot’s logic adapters assign a confidence score to the statement before it is returned. The confidence score indicates the degree of certainty with which the chat bot believes this is the correct response to the given input.
InvalidTypeException(value='Received an unexpected value type.')¶
This method allows additional data to be stored on the statement object.
Typically this data is something that pertains just to this statement. For example, a value stored here might be the tagged parts of speech for each word in the statement text.
- key = ‘pos_tags’
- value = [(‘Now’, ‘RB’), (‘for’, ‘IN’), (‘something’, ‘NN’), (‘different’, ‘JJ’)]
- key (str) – The key to use in the dictionary of extra data.
- value – The value to set for the specified key.
Add the response to the list of statements that this statement is in response to. If the response is already in the list, increment the occurrence count of that response.
Parameters: response (Response) – The response to add.
Find the number of times that the statement has been used as a response to the current statement.
Parameters: statement (Statement) – The statement object to get the count for. Returns: Return the number of times the statement has been used as a response. Return type: int
Removes a response from the statement’s response list based on the value of the response text.
Parameters: response_text (str) – The text of the response to be removed.
This property is to allow ChatterBot Statement objects to be swappable with Django Statement models.
Save the statement in the database.
ChatterBot’s response objects represent the relationship between two statements. A response indicates that one statement was issued in response to another statement.
A response represents an entity which response to a statement.
ChatterBot stores knowledge of conversations as statements. Each statement can have any number of possible responses.
Statement object has an
in_response_to reference which links the
statement to a number of other statements that it has been learned to be in response to.
in_response_to attribute is essentially a reference to all parent statements
of the current statement.
occurrence attribute indicates the number of times
that the statement has been given as a response. This makes it possible for the chat bot
to determine if a particular response is more commonly used than another.