Example #1
0
        public static async Task <string> GetQnAAnswer(string question, IConfiguration configuration)
        {
            var response = "Something Happened.  Jim Bot is having issues!  Try again later.";

            try
            {
                var settings        = configuration.GetSection("QnASettings");
                var endpointKey     = settings["EndpointKey"];
                var knowledgeBaseId = settings["KnowledgeBaseId"];
                var url             = settings["Url"];


                var qnaEndpoint1 = new QnAMakerEndpoint
                {
                    // add subscription key for QnA and knowledge base ID
                    EndpointKey     = endpointKey,
                    KnowledgeBaseId = knowledgeBaseId,
                    Host            = url
                };


                var qnaMaker     = new Microsoft.Bot.Builder.Ai.QnA.QnAMaker(qnaEndpoint1);
                var queryResults = await qnaMaker.GetAnswers(question);

                if (queryResults != null && queryResults.Any())
                {
                    var enumerator = queryResults.GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        response = $"Another possible answer is {enumerator.Current}";
                    }
                    response = queryResults.First().Answer;
                }
                else
                {
                    response = "I don't have an answer for you.";
                }
            }
            catch (Exception exception)
            {
                Console.Write(exception.Message);

                return("Error Occurred");
            }
            return(response);
        }
        public QnAMakerMiddleware(QnAMakerMiddlewareOptions options, HttpClient httpClient = null)
        {
            _options = options ?? throw new ArgumentNullException(nameof(options));

            _qnaMaker = new QnAMaker(options, httpClient);
        }
 /// <summary>
 /// Creates a new <see cref="QnAMakerMiddleware"/> instance.
 /// </summary>
 /// <param name="endpoint">Endpoint details to connect to the QnA service.</param>
 /// <param name="options">Options to control the behavior of the middleware.</param>
 /// <param name="httpClient">A client with which to talk to QnAMaker.
 /// If null, a default client is used for this instance.</param>
 public QnAMakerMiddleware(QnAMakerEndpoint endpoint, QnAMakerMiddlewareOptions options = null, HttpClient httpClient = null)
 {
     _endpoint = endpoint ?? throw new ArgumentNullException(nameof(endpoint));
     _options  = options ?? new QnAMakerMiddlewareOptions();
     _qnaMaker = new QnAMaker(endpoint, options, httpClient);
 }
Example #4
0
        public async Task OnTurn(ITurnContext context)
        {
            switch (context.Activity.Type)
            {
            case ActivityTypes.ConversationUpdate:
                if (context.Activity.MembersAdded.FirstOrDefault()?.Id == context.Activity.Recipient.Id)
                {
                    Activity activity = new Activity();
                    await context.SendActivity("Luis Dispatch Bot says hello, you can ask me some stuff.", "James Please");
                }
                break;

            case ActivityTypes.Message:
                string utterance = context.Activity.Text;
                await context.SendActivity("You said: " + utterance);

                var luisResult = context.Services.Get <RecognizerResult>(LuisRecognizerMiddleware.LuisRecognizerResultKey);
                var(topIntent, score) = luisResult.GetTopScoringIntent();

                if (topIntent == "None")
                {
                    await context.SendActivity("LUIS has no idea what you're talking about...");

                    //var image = new Attachment { ContentUrl = "C:\\Work\\jamesbotv4\\jamesbotv4\\wwwroot\\james.jpg", ContentType = "image/png" };
                    //var activity = MessageFactory.Attachment(image);
                    //await context.SendActivity(activity);
                }
                else
                {
                    await context.SendActivity($"I talked to LUIS and she thinks your answer will be in the {topIntent} knowledge base.");

                    await context.SendActivity($"She gave a score of {score}");

                    if (true)
                    {
                        foreach (var intent in luisResult.Intents)
                        {
                            if (topIntent != intent.Key)
                            {
                                await context.SendActivity($"The {intent.Key} KB is another possibility with a score of {intent.Value.Value<string>("score")}.");
                            }
                        }
                    }

                    await context.SendActivity("Now I'm going to QnA Maker!");

                    var qnaMaker     = new Microsoft.Bot.Builder.Ai.QnA.QnAMaker(qnaMap[topIntent]);
                    var queryResults = await qnaMaker.GetAnswers(utterance);

                    if (queryResults != null && queryResults.Any())
                    {
                        var enumerator = queryResults.GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            await context.SendActivity($"Another possible answer is {enumerator.Current}");
                        }
                        await context.SendActivity(queryResults.First().Answer);
                    }
                    else
                    {
                        await context.SendActivity("I don't have an answer for you.");
                    }
                }
                break;

            default:
                break;
            }
        }