Leverage QnA Maker Search within a Client Application
In this post, App Dev Manager Patrick King explains how to integrate a QnA Maker knowledgebase into a client application.
QnA Maker is an Azure Cognitive Service that enables you to ask questions and get answers from a knowledgebase built from your own documentation. QnA Maker also makes it very simple to add an intelligent chatbot interface to query your KB conversationally, but usage of QnA Maker is not limited to use in Chatbot apps. I recently worked on a proof-of-concept where the goal was to provide Q&A style help within a Windows application, and found that it is easy to leverage QnA Maker to search a knowledgebase from almost any kind of application using its Web-based API.
Step 1 – Provision a QnA Maker Knowledgebase
The first steps are to create the QnA Maker service, load your documents into it, and create the knowledgebase. Those steps are covered in the Quickstart: Create, train, and publish your QnA Maker knowledge base.
Step 2 – Collect Settings Needed by the API
QnA Maker exposes a runtime API that is used to submit questions and get results via the GenerateAnswer API method. You need three settings to call the runtime API, which you can only retrieve once the knowledgebase has been created because some are specific to the knowledgebase.
Once you have created the knowledge base, retrieve the settings from the QnA Maker knowledgebase settings by going to the QnA portal knowledgebase list and clicking the View Code button for the knowledgebase you want to connect to. The settings are:
- Runtime Endpoint: the URL of the Runtime API service, which is an Azure App Service Web Site that is provisioned by QnA Maker.
- Runtime Key: the security key your code uses to access the API & Knowledgebase
- Knowledgebase Id: the unique ID of the knowledgebase you are accessing.
Step 3 –Call the QnA Maker Runtime API
To query your knowledgebase, submit the user question in a call to the QnA Maker Runtime API’s GenerateAnswer method as illustrated below. The GenerateAnswer method returns question/answer pairs that matched your question along with the source document name or URL that the answer was found in.
Calling the QnA Maker Runtime API requires that you have the settings we discussed in Step 2 above, and that you can make an outbound call on port 443 to the runtime endpoint. While you can make the call as a direct http request, several SDK’s exist to lessen the effort.
- Help documentation for calling GenerateAnswer using REST calls in C# is here,.
- An example of calling GenerateAnswer using the QnA Maker SDK in C# is here
Below is an example of iterating over the results and transferring them into a custom class. The results list is in the Answers property of the QnASearchResult:
That’s all it takes to get started using a QnA Maker knowledgebase in your own application. You can find more help and examples in the QnA Maker documentation.