C# 如何让QnA maker和Luis BOT的集成准确地工作?

C# 如何让QnA maker和Luis BOT的集成准确地工作?,c#,visual-studio,azure,C#,Visual Studio,Azure,我面临的问题是,我正在遵循此文档来集成LUIS和QnA maker 我已经修改了FAQbot的代码。我有两个意图,一个是有QnA制作者的FAQ意图,然后是另一个意图。 当我在聊天机器人中从FAQ意图提问时,它会给出准确的回答,当我问一个完全不同的问题时,它也会转到另一个意图。 然而,当我问另一个新问题时,它不在知识库中,但有一些与现有问题相似的单词,它会给我一个答案,预测它来自常见问题解答的意图。 而不是其他意图。如何提高模型的准确性 public class Metadata { p

我面临的问题是,我正在遵循此文档来集成LUIS和QnA maker

我已经修改了FAQbot的代码。我有两个意图,一个是有QnA制作者的FAQ意图,然后是另一个意图。 当我在聊天机器人中从FAQ意图提问时,它会给出准确的回答,当我问一个完全不同的问题时,它也会转到另一个意图。 然而,当我问另一个新问题时,它不在知识库中,但有一些与现有问题相似的单词,它会给我一个答案,预测它来自常见问题解答的意图。 而不是其他意图。如何提高模型的准确性

public class Metadata
{
    public string name { get; set; }
    public string value { get; set; }
}

public class Answer
{
    public IList<string> questions { get; set; }
    public string answer { get; set; }
    public double score { get; set; }
    public int id { get; set; }
    public string source { get; set; }
    public IList<object> keywords { get; set; }
    public IList<Metadata> metadata { get; set; }
}

public class QnAAnswer
{
    public IList<Answer> answers { get; set; }
}

[Serializable]
public class QnAMakerService
{
    private string qnaServiceHostName;
    private string knowledgeBaseId;
    private string endpointKey;

    public QnAMakerService(string hostName, string kbId, string endpointkey)
    {
        qnaServiceHostName = hostName;
        knowledgeBaseId = kbId;
        endpointKey = endpointkey;

    }
    async Task<string> Post(string uri, string body)
    {
        using (var client = new HttpClient())
        using (var request = new HttpRequestMessage())
        {
            request.Method = HttpMethod.Post;
            request.RequestUri = new Uri(uri);
            request.Content = new StringContent(body, Encoding.UTF8, "application/json");
            request.Headers.Add("Authorization", "EndpointKey " + endpointKey);

            var response = await client.SendAsync(request);
            return  await response.Content.ReadAsStringAsync();
        }
    }
    public async Task<string> GetAnswer(string question)
    {
        string uri = qnaServiceHostName + "/qnamaker/knowledgebases/" + knowledgeBaseId + "/generateAnswer";
        string questionJSON = "{\"question\": \"" + question.Replace("\"","'") +  "\"}";

        var response = await Post(uri, questionJSON);

        var answers = JsonConvert.DeserializeObject<QnAAnswer>(response);
        if (answers.answers.Count > 0)
        {
            return answers.answers[0].answer;
        }
        else
        {
            return "No good match found.";
        }
    }
}

[Serializable]
public class BasicLuisDialog : LuisDialog<object>
{
    // LUIS Settings
    static string LUIS_appId = "29e08438-43ae-40ab-8a77-7bb6474edd13";
    static string LUIS_apiKey = "95137566e76443019e26a653f99d7a0c";
    static string LUIS_hostRegion = "westus.api.cognitive.microsoft.com";

    // QnA Maker global settings
    // assumes all KBs are created with same Azure service
    static string qnamaker_endpointKey = "40dfaeb5-5679-4f8f-863f-a5f587101a88";
    static string qnamaker_endpointDomain = "azurebot123";

    // QnA Maker TA_FAQbot Knowledge base
    static string TA_FAQbot_kbID = "13fed287-64d7-43aa-9a39-2c6bc86ea511";


    // Instantiate the knowledge bases
    public QnAMakerService azurebot123QnAService = new QnAMakerService("https://" + qnamaker_endpointDomain + ".azurewebsites.net", TA_FAQbot_kbID, qnamaker_endpointKey);


    public BasicLuisDialog() : base(new LuisService(new LuisModelAttribute(
        LUIS_appId,
        LUIS_apiKey,
        domain: LUIS_hostRegion)))
    {
    }

    [LuisIntent("None")]
    public async Task NoneIntent(IDialogContext context, LuisResult result)
    {
        HttpClient client = new HttpClient();
        await this.ShowLuisResult(context, result);
    }


    [LuisIntent("RandomFAQ")]
    public async Task RandomFAQIntent(IDialogContext context, LuisResult result)
    {
        HttpClient client = new HttpClient();
        await this.ShowLuisResult(context, result);
    }

    // TA_FAQbot Intent
    [LuisIntent("TA_FAQbot")]
    public async Task TA_FAQbotIntent(IDialogContext context, LuisResult result)
    {
        // Ask the FAQ knowledge base
        var qnaMakerAnswer = await azurebot123QnAService.GetAnswer(result.Query);
        await context.PostAsync($"{qnaMakerAnswer}");
        context.Wait(MessageReceived);
    }

    =
    private async Task ShowLuisResult(IDialogContext context, LuisResult result)
    {
        await context.PostAsync($"You have reached {result.Intents[0].Intent}. Sorry, I do not have the answer to this question. I will get back to you with an answer soon.");
        context.Wait(MessageReceived);

    }

