Skip to content

Instantly share code, notes, and snippets.

@jakkaj
Created April 9, 2017 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jakkaj/432796a53182d0c970ccf44d4ee87be6 to your computer and use it in GitHub Desktop.
Save jakkaj/432796a53182d0c970ccf44d4ee87be6 to your computer and use it in GitHub Desktop.
QnA Query maker example
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Net;
using System.Threading.Tasks;
using Autofac;
using EventBot.SupportLibrary.Contract;
using EventBot.SupportLibrary.Entity;
using Newtonsoft.Json;
using Xamling.Azure.Portable.Contract;
using XamlingCore.Portable.Data.Glue;
using XamlingCore.Portable.Model.Other;
namespace EventBot.QnAMaker
{
[Serializable]
public class QnaMakerKb
{
// External call (metered)
//private readonly Uri qnaBaseUri = new Uri("https://westus.api.cognitive.microsoft.com/qnamaker/v1.0");
// Internal call
private readonly Uri qnaBaseUri = new Uri("http://qnaservice.cloudapp.net");
private readonly string KbId = ConfigurationManager.AppSettings["QnaKnowledgeBaseId"];
private readonly string SubscriptionKey = ConfigurationManager.AppSettings["QnaSubscriptionKey"];
private IQuestionCacheService _questionCache;
private ILogService _logService;
public QnaMakerKb()
{
_questionCache = ContainerHost.Container.Resolve<IQuestionCacheService>();
_logService = ContainerHost.Container.Resolve<ILogService>();
}
// Sample HTTP Request:
// POST /knowledgebases/{KbId}/generateAnswer
// Host: https://westus.api.cognitive.microsoft.com/qnamaker/v1.0
// Ocp-Apim-Subscription-Key: {SubscriptionKey}
// Content-Type: application/json
// {"question":"hi"}
public async Task<QnaMakerResult> SearchFaqAsync(string question)
{
_logService.TrackTrace("QandAQuestion", XSeverityLevel.Information, new Dictionary<string, string>
{
{"Question", question }
});
var checkCache = await _questionCache.CheckCache(question);
if (checkCache != null)
{
_logService.TrackTrace("QandAQuestionCacheHit", XSeverityLevel.Information, new Dictionary<string, string>
{
{"Question", question }
});
return checkCache;
}
var responseString = string.Empty;
// External call (metered)
//var uri = new UriBuilder($"{qnaBaseUri}/knowledgebases/{this.KbId}/generateAnswer").Uri;
// Internal call
var uri = new UriBuilder($"{qnaBaseUri}/KbService.svc/v2/GetAnswer").Uri;
// External call (metered)
//var postBody = $"{{\"question\": \"{question}\"}}";
// Internal call
var postBody = $"{{\"question\": \"{question}\", \"kbId\": \"{KbId}\"}}";
//Send the POST request
using (var client = new WebClient())
{
client.Headers.Add("Content-Type", "application/json");
client.Headers.Add("Ocp-Apim-Subscription-Key", this.SubscriptionKey);
try
{
responseString = await client.UploadStringTaskAsync(uri, postBody);
}
catch (WebException we)
{
// TODO Error handling
throw;
}
}
var result = ConvertResponseFromJson(responseString);
if (result == null || result.Score <= 0)
{
_logService.TrackTrace("QandAQuestionNoAnswer", XSeverityLevel.Information, new Dictionary<string, string>
{
{"Question", question }
});
return null;
}
await _questionCache.SaveQuestion(question, result);
_logService.TrackTrace("QandAQuestionAnswerFound", XSeverityLevel.Information, new Dictionary<string, string>
{
{"Question", question },
{"Answer", result.Answer }
});
return result;
}
private QnaMakerResult ConvertResponseFromJson(string responseString)
{
QnaMakerResult response;
try
{
response = JsonConvert.DeserializeObject<QnaMakerResult>(responseString);
}
catch
{
throw new Exception("Unable to deserialize QnA Maker response string.");
}
return response;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment