Integrating FormFlow with Microsoft LUIS

0

I made a bot using the Microsoft BotBuilder SDK. I want a BotBuilder FormFlow to recognize and capture entities in a user response. For example, if the user responds: my email is [email protected], then FormFlow would have to capture, as an answer to his question only the email ([email protected]), ignoring the initial part of the text ( my email is). Is it possible to do this only with FormFlow or do I need to use LUIS to do this? How can LUIS be used together with FormFlow?

    
asked by anonymous 23.03.2018 / 13:44

1 answer

0

Yes, it is possible.

At the time of constructing your FormFlow, for all the properties of your entity, set the formflow's Field () method to the validate parameter and call a method that tries to extract all possible entities from LUIS and store in the instance that FormFlow is filling in.

 .Field(nameof(NomePropriedade), validate: async (state, value) => await ExtractEntity((state as NomeClasse), value, "nomeEntidadeLuisCorrespondenteAoCampoDaVez"))

Applying would look like this:

     .Field(nameof(DataNascimento), validate: async (state, value) => await ExtractEntity((state as Cliente), value, "data_nascimento"))

So in every question that FormFlow does you can intercept and extract the entities from luis and send pro state.

The ExtractEntity method would look like this:

 protected static async Task<ValidateResult> ExtractEntity(Cliente state, object value, string entityName, Func<ValidateResult, Task<ValidateResult>> next = null)
    {
        INLPService service = NLPServiceFactory.Create("NomeServicoLuis");
        var nlpresult = await service.QueryAsync(value.ToString());
        var entity = nlpresult.QueryEntity(entityName);
        var result = new ValidateResult { IsValid = true, Value = string.IsNullOrEmpty(entity) ? value : entity };

        var nomeEntity = nlpresult.QueryEntity("nome");
        var dataNascimentoEntity = nlpresult.QueryEntity("data_nascimento");

        state.Nome = CoalesceEntityValue(nomeEntity, entityName, "nome", value.ToString(), state.Nome);
        state.DataNascimento = CoalesceEntityValue(dataNascimentoEntity, entityName, "data_nascimento", value.ToString(), state.DataNascimento);


        if (next != null)
            return await next(result);
        else
            return result;
    }

And finally the CoalesceEntityValue method decides which value to assign in the property, the value extracted from luis, what user entered, or the current value.

 private static string CoalesceEntityValue(string extractedEntityValue, string extractedEntityName, string entityName, string rawEntityValue, string currentValue)
    {
        if (!string.IsNullOrEmpty(extractedEntityValue))
        {
            return extractedEntityValue;
        }
        else if (!string.IsNullOrEmpty(rawEntityValue) && entityName == extractedEntityName)
        {
            return rawEntityValue;
        }
        else
        {
            return currentValue;
        }
    }

Any questions are available.

    
29.10.2018 / 16:31