Chatterbot: Intent and prompts

Created on 26 Sep 2016  路  14Comments  路  Source: gunthercox/ChatterBot

Hi Gunther,

We are doing intent classification with a custom Logic adapter. The intent will match for Book a flight ticket => book_ticket .

The matched Logic adapter will respond with What is your destination . Let's say a user types a random word asdadasd, is there anyway for me to go back to the Logic adapter and prompt What is your destination . Can conversations remain in the Logic adapter? I only want to exit once the book_ticket intent is complete

enhancement

Most helpful comment

Awesome. I have written a basic IntentClassifier for book_flight intent. Am I going in the right direction? Is there a better way?

from chatterbot.adapters.logic import LogicAdapter
from chatterbot.conversation import Statement
from textblob.classifiers import NaiveBayesClassifier

class IntentClassification(LogicAdapter):
    def __init__(self, **kwargs):
        super(IntentClassification, self).__init__(**kwargs)

        training_data = [
            ("Book me a ticket", 1),
            ("Book me a ticket to Singapore", 1),
            ("it is time to go to sleep", 0),
            ("what is your favorite color", 0),
            ("i had a great time", 0),
            ("what is", 0)
        ]
        self.classifier = NaiveBayesClassifier(training_data)
        self.intent_name = 'book_flight'

    def can_process(self, statement):
        return self.context.intent is None or self.context.intent == self.intent_name
    def process(self, statement):
        # If the intent is active
        if self.context.intent == self.intent_name:
            # Asks for destination if empty, or Book tickets
            self.context.intent = None
            return 1, Statement('We have booked your tickets')
        else:
            # Check if user's statement matches the intent
            confidence = self.classifier.classify(statement.text.lower())
            if confidence > 0:
                self.context.intent = self.intent_name
                destination_exists = 'singapore' in statement.text.lower()
                if destination_exists:
                    self.context.intent = None
                    return confidence, Statement('We have booked your flight tickets to singapore')
                else:
                    return confidence, Statement('Where are you fying to?')
            else:
                self.context.intent = None
                return confidence, Statement(None)

All 14 comments

This should be possible to do, but you will need to handle the state of this interaction within your custom logic adapter.

Awesome. I have written a basic IntentClassifier for book_flight intent. Am I going in the right direction? Is there a better way?

from chatterbot.adapters.logic import LogicAdapter
from chatterbot.conversation import Statement
from textblob.classifiers import NaiveBayesClassifier

class IntentClassification(LogicAdapter):
    def __init__(self, **kwargs):
        super(IntentClassification, self).__init__(**kwargs)

        training_data = [
            ("Book me a ticket", 1),
            ("Book me a ticket to Singapore", 1),
            ("it is time to go to sleep", 0),
            ("what is your favorite color", 0),
            ("i had a great time", 0),
            ("what is", 0)
        ]
        self.classifier = NaiveBayesClassifier(training_data)
        self.intent_name = 'book_flight'

    def can_process(self, statement):
        return self.context.intent is None or self.context.intent == self.intent_name
    def process(self, statement):
        # If the intent is active
        if self.context.intent == self.intent_name:
            # Asks for destination if empty, or Book tickets
            self.context.intent = None
            return 1, Statement('We have booked your tickets')
        else:
            # Check if user's statement matches the intent
            confidence = self.classifier.classify(statement.text.lower())
            if confidence > 0:
                self.context.intent = self.intent_name
                destination_exists = 'singapore' in statement.text.lower()
                if destination_exists:
                    self.context.intent = None
                    return confidence, Statement('We have booked your flight tickets to singapore')
                else:
                    return confidence, Statement('Where are you fying to?')
            else:
                self.context.intent = None
                return confidence, Statement(None)

It looks like you are going in the right direction.

Let me know if you have any feedback or thoughts on the current setup of logic adapters. I originally designed them for just processing responses to a user's input but I am seeing a lot of developers creating action-style logic adapters. I'm interested in making changes to better accommodate multi-step workflows like the one you are working on.

Sure. Thanks. I would be interested in having a persist flag to indicate if a conversation should remain in a Logic adapter.

If the persist flag is false, then only that Logic adapter will handle all future conversations.

What do you think?

Basically i am trying to build something similar to wit.ai's branching model when some of the extracted entities or context variables are missing.

Another issue i am facing is sometimes i need to save Context variables for a specific amount of time for a future conversation.

For eg:

user: What is the weather in Singapore?
bot: It will be sunny
user: How about tomorrow
bot: Sunny too
user: How about the traffic? (Matches traffic_intent logic adapter)
bot: It will always congested.

In this case, i have to save location context which can be re-used in a different logic adapter.

I can save it externally, but would be good if Chatterbot handles per-user settings and saving of context internally.

@gunthercox

I was wondering if chatterbot can have Topic based conversations in the core. Similar to

https://github.com/superscriptjs/superscript/wiki/Topics

Or should we just use the logic adapter pattern to do this? There is also another chatbot that implements Topics

https://github.com/open-intent-io/open-intent

@rmdort It looks like topics have to be set manual in these implementations. I have a few notes on the subject which I can create a ticket for, but I'm wondering if a setup where the chat bot learns to group similar conversations together by "topic" might be more flexible.

Is there a use case for setting topics manually? From my understanding, the goal of a topic is just to group similar conversation data together so make responses more coherent.

It would be good to see your notes on this. I am not sure how would you make the bot learn to group conversations into a topic.

I was thinking topics to be manually created ones, and conversations are tagged to a topic. But it will be interesting to see your POV

A few features off the top of my head

  1. Tagging conversations to topics.
  2. Branching between conversations inside a topic
  3. Branching from one topic to another

Hi @rmdort and @gunthercox, I've been looking for the way to create this intent and contextual chatting in this chatterbot. any updates on this?

Cheers,

+1

+1

@rmdort - Did u try using wit.ai with chatterbot ? OR api.ai etc for intent and entities?
I know API.AI is paid but just for the idea. Because my requirement is kind of same as yours. Or did u manage to built your own version using open-intent? https://github.com/open-intent-io/open-intent

I appreciate everyone's discussion regarding the subject of "intents" and "entities". I hope that It's alright but I'm going to close this ticket off. I don't have any plans to build new functionality into ChatterBot related to this within the next year. My main focus is going to be on improving the quality and accuracy of the dialog that the chat bot can produce.

@rmdort were you able to create a logic adapter for this?

Was this page helpful?
0 / 5 - 0 ratings