// </Main>

        // <GetQueryEndpointKey>
        private static async Task <String> GetQueryEndpointKey(IQnAMakerClient client)
        {
            var endpointKeysObject = await client.EndpointKeys.GetKeysAsync();

            return(endpointKeysObject.PrimaryEndpointKey);
        }
        // </CreateKBMethod>

        // <PublishKB>
        private static async Task PublishKb(IQnAMakerClient client, string kbId)
        {
            await client.Knowledgebase.PublishAsync(kbId);
        }
        // </GenerateAnswer>

        // <DeleteKB>
        private static async Task DeleteKB(IQnAMakerClient client, string kbId)
        {
            await client.Knowledgebase.DeleteAsync(kbId);
        }
Exemple #4
0
 /// <summary>
 /// Initializes a new instance of the <see cref="HomeController"/> class.
 /// </summary>
 /// <param name="configurationPovider">configurationPovider dependency injection.</param>
 /// <param name="qnaMakerClient">qnaMakerClient dependency injection.</param>
 public HomeController(IConfigurationDataProvider configurationPovider, IQnAMakerClient qnaMakerClient)
 {
     this.configurationPovider = configurationPovider;
     this.qnaMakerClient       = qnaMakerClient;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="QnaServiceProvider"/> class.
 /// </summary>
 /// <param name="configurationProvider">ConfigurationProvider fetch and store information in storage table.</param>
 /// <param name="optionsAccessor">A set of key/value application configuration properties.</param>
 /// <param name="qnaMakerClient">Qna service client.</param>
 public QnaServiceProvider(IConfigurationDataProvider configurationProvider, IOptionsMonitor <QnAMakerSettings> optionsAccessor, IQnAMakerClient qnaMakerClient)
 {
     this.configurationProvider = configurationProvider;
     this.qnaMakerClient        = qnaMakerClient;
     this.options = optionsAccessor.CurrentValue;
 }
        static void Main(string[] args)
        {
            if (args.Length != 4)
            {
                var exeName = "batchtesting.exe";
                Console.WriteLine("For Qna Maker GA");
                Console.WriteLine($"Usage: {exeName} <tsv-inputfile> <runtime-hostname> <runtime-endpointkey> <tsv-outputfile>");
                Console.WriteLine($"{exeName} input.tsv https://myhostname.azurewebsites.net 5397A838-2B74-4E55-8111-D60ED1D7CF7F output.tsv");
                Console.WriteLine("For QnA Maker managed (preview)");
                Console.WriteLine($"Usage: {exeName} <tsv-inputfile> <cs-hostname> <cs-endpointkey> <tsv-outputfile>");
                Console.WriteLine($"{exeName} input.tsv https://myhostname.cognitiveservices.azure.com b0863a25azsxdcf0b6855e9e988805ed output.tsv");
                Console.WriteLine();

                return;
            }

            var i           = 0;
            var inputFile   = args[i++];
            var runtimeHost = args[i++];
            var endpointKey = args[i++];
            var outputFile  = args[i++];

            var isQnAMakerV2 = CheckForQnAMakerV2(runtimeHost);

            var inputQueries   = File.ReadAllLines(inputFile);
            var inputQueryData = inputQueries.Select(x => GetTsvData(x)).ToList();

            IQnAMakerClient       qnaMakerClient        = null;
            QnAMakerRuntimeClient qnaMakerRuntimeClient = null;

            if (isQnAMakerV2)
            {
                qnaMakerClient = GetQnAMakerClient(endpointKey, runtimeHost);
            }
            else
            {
                qnaMakerRuntimeClient = new QnAMakerRuntimeClient(new EndpointKeyServiceClientCredentials(endpointKey))
                {
                    RuntimeEndpoint = runtimeHost
                };
            }

            var lineNumber       = 0;
            var answerSpanHeader = isQnAMakerV2 ? "\tAnswerSpanText\tAnswerSpanScore" : string.Empty;

            File.WriteAllText(outputFile, $"Line\tKbId\tQuery\tAnswer\tScore{answerSpanHeader}\tMetadata\tAnswerId\tExpectedAnswerId\tLabel{Environment.NewLine}");
            var watch = new Stopwatch();

            watch.Start();
            var maxLines = inputQueryData.Count;

            foreach (var queryData in inputQueryData)
            {
                try
                {
                    lineNumber++;
                    var(queryDto, kbId, expectedAnswerId) = GetQueryDTO(queryData, isQnAMakerV2);

                    QnASearchResultList response = null;

                    if (isQnAMakerV2)
                    {
                        response = qnaMakerClient.Knowledgebase.GenerateAnswerAsync(kbId, queryDto).Result;
                    }
                    else
                    {
                        response = qnaMakerRuntimeClient.Runtime.GenerateAnswerAsync(kbId, queryDto).Result;
                    }

                    var resultLine = new List <string>();
                    resultLine.Add(lineNumber.ToString());
                    resultLine.Add(kbId);
                    resultLine.Add(queryDto.Question);

                    // Add the first answer and its score
                    var firstResult = response.Answers.FirstOrDefault();
                    var answer      = firstResult?.Answer?.Replace("\n", "\\n");

                    resultLine.Add(answer);
                    resultLine.Add(firstResult?.Score?.ToString());

                    if (isQnAMakerV2 && firstResult?.AnswerSpan?.Text != null)
                    {
                        resultLine.Add(firstResult?.AnswerSpan?.Text);
                        resultLine.Add(firstResult?.AnswerSpan?.Score?.ToString());
                    }

                    // Add Metadata
                    var metaDataList = firstResult?.Metadata?.Select(x => $"{x.Name}:{x.Value}")?.ToList();
                    resultLine.Add(metaDataList == null ? string.Empty : string.Join("|", metaDataList));

                    // Add the QnaId
                    var firstQnaId = firstResult?.Id?.ToString();
                    resultLine.Add(firstQnaId);

                    // Add expected answer and label
                    if (!string.IsNullOrWhiteSpace(expectedAnswerId))
                    {
                        resultLine.Add(expectedAnswerId);
                        resultLine.Add(firstQnaId == expectedAnswerId ? "Correct" : "Incorrect");
                    }

                    var result = string.Join('\t', resultLine);
                    File.AppendAllText(outputFile, $"{result}{Environment.NewLine}");
                    PrintProgress(watch, lineNumber, maxLines);
                }
                catch (Exception ex)
                {
                    Console.WriteLine($"Error processing line : {lineNumber}, {ex}");
                }
            }
        }
        // </DownloadKB>

        // <UpdateKBMethod>
        private static async Task UpdateKB(IQnAMakerClient client, string kbId)
        {
            var urls = new List <string> {
                "https://docs.microsoft.com/azure/cognitive-services/QnAMaker/troubleshooting"
            };

            var updateOp = await client.Knowledgebase.UpdateAsync(kbId, new UpdateKbOperationDTO
            {
                // Create JSON of changes
                Add = new UpdateKbOperationDTOAdd
                {
                    QnaList = new List <QnADTO> {
                        new QnADTO {
                            Questions = new List <string> {
                                "bye",
                                "end",
                                "stop",
                                "quit",
                                "done"
                            },
                            Answer   = "goodbye",
                            Metadata = new List <MetadataDTO> {
                                new MetadataDTO {
                                    Name = "Category", Value = "Chitchat"
                                },
                                new MetadataDTO {
                                    Name = "Chitchat", Value = "end"
                                },
                            }
                        },
                        new QnADTO {
                            Questions = new List <string> {
                                "hello",
                                "hi",
                                "start"
                            },
                            Answer   = "Hello, please select from the list of questions or enter a new question to continue.",
                            Metadata = new List <MetadataDTO> {
                                new MetadataDTO {
                                    Name = "Category", Value = "Chitchat"
                                },
                                new MetadataDTO {
                                    Name = "Chitchat", Value = "begin"
                                }
                            },
                            Context = new QnADTOContext
                            {
                                IsContextOnly = false,
                                Prompts       = new List <PromptDTO>
                                {
                                    new PromptDTO
                                    {
                                        DisplayOrder = 1,
                                        DisplayText  = "Use REST",
                                        QnaId        = 1
                                    },
                                    new PromptDTO
                                    {
                                        DisplayOrder = 2,
                                        DisplayText  = "Use .NET NuGet package",
                                        QnaId        = 2
                                    },
                                }
                            }
                        },
                    },
                    Urls = urls
                },
                Update = null,
                Delete = null
            });;

            // Loop while operation is success
            updateOp = await MonitorOperation(client, updateOp);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="HomeController"/> class.
 /// </summary>
 /// <param name="configurationProvider">configurationProvider DI.</param>
 /// <param name="helpDataProvider">help content data provider instance.</param>
 /// <param name="qnaMakerClient">qnaMakerClient DI.</param>
 /// <param name="telemetryClient">telemetry client to trace logs.</param>
 public HomeController(IConfigurationProvider configurationProvider, IHelpDataProvider helpDataProvider, IQnAMakerClient qnaMakerClient, TelemetryClient telemetryClient)
 {
     this.configurationProvider = configurationProvider;
     this.qnaMakerClient        = qnaMakerClient;
     this.helpDataProvider      = helpDataProvider;
     this.telemetryClient       = telemetryClient;
 }
Exemple #9
0
 /// <summary>
 /// Initializes a new instance of the <see cref="MultiQnAMakerDialog"/> class.
 /// Dialog helper to generate dialogs.
 /// </summary>
 /// <param name="qnaMakerClient">Bot Services.</param>
 public MultiQnAMakerDialog(IQnAMakerClient qnaMakerClient, IConfiguration configuration) : base()
 {
     _qnaMakerClient = qnaMakerClient;
     _configuration  = configuration;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="QnaServiceProvider"/> class.
 /// </summary>
 /// <param name="configurationStorageProvider">storage provider.</param>
 /// <param name="qnaMakerClient">qna client.</param>
 public QnaServiceProvider(IConfigurationStorageProvider configurationStorageProvider, IQnAMakerClient qnaMakerClient)
 {
     this.configurationStorageProvider = configurationStorageProvider;
     this.qnaMakerClient = qnaMakerClient;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="QnaServiceProvider"/> class.
 /// </summary>
 /// <param name="configurationStorageProvider">storage provider.</param>
 /// <param name="configuration">configuration.</param>
 /// <param name="qnaMakerClient">qna service client.</param>
 /// <param name="qnaMakerRuntimeClient">qna service runtime client.</param>
 public QnaServiceProvider(IConfigurationStorageProvider configurationStorageProvider, IConfiguration configuration, IQnAMakerClient qnaMakerClient, IQnAMakerRuntimeClient qnaMakerRuntimeClient)
 {
     this.configurationStorageProvider = configurationStorageProvider;
     this.qnaMakerClient        = qnaMakerClient;
     this.qnaMakerRuntimeClient = qnaMakerRuntimeClient;
     this.scoreThreshold        = Convert.ToDouble(configuration["ScoreThreshold"]);
 }
Exemple #12
0
        private async Task <DialogTurnResult> ExecuteAdaptiveQnAMakerDialog(DialogContext dc, IQnAMakerClient qnaMaker, QnAMakerOptions qnamakerOptions, CancellationToken cancellationToken = default(CancellationToken))
        {
            var dialog = new QnAMakerActionBuilder(qnaMaker).BuildDialog(dc);

            // Set values for active dialog.
            var qnaDialogResponseOptions = new QnADialogResponseOptions
            {
                NoAnswer = NoAnswer ?? new StaticActivityTemplate(null),
                ActiveLearningCardTitle = ActiveLearningCardTitle ?? QnAMakerActionBuilder.DefaultCardTitle,
                CardNoMatchText         = CardNoMatchText ?? QnAMakerActionBuilder.DefaultCardNoMatchText,
                CardNoMatchResponse     = CardNoMatchResponse ?? new StaticActivityTemplate(null)
            };

            var dialogOptions = new Dictionary <string, object>
            {
                [QnAMakerActionBuilder.QnAOptions] = qnamakerOptions,
                [QnAMakerActionBuilder.QnADialogResponseOptions] = qnaDialogResponseOptions
            };

            return(await dc.BeginDialogAsync(QnAMakerActionBuilder.QnAMakerDialogName, dialogOptions, cancellationToken).ConfigureAwait(false));
        }