public async Task <QnAMakerResults> QueryServiceAsync(Uri uri, QnAMakerRequestBody postBody, string authKey) { //Extremely simplified for the sake of the workshop. //Note in a production application, you would not want to continually create and dispose of HttpClient in this way! using (var client = new HttpClient()) { client.DefaultRequestHeaders.Add("Authorization", authKey); var response = await client.PostAsync(uri, new StringContent(JsonConvert.SerializeObject(postBody), Encoding.UTF8, "application/json")); var json = await response.Content.ReadAsStringAsync(); try { var jObject = JObject.Parse(json); var answers = jObject["answers"]; if (answers == null) { return(new QnAMakerResults()); } return(new QnAMakerResults(JsonConvert.DeserializeObject <List <QnAMakerResult> >(answers.ToString()))); } catch (JsonException ex) { throw new ArgumentException("Unable to deserialize the QnA service response.", ex); } } }
public async Task QnATests() { string text = "asd"; Uri uri = new Uri("http://test"); QnAMakerRequestBody postBody = new QnAMakerRequestBody(); string subscriptionKey; QnAMakerResults qnaResults1 = JsonConvert.DeserializeObject <QnAMakerResults>( "{\"answers\":[{\"answer\":\"Test answer 1.1\",\"questions\":[\"Test question 1.1\",\"Test alternate phrase 1.1\"],\"score\":88.5},{\"answer\":\"Test answer 1.2\",\"questions\":[\"Test question 1.2\",\"Test alternate phrase 1.2\"],\"score\":58.3},{\"answer\":\"Test answer 1.3\",\"questions\":[\"Test question 1.3\",\"Test alternate phrase 1.3\"],\"score\":42.7}]}"); string expectedResponse1 = "Test answer 1.1"; Mock <IQnAService> qnaServiceMock1 = new Mock <IQnAService>(); qnaServiceMock1.Setup(x => x.QueryServiceAsync(It.IsAny <Uri>(), It.IsAny <QnAMakerRequestBody>(), It.IsAny <string>())).ReturnsAsync(qnaResults1); QnAMakerResults qnaResults2 = JsonConvert.DeserializeObject <QnAMakerResults>( "{\"answers\":[{\"answer\":\"Test answer 2.1\",\"questions\":[\"Test question 2.1\",\"Test alternate phrase 2.1\"],\"score\":98.5},{\"answer\":\"Test answer 2.2\",\"questions\":[\"Test question 2.2\",\"Test alternate phrase 2.2\"],\"score\":78.3},{\"answer\":\"Test answer 2.3\",\"questions\":[\"Test question 2.3\",\"Test alternate phrase 2.3\"],\"score\":40.5}]}"); string expectedResponse2 = "Test answer 2.1"; Mock <IQnAService> qnaServiceMock2 = new Mock <IQnAService>(); qnaServiceMock2.Setup(x => x.QueryServiceAsync(It.IsAny <Uri>(), It.IsAny <QnAMakerRequestBody>(), It.IsAny <string>())).ReturnsAsync(qnaResults2); var qnaDialog1 = new QnADialog(qnaServiceMock1.Object); await this.TestQnADialogResponse(qnaDialog1, expectedResponse1); var qnaDialog2 = new QnADialog(qnaServiceMock1.Object, qnaServiceMock2.Object); await this.TestQnADialogResponse(qnaDialog2, expectedResponse2); return; }
public Uri BuildRequest(string queryText, out QnAMakerRequestBody postBody, out string authKey) { var builder = new UriBuilder($"{_qnaHost}/knowledgebases/{_qnAMakerAttribute.KnowledgebaseId}/generateAnswer"); postBody = new QnAMakerRequestBody { question = queryText, top = _qnAMakerAttribute.Top }; authKey = _qnAMakerAttribute.AuthKey; return(builder.Uri); }