          [LuisIntent("Cancel")]
          public async Task CancelIntent(IDialogContext context, LuisResult result)
          {
              await this.ShowLuisResult(context, result);
          }

          [LuisIntent("Help")]
          public async Task HelpIntent(IDialogContext context, LuisResult result)
       {
              await this.ShowLuisResult(context, result);
          }
}
公共类元数据
{
公共字符串名称{get;set;}
公共字符串值{get;set;}
}
公开课答案
{
公共IList问题{get;set;}
公共字符串答案{get;set;}
公共双倍分数{get;set;}
公共int id{get;set;}
公共字符串源{get;set;}
公共IList关键字{get;set;}
公共IList元数据{get;set;}
}
公共类QNAR答案
{
公共IList答案{get;set;}
}
[可序列化]
公共类QNAMAKERSERSERVICE
{
私有字符串qnaServiceHostName;
私有字符串knowledgeBaseId;
私钥;
公共QnAMakerService(字符串主机名、字符串kbId、字符串端点键)
{
qnaServiceHostName=主机名;
knowledgeBaseId=kbId;
endpointKey=endpointKey;
}
异步任务Post(字符串uri、字符串正文)
{
使用(var client=new HttpClient())
使用(var request=new HttpRequestMessage())
{
request.Method=HttpMethod.Post;
request.RequestUri=新Uri(Uri);
request.Content=newstringcontent(body,Encoding.UTF8,“application/json”);
添加(“授权”、“EndpointKey”+EndpointKey);
var response=wait client.sendaync(请求);
return wait response.Content.ReadAsStringAsync();
}
}
公共异步任务GetAnswer(字符串问题)
{
字符串uri=qnaServiceHostName+“/qnamaker/Knowledgebase/”+knowledgeBaseId+“/generateAnswer”;
字符串questionJSON=“{\'question\':\”“+question.Replace(“\”,“'”)+“\”}”;
var response=await Post(uri,questionJSON);
var answers=JsonConvert.DeserializeObject(响应);
如果(answers.answers.Count>0)
{
返回答案。答案[0]。答案;
}
其他的
{
返回“未找到好匹配项。”;
}
}
}
[可序列化]
公共类基本对话:LuisDialog
{
//路易斯设置
静态管柱LUIS_appId=“29e08438-43ae-40ab-8a77-7bb6474edd13”;
静态字符串LUIS_apiKey=“95137566e76443019e26a653f99d7a0c”;
静态字符串LUIS_hostRegion=“westus.api.cognitive.microsoft.com”;
//QnA Maker全局设置
//假设所有知识库都是使用相同的Azure服务创建的
静态字符串qnamaker_endpointKey=“40dfaeb5-5679-4f8f-863f-A5F58710A88”;
静态字符串qnamaker_endpointDomain=“azurebot123”;
//QnA Maker TA_FAQbot知识库
静态字符串TA_FAQbot_kbID=“13fed287-64d7-43aa-9a39-2c6bc86ea511”;
//实例化知识库
公共QnAMakerService azurebot123QnAService=新QnAMakerService(“https://“+qnamaker_endpointDomain+”.azurewebsites.net”、TA_FAQbot_kbID、qnamaker_endpointKey);
public BasicLuisDialog():基本(新的LuisService(新的LuisModelAttribute(
路易斯·阿皮德,
路易斯·阿皮基,
域名:LUIS_hostRegion)
{
}
[路易辛顿(“无”)]
公共异步任务NoneIntent(IDialogContext上下文,LuisResult结果)
{
HttpClient=新的HttpClient();
等待这个。ShowLuisResult(上下文、结果);
}
[LuisIntent(“随机问答”)]
公共异步任务(IDialogContext上下文、LuisResult结果)
{
HttpClient=新的HttpClient();
等待这个。ShowLuisResult(上下文、结果);
}
//塔乌
[LuisIntent(“TA_FAQbot”)]
公共异步任务TA_FAQbotIntent(IDialogContext上下文,LuisResult结果)
{
//询问常见问题解答知识库
var qnaMakerAnswer=await azurebot123QnAService.GetAnswer(result.Query);
wait context.PostAsync($“{qnaMakerAnswer}”);
Wait(MessageReceived);
}
=
专用异步任务ShowLuisResult(IDialogContext上下文,LuisResult结果)
{
wait context.PostAsync($“您已到达{result.Intents[0].Intent}。抱歉,我没有这个问题的答案。我会尽快给您答复。”);
Wait(MessageReceived);
}
[LUISINT(“取消”)]
公共异步任务CancelIntent(IDialogContext上下文,LuisResult结果)
{
等待这个。ShowLuisResult(上下文、结果);
}
[LuisIntent(“帮助”)]
公共异步任务帮助意图(IDialogContext上下文,LuisResult结果)
{
等待这个。ShowLuisResult(上下文、结果);
}
}

当您计划构建LUIS模型时,请选择一个好的命名约定。否则,当您从代码中引用特定意图时,将很难做到这一点

不要使用太长的单词作为意图名称。只使用简短的描述性单词。使用驼峰大小写或点分隔短语是一个很好的做法

必须记住路易斯:

  • 确定不同的意图
  • 确保每个意图的词汇表仅适用于该意图,并且 没有以不同的意图重叠

  • 一定要为意图找到最佳点
  • 使用路易斯的预测数据来确定你的意图是否正确 重叠。重叠的意图混淆了路易斯。结果是 得分最高的意图是