Chat with your SAP system using SCP and Google’s api.ai
Have you talked with IKEAS’ chat-bot Anna, and maybe you have asked her about stock information for one of their products, or the opening hours of the nearest warehouse? Did you know that it’s not so hard to build the same functionality using SAP on-prem or cloud solutions? This blog article will give you an idea of how to build a chat bot using SAP Cloud Platform and Google’s api.ai.
Natural Language Processing
Chat bots can be built using a combination of the SAP Cloud Platform (SCP) as well as a provider of natural language processing (NLP). In this example, we use services from the Google acquired company api.ai originated in Palo Alto. NLP supports the interpretation of user’s intention and passes it forward to the SCP. It uses machine learning to interpret variations of a topic, so that the user does not have to say exactly what the bot is expecting, but can talk in a way that feels normal to the user.
Set up your api.ai agent
This part has been quite well explained in blogs and videos from the nice people at api.ai. Here’s a few guides that should help you through the process:
- Whatch this: https://docs.api.ai/v25/docs/apiai-in-2-minutes-1
- Do that: https://docs.api.ai/docs/get-started
After following the instruction, preferably with your own idea in mind, you should be able to test it out using api.ai’s own user interface simulator.
Don’t be too fancy in your initial prototype agent. Just make sure that you can do some basic communication with some defined intent, action and a parameter.
Prepare your SAP Cloud Platform.
Great news: Your brand new api.ai agent speaks JSON. That makes everything quite easy in terms of handling the messages being passed to and from the platform.
On the SAP Cloud Platform side, you would want to build a webhook in Java that can interpret this JSON stream and enrich it with whatever business related data you desire. I will not go into details about creating Java applications in Eclipse and deployment on SCP – so many great people have done that better than I ever can. Do a quick search or take the openSAP course SCP Essentials.
Remember to use the following annotation:
In your return JSON you should at least fill in the parameters:
As a starter you may want to just enter dummy values in the parameters test your application in Postman or some other tool of your choice to see that a JSON is generated.
Connecting the agent to SAP Cloud Platform
By now you have your agent running on api.ai and a webhook running on SCP. Now, let’s get your agent to talk to SCP. For that to happen, you need your webhook URL. You get that in your SCP cockpit.
In your api.ai you need to go to your agent “Fulfillment” page. Make sure that it is enabled, and add your webhook URL is placed in the URL parameter.
If your webhook isn’t publicly available, this is also where you would add username/password or header keys. Make sure you remember to hit “Save” before you continue.
…this is where you would add username/password or header keys.
Then, still in your api.ai cockpit, you should be able to to back to the intent that you wish to pass on to SCP and check the “Use webhook” checkbox at the very bottom. What will happen now is that the JSON will be passed on from your agent to the URL you provided in the fulfillment section.
This can be a bit hard to debug, so again you should do this in baby steps. Try to alter the JSON with adding to it’s response first, without caring about the input intents or parameters. When you get that working, use your JSON weapon of choice to interpret the intent and parameters.
Add business value
When you have managed to connect the dots and are able to pass a reply back to your agent, you can start using the intent and the parameters on the SCP side. That is where the good bits are, because now you can use all the super powers of your cloud products or backend system of record to enrich your agents response. This is really where your business value is. For instance, you can create a simple destination in SCP that reaches our to your SAP backend system, pass the intent and parameter back to an ABAP function that returns some business data. Your SCP application can transform the return into a meaningful sentence and pass it on to your agent. Confused? Hope not. Just try it out!
Add a nice user interface using the many options of api.ai and voila!
Ok, it may not be that simple to get business value from your creation. First of all, finding the perfect use case within your organization can be quite hard. In addition to that, setting up the logic on your agent can be somewhat of a hassle. If you want to make a full scale production bot I would recommend reading up on (and testing) the CVS or JSON based upload of entities on api.ai. It will make your life easier, as your data sets probably are big and the worst of it all: They change over time. Intents can also be uploaded with JSON, and you can train your agent using text files.
Think big – but start small. Thanks for reading and good luck!
(ps. Check this out to avoid the major bloopers: https://chatbot.fail/ )
About the author:
Simen Huuse is a Norwegian native, educated with a MSc in neuroinformatics from the University of Edinburgh. He has been passionate about SAP since 2005. He currently lives in Palo Alto, California with his wife and two kids.