0
votes

I am programmatically building a QnA using C#. I want to programmatically obtain the answer of a question, to do so, I have used the documentation provided by Microsoft in the following link:

https://docs.microsoft.com/en-us/azure/cognitive-services/qnamaker/quickstarts/csharp#GetAnswers

However, if I follow the instructions there:

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace QnAMaker
{
    class Program
    {
        // NOTE: Replace this with a valid host name.
        static string host = "ENTER HOST HERE";

        // NOTE: Replace this with a valid endpoint key.
        // This is not your subscription key.
        // To get your endpoint keys, call the GET /endpointkeys method.
        static string endpoint_key = "ENTER KEY HERE";

        // NOTE: Replace this with a valid knowledge base ID.
        // Make sure you have published the knowledge base with the
        // POST /knowledgebases/{knowledge base ID} method.
        static string kb = "ENTER KB ID HERE";

        static string service = "/qnamaker";
        static string method = "/knowledgebases/" + kb + "/generateAnswer/";

        static string question = @"
{
    'question': 'Is the QnA Maker Service free?',
    'top': 3
}
";

        async static Task<string> Post(string uri, string body)
        {
            using (var client = new HttpClient())
            using (var request = new HttpRequestMessage())
            {
                request.Method = HttpMethod.Post;
                request.RequestUri = new Uri(uri);
                request.Content = new StringContent(body, Encoding.UTF8, "application/json");
                request.Headers.Add("Authorization", "EndpointKey " + endpoint_key);

                var response = await client.SendAsync(request);
                return await response.Content.ReadAsStringAsync();
            }
        }

        async static void GetAnswers()
        {
            var uri = host + service + method;
            Console.WriteLine("Calling " + uri + ".");
            var response = await Post(uri, question);
            Console.WriteLine(response);
            Console.WriteLine("Press any key to continue.");
        }

        static void Main(string[] args)
        {
            GetAnswers();
            Console.ReadLine();
        }
    }
}

Rather than getting an answer, I am getting a resource not found. While the others methods as update the knowledge base, work well with the same uri, does anybody know why is this happening?

1

1 Answers

0
votes

It's because some endpoints of the API are not generic, but are exposed on a host on your side.

As the documentation says, Replace the host value with the Website name for your QnA Maker subscription: the host is not https://westus.api.cognitive.microsoft.com/...

The architecture is the following and the endpoint you're looking for is on the green side, not the blue side: enter image description here

That's also why you can't find generateAnswer on the documentation https://westus.dev.cognitive.microsoft.com/docs/services/5a93fcf85b4ccd136866eb37/operations/5ac266295b4ccd1554da75ff

So go to https://www.qnamaker.ai/Home/MyServices, click on your QnA KB, publish it if not already done and then in the settings you will have the interesting details like below:

enter image description here