Make your (Dynamics NAV) assistant

AIToday we will look at how to make an assistant. Despite the title, our assistant is not intended only for Dynamics NAV. In fact, the example shown here is written as extension for Dynamics Financials (Business edition). But a title saying  Make your Dynamics NAV/Financials (Business Edition)/Tenerife assistant would be – well outright ridiculous.

There are several ways to do this, I will illustrate one of them. Let’s start at the end : Cortana and Microsoft Cognitive Services. 

In the example here, I’m using QnA maker. For nice intro, watch this: Creating FAQ Bots  In short, with QnA Maker you can make your own BOT with simple actions like copy-paste, or uploading your knowledgebase document. The purpose is of course to help user solve basic problems on their own. Register for QnA Maker Service (you can use your Azure subscription for it), and once you created your knowledge base, which is really just a FAQ document with question/answer pairs, upload the document. Train the service, publish, and you got yourself a bot. Now all you need to do is to use the API to connect it to NAV. You can use it as it is, its just a web service, but since we’re looking for unified experience and simplicity here, we’ll add it to our solution. As an extension, naturally.

If you haven’t yet started with Developer’s Preview, now is the time. In this example, we will use REST Web Services, so once you get your Visual Studio Code up and running, have a look at this great examples on GitHub, GetAddressService  for a reference on how to use REST API with .al. But for even more detailed reference, check this excellent  post , it saved me a bit of a headache with a particular content-type nut, and I warmly recommend reading it.

The process is simple. We use the web service url, along with the access key to our cognitive service, call the Post method on the web service passing the question that was asked and retrieving the answer, or top X matching answers. Sample request should look like this:

POST /knowledgebases/<Your KB ID>/generateAnswer HTTP/1.1
Host: https://westus.api.cognitive.microsoft.com/qnamaker/v1.0
Ocp-Apim-Subscription-Key: <Your Subscription key>
Content-Type: application/json
Cache-Control: no-cache
{“question”: “Question goes here”}

So lets begin. You need to define these rest api vars:

httpclient, httprequestmessage, httpresponsemessage, httpheader, httpcontent,

and as step 1: add a request header to the httpclient.

 

httpclient.DefaultRequestHeaders.Add(‘Ocp-Apim-Subscription-Key’,<your subscription key>);

Next is to set service uri:

httprequest.SetRequestUri(STRSUBSTNO(HelpUri,KBValue));

 

and KbValue is your knowledge base id, all this you can read on settings page of your bot (QnA Maker) service.

Next , define the POST method on your request

httprequest.Method(‘Post’);
So add the question token
body := ‘{“question”: “‘ + question + ‘”,’+'”top”: 3}’;
httpcontent.WriteFrom(body);

Naturally, you should be doing all this properly in your solution, json objects and all, but polish it at your own time. Next, add the content-type to the header, and that needs to be ‘application/json’ or the call will fail. This is where things could be tricky,  if you didn’t know about this little trick – apparently we must remove the content-type before adding it again, with ‘application/json’ value.

httpcontent.GetHeaders(httpheader);

httpheader.Remove(‘Content-Type’);

httpheader.Add(‘Content-Type’,’application/json’);

And add the content to the header:

httprequest.Content(httpcontent);

 

The only thing left is to make the call:

httpclient.send(httprequest,httpresponse);
and read the response:
httpresponse.Content.ReadAs(answertxt);

and that’s it! So publish the extension and this is what it looks like (brace yourselves):

comprofile_ext

 

Admittedly, not a thing of beauty, what with web client limitations and on-the-fly sample. But as of a week ago, It is now possible to develop JavaScript control add-ins in AL!! If you haven’t tried it yet, look for one of Vjeko’s excellent posts or webinars/courses on the topic. Or visit EdX – lots of knowledge waiting to be grabbed there. So now you can pimp that assistant

Izzy

Ok, just kidding, this last one was good old c/al   – but pimp your own assistant 🙂

So conclusion: tired of answering the same old questions over and over again? Delegate it :)!

6 thoughts on “Make your (Dynamics NAV) assistant

  1. Hi! Nice post… i am trying this code but not found. The message of error is The request entity’s media type ‘text/plain’ is not supported for this resource.” but i am using your code.
    If first step i remove the TAG “Conent-type” and i add it again not found.
    Might you help me? Thank you

    Like

    1. I think i read your comment too fast, i did not see what you wrote about content type. That info (content-type) was provided by qna maker site. And at the time this post was written, qna maker was in preview and a free service. Now you have to sign up for the service and chose the pricing tier (though the lowest tier is still free). I believe they support multiple content types now, the most natural type to use is probably application/json. The following link will give you current endpoint and request format details on qna maker service: https://docs.microsoft.com/en-us/azure/cognitive-services/QnAMaker/how-to/metadata-generateanswer-usage

      Like

  2. Hi, do you think is posible to use with C/AL code? I have my QnA in Azure and would be great link it with my NAV 2016 until we can migrate to Business central

    Like

    1. Hi, yes it is. I wrote this a while ago (that is a while before i wrote the blog post), and it was originally in c/side. The code in the post was from later conversion to c/AL. There are a few (minor) differences, but nothing major

      Like

      1. I’m triying but I can’t connect 😦
        I’m using codeunit 1290 but I’m doing some thing wrong with qnamaker urls, maybe you still have the original code in c/AL?

        Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s