在C#中从FormFlow调用LUIS

问题描述 投票:2回答:2

我正在使用Microsoft bot框架创建一个机器人来询问用户,然后了解答案。在bot框架中使用FormFlow API对用户进行询问并检索答案。以下是表单流的代码:

public enum Genders { none, Male, Female, Other};

[Serializable]
public class RegisterPatientForm
{

    [Prompt("What is the patient`s name?")]
    public string person_name;

    [Prompt("What is the patients gender? {||}")]
    public Genders gender;

    [Prompt("What is the patients phone number?")]
    [Pattern(@"(<Undefined control sequence>\d)?\s*\d{3}(-|\s*)\d{4}")]
    public string phone_number;

    [Prompt("What is the patients Date of birth?")]
    public DateTime DOB;

    [Prompt("What is the patients CNIC number?")]
    public string cnic;


    public static IForm<RegisterPatientForm> BuildForm()
    {
        OnCompletionAsyncDelegate<RegisterPatientForm> processHotelsSearch = async (context, state) =>
        {
            await context.PostAsync($"Patient {state.person_name} registered");
        };

        return new FormBuilder<RegisterPatientForm>()
            .Field(nameof(person_name),
            validate: async (state, value) =>
            {
                //code here for calling luis
            })
            .Field(nameof(gender))
            .Field(nameof(phone_number))
            .Field(nameof(DOB))
            .Field(nameof(cnic))
            .OnCompletion(processHotelsSearch)
            .Build();
    }

}

用户可以在询问姓名时输入:

我的名字是詹姆斯邦德

名称也可以是可变长度。我最好从这里调用luis并获取意图的实体(名称)。我目前不知道如何从表单流程调用luis对话框。

c# azure botframework luis formflow
2个回答
6
投票

您可以使用LUIS的API方法,而不是对话框方法。

你的代码是 - RegisterPatientForm类:

public enum Genders { none, Male, Female, Other };

[Serializable]
public class RegisterPatientForm
{

    [Prompt("What is the patient`s name?")]
    public string person_name;

    [Prompt("What is the patients gender? {||}")]
    public Genders gender;

    [Prompt("What is the patients phone number?")]
    [Pattern(@"(<Undefined control sequence>\d)?\s*\d{3}(-|\s*)\d{4}")]
    public string phone_number;

    [Prompt("What is the patients Date of birth?")]
    public DateTime DOB;

    [Prompt("What is the patients CNIC number?")]
    public string cnic;


    public static IForm<RegisterPatientForm> BuildForm()
    {
        OnCompletionAsyncDelegate<RegisterPatientForm> processHotelsSearch = async (context, state) =>
        {
            await context.PostAsync($"Patient {state.person_name} registered");
        };

        return new FormBuilder<RegisterPatientForm>()
            .Field(nameof(person_name),
            validate: async (state, response) =>
            {
                var result = new ValidateResult { IsValid = true, Value = response };

                //Query LUIS and get the response
                LUISOutput LuisOutput = await GetIntentAndEntitiesFromLUIS((string)response);

                //Now you have the intents and entities in LuisOutput object
                //See if your entity is present in the intent and then retrieve the value
                if (Array.Find(LuisOutput.intents, intent => intent.Intent == "GetName") != null)
                {
                    LUISEntity LuisEntity = Array.Find(LuisOutput.entities, element => element.Type == "name");

                    if (LuisEntity != null)
                    {
                        //Store the found response in resut
                        result.Value = LuisEntity.Entity;
                    }
                    else
                    {
                        //Name not found in the response
                        result.IsValid = false;
                    }
                }
                else
                {
                    //Intent not found
                    result.IsValid = false;
                }
                return result;
            })
            .Field(nameof(gender))
            .Field(nameof(phone_number))
            .Field(nameof(DOB))
            .Field(nameof(cnic))
            .OnCompletion(processHotelsSearch)
            .Build();
    }

    public static async Task<LUISOutput> GetIntentAndEntitiesFromLUIS(string Query)
    {
        Query = Uri.EscapeDataString(Query);
        LUISOutput luisData = new LUISOutput();
        try
        {
            using (HttpClient client = new HttpClient())
            {
                string RequestURI = WebConfigurationManager.AppSettings["LuisModelEndpoint"] + Query;
                HttpResponseMessage msg = await client.GetAsync(RequestURI);
                if (msg.IsSuccessStatusCode)
                {
                    var JsonDataResponse = await msg.Content.ReadAsStringAsync();
                    luisData = JsonConvert.DeserializeObject<LUISOutput>(JsonDataResponse);
                }
            }
        }
        catch (Exception ex)
        {

        }
        return luisData;
    }
}

这里GetIntentAndEntitiesFromLUIS方法使用您的Luis应用程序公开的端点查询LUIS。使用密钥LuisModelEndpoint将端点添加到Web.config

通过转到luis应用程序中的“发布”选项卡,找到您的luis端点

你的web.config看起来像这样

<appSettings>
  <!-- update these with your BotId, Microsoft App Id and your Microsoft App Password-->
  <add key="BotId" value="YourBotId" />
  <add key="MicrosoftAppId" value="" />
  <add key="MicrosoftAppPassword" value="" />
  <add key="LuisModelEndpoint" value="https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/YOUR_MODEL_ID?subscription-key=YOUR_SUBSCRIPTION_KEY&amp;verbose=true&amp;timezoneOffset=0&amp;q="/>
</appSettings>

我创建了一个LUISOutput类来反序列化响应:

public class LUISOutput
{
    public string query { get; set; }
    public LUISIntent[] intents { get; set; }
    public LUISEntity[] entities { get; set; }
}
public class LUISEntity
{
    public string Entity { get; set; }
    public string Type { get; set; }
    public string StartIndex { get; set; }
    public string EndIndex { get; set; }
    public float Score { get; set; }
}
public class LUISIntent
{
    public string Intent { get; set; }
    public float Score { get; set; }
}

模拟器响应enter image description here


1
投票

在这种情况下,最好在formflow之外调用luis intent。您正在寻找的功能并不完全存在。您可以调用“名称”意图,然后像这样调用您的表单:

[LuisIntent("Name")]
public async Task Name(IDialogContext context, LuisResult result)
{
    //save result in variable
    var name = result.query
    //call your form
    var pizzaForm = new FormDialog<PizzaOrder>(new PizzaOrder(), this.MakePizzaForm, FormOptions.PromptInStart, entities);
    context.Call<PizzaOrder>(pizzaForm, PizzaFormComplete);
    context.Wait(this.MessageReceived);
}
© www.soinside.com 2019 - 2024. All rights reserved.