superscriptjs / superscript

A dialogue engine for creating chat bots
http://superscriptjs.com
MIT License
1.65k stars 209 forks source link

Topic switch does not work during coversation #375

Open alexraj opened 6 years ago

alexraj commented 6 years ago

When the topic is switched, and the first gambit is a conversation, it always matches the first topic, rather than switched topic. Consider the following gambits.

+ I love animals
- Me too {topic=animals}
+ I love pets
- Me too {topic=pets}
> topic animals {keep}
  + My favorite is cats
  - cats are scary
< topic
> topic pets {keep}
  + My favorite is cats
  - cats are fun!!
< topic

and

+ I love animals
- Me too, what is your favorite? {topic=animals}
+ I love pets
- Me too, what is your favorite? {topic=pets}
> topic animals {keep}
  + *1
  % * what is your favorite? *
  - <cap1> are scary
< topic
> topic pets {keep}
  + *1
  % * what is your favorite? *
  - <cap1> are fun!!
< topic

Expected Behavior The first one works as expected. The conversation is as follows. Bot> Hi, How can I help you? You> i love pets Bot> Me too You> my favorite is cats Bot> cats are fun!!

For the second one, it always matches the "animals" topic. It should say "cats are fun!!" as a last reply. The conversation is as follows.

Bot> Hi, How can I help you? You> I love pets Bot> Me too, what is your favorite? You> cats Bot> cats are scary

Current Behavior The topic switch doesn't happen, and it matches the first one irrespective of topic, instead of matching the current topic, which is "pets" Steps to Reproduce (for bugs) Try out the two sets of Gambits.

Context We are creating a chatops, where the first question asks for environment, the rest of the conversation depends on the first question, that determines the topic.

Your Environment

silentrob commented 6 years ago

Interesting, So I think what is happening here is that the conversation simply isn't working because % * what is your favorite? * needs to be in the same topic as the last reply. And + *1 matches on cats and uses the first match found.

There are some work arounds for this.

I didn't test this but it should work.

+ I love animals
- ^topicRedirect("animals", "__favorite__")

+ I love pets
- ^topicRedirect("pets", "__favorite__")

> topic animals {keep}
  + __favorite__
  - Me too, what is your favorite?

  + *1
  % * what is your favorite? *
  - <cap1> are scary
< topic

> topic pets {keep}
  + __favorite__
  - Me too, what is your favorite?

  + *1
  % * what is your favorite? *
  - <cap1> are fun!!
< topic

Here is another example, using respond this will redirect the current flow to another topic and keep looking for matches there.


+ I love animals
- ^respond("animals")

+ I love pets
- ^respond("pets"))

> topic animals {keep}
  + I love animals
  - Me too, what is your favorite?

  + *1
  % * what is your favorite? *
  - <cap1> are scary
< topic

> topic pets {keep}
  + I love pets
  - Me too, what is your favorite?

  + *1
  % * what is your favorite? *
  - <cap1> are fun!!
< topic
alexraj commented 6 years ago

Thanks @silentrob . I tried both options, and neither of them seem to work. The output is like below;

Bot> Hi, How can I help you? You> I love pets Bot> Me too, what is your favorite? You> cats Bot> cats are scary

I also attached the debug output. I think after first reply, the topic correctly gets set to "pets". However in the second request, it goes back to "random". superscript_topic_conversation_issue.txt

alexraj commented 6 years ago

Can you please help me to get out of this issue. In my bot, each conversation starts with the same reply. Meaning, the first reply would be "What is your favorite", and the subsequent conversation varies based on what the user said like "I love pets|animals|zoo". So, it is very important for me to switch to a right topic after I get the response from user.

The only workaround for me is to ask the same question differently. Like; "what is your favorite" and "what is the favorite". But, the option on that is very limited, and I am potentially looking at atleast 100 topics.

If there is a patch, I can try. Or, if someone give a pointer on where to look for potential fix, I can check that too.

alexraj commented 6 years ago

I think the issue is with matching the conversation. I removed the <cap> from the Gambit. The below gambit shows ones with out *1, and it still have the same issue. The conversation always ends with "Cats are scary".

+ I love animals
- ^topicRedirect("animals", "__favorite__")

+ I love pets
- ^topicRedirect("pets", "__favorite__")

> topic animals {keep}
  + __favorite__
  - Me too, what is your favorite?

  + Mine is cat
  - Cats are scary, what is your favorite?
  + whatever
  % * what is your favorite *
  -Cats are scary
< topic

> topic pets {keep}
  + __favorite__
  - Me too, what is your favorite?

  + Mine is cat
  - I love it too, what is your favorite ?
  + whatever
  % * what is your favorite *
  -Cats are fun
< topic

and here is the conversation.

You> I love pets Bot> Me too, what is your favorite? You> mine is cat Bot> I love it too, what is your favorite ? You> whatever Bot> Cats are scary

The following text from the documentation has some clue. Is it possible that when we search for previous reply, we get outside the context of topic?

When the next input from the user comes into the system, we first check the history and see if the previous reply has more dialogue. This is noted by the % What is your favorite color? line in SuperScript.

alexraj commented 6 years ago

I finally made it work. I am not sure if this is workaround or a fix. The fix is to add a topic field to the gambit model, and during the match when there are multiple matches for a conversation gambit, use the one that belongs to the user's current topic. These are the changes;

  1. In db/models/gambit.js added following line at line 87 topic: { type: String, default: 'random'}
  2. In db/import.js added following line at line 42 topic: gambit.topic
  3. In getReply.js added following lines at line 122

      var preservedMatch = unfilteredMatches[0];
      var previousUnfilteredMatches = unfilteredMatches.length;
      // If there are multiple matches, then select the one from topic.
      if (unfilteredMatches.length > 1) {
        unfilteredMatches = unfilteredMatches.filter(match => 
          match.gambit.topic === options.user.getTopic()
        );
      }
    
      // Fallback, just in case no match for a topic, unlikely to happen.
      if (previousUnfilteredMatches != 0 && unfilteredMatches.length === 0) {
        unfilteredMatches.push(preservedMatch);
      }

    If this sounds like an appropriate fix, please let me know and I can create a pull request. I ran "npm test" with this change, and the results are same (157 passing, 7 pending).