c# - 在 C# 中从 FormFlow 调用 LUIS

标签 c# azure botframework azure-language-understanding formflow

我正在使用 Microsoft 机器人框架创建一个机器人来询问用户然后理解答案。使用机器人框架中的 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();
    }

}

当要求输入姓名时,用户可以输入:

my name is James Bond

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

最佳答案

您可以使用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

Find your luis endpoint by going to Publish tab in your luis app

你的 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>

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

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

关于c# - 在 C# 中从 FormFlow 调用 LUIS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48004803/

相关文章:

C# - 设置 HttpClient header 以将数据 POST 到 Azure REST API

c# - Herocard 在 Skype Microsoft 机器人框架中不显示超过 3 个按钮

node.js - 哪些因素可能会阻止 EC2 托管的机器人回复 Microsoft Bot Framework 上的消息?

node.js - 当机器人在 Teams 中发送消息时,如何收到通知?

c# - 遍历一次性对象

c# - 通知自定义声音不播放 UWP

azure - 为什么需要在 Azure 管理门户中而不是在 Web 作业的 App.config 中配置 Web 作业的连接字符串?

azure - 从 Azure 网站访问 Azure 文件服务

javascript - asp.net按钮-JS点击不起作用

c# - Fluent NHibernate 映射 IList<Point> 作为单列的值