A Fluent API to create chatbots in Java (and why this is our third attempt to define a great Bot Definition Language!)
Tags: fluent, java, nlp, bot
Published: 2020-08-11 17:52:16

At <a href="https://xatkit.com/" target="_blank" rel="noopener noreferrer">Xatkit</a>, we continue our quest to find the optimal language for bot and chatbot development. Some time ago we explained our decision to move to a <a href="https://martinfowler.com/books/dsl.html" target="_blank" rel="noopener noreferrer">DSL</a> (Domain-Specific Language) based on <a href="https://xatkit.com/chatbot-dsl-state-machines-xatkit-language/" target="_blank" rel="noopener noreferrer">state machine semantics</a> to help you create more powerful bots.

This DSL was definitely an improvement over our first attempt at creating a chatbot DSL, but we quickly noticed some issues, nice-to-have features, or cumbersome things we still had to deal with. For instance, the execution language needed to integrate reusable functions/methods to support the complex bots we are working on. Dependency management was also a nightmare because of the multiple layers that needed to be configured.<strong> Most of these issues had nothing to do with the semantics of the language but with the syntax and infrastructure.</strong>

Therefore, we decided to keep the state machine semantics but change the language syntax by <strong>moving from an External DSL to an <a href="https://martinfowler.com/bliki/InternalDslStyle.html" target="_blank" rel="noopener noreferrer">Internal DSL</a>.</strong> An external DSL is an independent language (with its own grammar, parser, compiler,...). Internal DSLs are embedded in a general-purpose language, typically as a particular form of API, known as a Fluent API or <a href="https://martinfowler.com/bliki/FluentInterface.html">fluent interface</a>.

<strong>The move to an internal DSL brings several benefits</strong> that we hope you'll appreciate:
<ul>
 	<li>No need to learn a new language, just use Java</li>
 	<li>No need to use any specific tool to build Xatkit bots, any Java IDE will do</li>
 	<li>Benefit from all existing Java tools when creating, debugging and testing bots</li>
 	<li>Use the full power of the Java language (and all the libraries) to create complex bots (if needed).</li>
</ul>
All this while keeping well-defined and high-level semantics provided by our internal DSL / fluent interface. So, we are basically <strong>combining the benefits of having a specific chatbot DSL with the power of a general-purpose language. </strong>

We have now released a <a href="https://github.com/xatkit-bot-platform/xatkit" target="_blank" rel="noopener noreferrer">new version of Xatkit</a> and its runtime engine so that you can start using this new syntax in the creation of your bots. We'll be adding more documentation on the new <a href="https://github.com/xatkit-bot-platform/xatkit/wiki/Xatkit-DSL-constructs" target="_blank" rel="noopener noreferrer">Fluent Interface / DSL constructs</a> soon but let me present to you the key ones in this <a href="https://github.com/xatkit-bot-platform/xatkit-examples/tree/master/GreetingsBots/GreetingsBot-Extended" target="_blank" rel="noopener noreferrer">GreetingsBot example,</a> a simple bot that just says replies when it detects you're greeting it. You can find the <a href="https://github.com/xatkit-bot-platform/xatkit-examples/blob/master/GreetingsBots/GreetingsBot-Extended/src/main/java/com/xatkit/example/GreetingsBotExtended.java" target="_blank" rel="noopener noreferrer">full source bot code</a> here, in the following I'll just focus on a few core parts.
<h2>Intent definition</h2>
We can easily create the intents the bot should recognize (in this example, the greetings and how the user is feeling right now) and the training sentences to be used to teach the bot how to understand them (these sentences will be used, for instance, when configuring the bot to use <a href="https://github.com/xatkit-bot-platform/xatkit/wiki/Integrating-DialogFlow" target="_blank" rel="noopener noreferrer">DialogFlow as Intent Recognition Provider</a>).

{% gist https://gist.github.com/jcabot/0d139e33e1c02135a63dea055fc4281a %}

In a similar fashion, you can also define mapping <em>entities</em>  and parameters to collect relevant data from the user utterance during the matching process (to be used later on in the response, e.g. getting the user name and using it to answer her in a more personal way later on in the bot workflow).
<h2>Platform definition</h2>
Xatkit supports a number of platforms (and you can build your own!) so the next step is to declare what combinations of platforms your bot is going to be using (either calling actions on the platform or getting events from it). It could be more than one (e.g. our <a href="https://livablesoftware.com/slack-chatbot-github-repositories/" target="_blank" rel="noopener noreferrer">bots that let you talk with GitHub from Slack</a>). In this example, we use our <a href="https://github.com/xatkit-bot-platform/xatkit-react-platform" target="_blank" rel="noopener noreferrer">web-based React chat platform</a> as the only input/output of the bot.

{% gist https://gist.github.com/jcabot/586ec54aa0c3c7c66f18dfb72b9eec52 %}

<h2>Conversation flow and bot reactions definition</h2>
Finally for each intent we define the bot reply. As our <a href="https://xatkit.com/chatbot-dsl-state-machines-xatkit-language/" target="_blank" rel="noopener noreferrer">chatbot language is based on state-machine semantics</a>, we can define states, (conditional) transitions between states, default and local fallback states,... to create conversations as complex as needed.

{% gist https://gist.github.com/jcabot/92682bd84c1d9c36cf4528f672288c39 %}

As you can see, the <a href="https://blog.jooq.org/2012/01/05/the-java-fluent-api-designer-crash-course/" target="_blank" rel="noopener noreferrer">design of the Fluent API</a> relies heavily on method chaining to improve code legibility and reduce the need for intermediate variables. At the same time, the key concepts of bot design (intent, state, platform,...) are immediately available to the bot designer that doesn't need to get distracted by boilerplate or advanced Java code (unless s/he wants to).

By the way, we are aware that it's impossible to create a single language that caters to all types of chatbot creators. There is <strong>no such thing as a bot language to rule them all</strong>. The experiences we had with non-developers led to a conclusion: <b>as abstract as our DSL was it was still too technical to use</b>. So instead of keep pursuing this dream, we have focused on building a bot environment easy to use by developers, allowing them to reuse their preferred language, IDE, and tooling. We'll keep adding more docs and tutorials explaining the new syntax but some of the <a href="https://github.com/xatkit-bot-platform/xatkit/wiki/Xatkit-DSL-constructs" target="_blank" rel="noopener noreferrer">key methods are explained here</a>.

We're confident technical people will enjoy this new proposal of using a fluent interface for defining bots. While for non-technical people will develop other interfaces (e.g. see our <a href="https://xatkit.com/graphical-rendering-of-your-chatbot-models/" target="_blank" rel="noopener noreferrer">graphical bot representation </a>or even more (semi)automatic ways to create a bot from whatever information they already have) on top of it. Expect some news on these other fronts as well! And as always, let us know what you think about this new way of defining bots in Xatkit.