在有人选择任何意图时尝试尝试调用特定意图,例如:如果用户选择cmtools意图,则用户只能提出与cmtools相关的问题。除了cmtools以外,如果用户提出问题,则该bot应该rply对不起,这不是一个很好的选择。 提前致谢!
using System;
using System.Net.Http;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.Bot.Builder.Dialogs;
using Microsoft.Bot.Builder.Luis;
using Microsoft.Bot.Builder.Luis.Models;
using Newtonsoft.Json;
using System.Text;
namespace Microsoft.Bot.Sample.LuisBot
{
[LuisModel("id","key")]
[Serializable]
public class BasicLuisDialog : LuisDialog<object>
{
// QnA Maker global settings
// assumes all KBs are created with same Azure service
static string qnamaker_endpointKey = "endpointkey";
static string qnamaker_endpointDomain = "chatbot";
// QnA Maker Knowledge base
static string AIS_KB= "id1";
static string Speed_KB = "id2";
static string CMTools_KB = "id3";
// QnA Maker Finance Knowledge base
// Instantiate the knowledge bases
public QnAMakerService AIS_KBQnAService = new QnAMakerService("https://" + qnamaker_endpointDomain + ".azurewebsites.net", AIS_KB, qnamaker_endpointKey);
public QnAMakerService Speed_KBQnAService = new QnAMakerService("https://" + qnamaker_endpointDomain + ".azurewebsites.net", Speed_KB, qnamaker_endpointKey);
public QnAMakerService CMTools_KBQnAService = new QnAMakerService("https://" + qnamaker_endpointDomain + ".azurewebsites.net", CMTools_KB, qnamaker_endpointKey);
public const string AIS_KBIntent = "AIS_KB";
public const string Speed_KBIntent = "Speed_KB"; // new intent
public const string CMTools_KBIntent = "CMTools_KB"; // new intent
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 + "'}";
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.";
}
}
}
public enum Selection
{
CMTools, AIS, Speed
}
[LuisIntent("AppSelection")]
private async Task AppSelection(IDialogContext context, LuisResult result)
{
var options = new Selection[] { Selection.CMTools, Selection.AIS, Selection.Speed };
var descriptions = new string[] { "CMTools", "AIS", "Speed" };
PromptDialog.Choice<Selection>(context, ResumeAfterOrderSelectionClarification,options, "Please choose your application?", descriptions: descriptions );
}
private async Task ResumeAfterOrderSelectionClarification(IDialogContext context, IAwaitable<Selection> result,LuisResult result1)
{
var selection = await result;
await context.PostAsync($"Thanks for choosing {selection}. how can I help you ?");
switch(selection){
case CMTools:
await context.PostAsync($"CMTools");
//code needed to call CMTools_KB intent
break;
case AIS:
await context.PostAsync($"AIS_KB");
//code needed to call AIS_KB intent
break;
case Speed:
await context.PostAsync($"Speed_KB");
// Ask the HR knowledge base
//code needed to call Speed_KB intent
break;
default:
await context.PostAsync($"testing.........");
}
}
[LuisIntent("")]
[LuisIntent("None")]
public async Task NoneIntent(IDialogContext context, LuisResult result)
{
HttpClient client = new HttpClient();
await this.ShowLuisResult(context, result);
}
[LuisIntent("AIS_KB")]
public async Task AIS_KBIntent(IDialogContext context, LuisResult result)
{
// Ask the HR knowledge base
var qnaMakerAnswer = await AIS_KBQnAService.GetAnswer(result.Query);
await context.PostAsync($"{qnaMakerAnswer}");
context.Wait(MessageReceived);
}
[LuisIntent("Speed_KB")]
public async Task Speed_KBIntent(IDialogContext context, LuisResult result)
{
// Ask the HR knowledge base
var qnaMakerAnswer = await Speed_KBQnAService.GetAnswer(result.Query);
await context.PostAsync($"{qnaMakerAnswer}");
context.Wait(MessageReceived);
}
[LuisIntent("CMTools_KB")]
public async Task CMTools_KBIntent(IDialogContext context, LuisResult result)
{
// Ask the HR knowledge base
var qnaMakerAnswer = await CMTools_KBQnAService.GetAnswer(result.Query);
await context.PostAsync($"{qnaMakerAnswer}");
context.Wait(MessageReceived);
}
private async Task ShowLuisResult(IDialogContext context, LuisResult result)
{
await context.PostAsync($"Sorry , I am not able to help you with this questions,Please connect our L2 Support Group");
var qnaMakerAnswer = await CMTools_KBQnAService.GetAnswer(result.Query);
await context.PostAsync($"{qnaMakerAnswer}");
await context.PostAsync($"it is finished ");
context.Wait(MessageReceived);
}
}
}