Skip to content

Tutorial

toncho11 edited this page Oct 30, 2019 · 33 revisions

In KorraAI you create a "model" that incorporates everything the bot can do. This includes the phrases the bot will use, when the phrases will be used, after what time or due to what circumstances the behavior will change.

In KorraAI these are 7 things to create or modify:

  1. Create a new model using the KorraAIModel class (or re-use an existing one)

  2. Choose categories of interactions

  3. For each category provide items

  4. Build a probabilistic distribution over the categories

  5. Choose probabilistic variables that correspond to concepts from your domain and set dependencies (also called causality) between them

  6. Adjust cognitive distributions

  7. Encode changes of behavior based on the user's responses or the time that has passed

It is recommended to start with the model already provided called Joi. If you just want to have a feel of how it works you can modify the existing phrases or add phrases to the existing categories. Phrases are the sentences the bot will use and they are also generalized as "items", where an item can be non-verbal interaction. For example you can add more sport recommendations (items) in the category "Suggestions" - > "SuggestGoToGym".

1. Create a new model

KorraAIModel class is a C# class that allows you to bring together everything that defines your bot. The model Joi uses this class (actually an interface). This is your starting point. All the parameters, distributions and items are accessible from this class. An exception is the list of random variables, which accessible by the ProbVariables class.

2,3,4. Categories, Items and Main Interaction Distribution

Currently encoded categories:

              MakeSuggestion  Say a joke, suggest a movie to watch, play a song
     AskUncertanFactQuestion  Ex. "Are you tired?" , how much tired can vary
AskPureFactQuestionAboutUser  Ex. "How old are you?", an exact fact about the user
   SharePureFactInfoAboutBot  Ex. "I am 30 years old", an exact fact about the bot
      ChangeVisualAppearance  Changing clothes from time to time.
       ExpressingMentalState  Ex. "I am definitely in a good mood today!", how much happy can vary

Next you can modify the probabilities of each category:

              MakeSuggestion  50.3% ##################################################
     AskUncertanFactQuestion  1.06% #
AskPureFactQuestionAboutUser  45.5% #############################################
   SharePureFactInfoAboutBot     0% 
      ChangeVisualAppearance  1.59% #
       ExpressingMentalState  1.59% #

The above shows that in this model we favor 'MakeSuggestion' and 'AskPureFactQuestionAboutUser' with the later asking the user questions such as: age, name, location, etc. The 'MakeSuggestion' category has subcategories:

           TellJoke  44.7% ############################################
SuggestListenToSong  32.6% ################################
              GoOut   4.1% ####
SuggestToWatchMovie  9.31% #########
     SuggestGoToGym  9.31% #########  

From each category we sample "interactions", also called "items". Interactions are pre-sampled, although we could sample just one before using it. We encoded a distribution, so it should not matter if we sampled 100, 1000 or 1, we should get the same proportions in the end. Pre-sampling also helps to verify that we really got the distribution we wanted and that can be verified even at run-time. Pre-sampling also helps with debugging. The final result is a list of interactions ready to be executed in the conversation with the user. The CompanionLog.txt contains all this information. Also statistics are available describing how many items were assigned to each category:

|SuggestGoToGym: 8
|AskPureFactQuestionAboutUser: 0
|SuggestListenToSong: 37
|ExpressingMentalState: 3
|TellJoke_missing: 33
|**SuggestToWatchMovie_missing: 4**
|AskUncertanFactQuestion: 2
|ChangeVisualAppearance: 1
|GoOut: 2
|**SuggestGoToGym_missing: 2**

If we sample for too many items then there might not be enough items. These are showed as "Category__missing". Probabilities are adjusted once a category is depleted. Please also check the Add a concept page.

5. Choose probabilistic variables and links between them

Please check the Probabilistic Examples page. Probabilistic modelling can be a hard task, so you can skip it for now until you feel comfortable with it.

6. Adjust cognitive distributions provided by KorraAI

The KorraAI bot framework gives control over events related to the appearance of the bot as well as certain timings events. The following are currently supplied to the bot designer.

    // Returns the pause (time interval) between two interactions (of any type)
    // Example: Normal distribution (3.7, 0.25)
    float GetNextInteactionPause();

    // Returns the pause (time interval) between two smiles
    // Example: Normal distribution (12, 3)
    float NextSmilePauseTime();

    // Returns how much time the eyes will stay focused on the camera after started talking
    // Example: Normal distribution (7, 1.2)
    float NextTimeDurationEyesStayFocusedOnCameraAfterStartedTalking();

    // After what amount of time the bot will stop waiting for a response from the user
    // Example: Normal distribution (25, 4)
    float NextQuestionTimeout();

    // Returns the next clothing outfit following a distribution or specific logic
    int GetNextOutfit(int[] activeOutfitsIndexes, int lastOutfitUsed);

Let's take a look at: GetNextInteactionPause(). Using it we can reduce the time between interactions, but not by a fixed amount. GetNextInteactionPause() uses the (ex. Normal(m,n) distribution) and the pauses between interactions are centered for example around mean = 2.1 seconds and variance = 1. So we can reduce the mean in order to decrease the time between two interactions.

And here is the full source code:

float IDistributions.GetNextInteactionPause(CommItem? prev, CommItem? next, bool isReacting)
{
    if (!isReacting)
    {
        if (allInteractionPauses.Count == 0)
        {
            var normalDist = from seconds in Normal(3.7, 0.25) //default 4.7, 1
                                select seconds;

            for (var i = 0; i < 101; i++)
            {
                float value = (float)normalDist.Sample(); //sampling
                allInteractionPauses.Enqueue(value);
            }
        }

        return allInteractionPauses.Dequeue();
    }
    else //if it is a response, we try to react quickly in the range of [0.3-0.5] seconds
    {
        float value = pauseUniformAfterReaction.Next(3, 6) / 10f;
        return value;
    }
}

In the above example we encode Normal distribution between unrelated interactions and a Uniform when the bot is reacting to the user's response. It is amused that in the later the pause time should be shorter.

Although the Normal distribution (as well as the Uniform) is continuous, but there are ways to sample (or rather generate) a discrete value from it. Another aspect is that the theory of probability distributions works only when we are using a rather large amount of samples. If we have a low amount of items and a low number of samples we might want to put an extra effort in order to achieve a desired cognitive effect.

7. Encode changes of behavior based on the user's responses or the time that has passed

The method UpdateModel() is called frequently. This method allows a bot designer to:

  • If time based

Check the time elapsed since the bot has been activated or if the time of the day has changed (time for lunch, sleep, go to work, etc)

  • If response based

See if a pure fact or uncertain fact has been updated by response from the user

Both of the above conditions can update probabilistic variables. When time based we can for example change the bot form being 0.7 happy to 0.3 happy and make him/her sad (where 0.7 is the parameter of the Bernulli distribution).

See the example below where if the user responded that he does not do sport, we increase the rate at which we suggest him to do more sport (from 0.10 to 0.15).

if (factSport.IsAnswered && context.Phrases.IsNo(factSport.Value))
                {
                    ProbVariables.Bot.SuggestGoToGym = BernoulliF(Prob(0.15));
                    requestRegeneration = true;
                }

In the end we decide whether the behavior of the bot should be affected by setting the variable 'requestRegeneration' to true. This triggers a re-sampling and thus increases (or decreases) the number of sport suggestions the user will encounter for a fixed amount of time. For example instead of 3 sports suggestions per hour, he will get 5.

You will also need to go through these subjects: