Conversations

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.

Conversation scope

If two 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.

Conversation example

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)

Statements

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.

class chatterbot.conversation.Statement(text, **kwargs)[source]

A statement represents a single spoken entity, sentence or phrase that someone can say.

confidence = None

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.

add_extra_data(key, value)[source]

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’)]
Parameters:
  • key (str) – The key to use in the dictionary of extra data.
  • value – The value to set for the specified key.
add_response(response)[source]

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.
get_response_count(statement)[source]

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
remove_response(response_text)[source]

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.
response_statement_cache

This property is to allow ChatterBot Statement objects to be swappable with Django Statement models.

save()[source]

Save the statement in the database.

serialize()[source]
Returns:A dictionary representation of the statement object.
Return type:dict

Responses

ChatterBot’s response objects represent the relationship between two statements. A response indicates that one statement was issued in response to another statement.

class chatterbot.conversation.Response(text, **kwargs)[source]

A response represents an entity which response to a statement.

Statement-response relationship

ChatterBot stores knowledge of conversations as statements. Each statement can have any number of possible responses.

ChatterBot statement-response relationship

Each 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. The in_response_to attribute is essentially a reference to all parent statements of the current statement.

ChatterBot statement relationship

The Response object’s 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.