<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Botisan AI - Medium]]></title>
        <description><![CDATA[We share our experiences during our chatbot building journey. - Medium]]></description>
        <link>https://medium.com/botisan-ai?source=rss----6679d6fe6a41---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Botisan AI - Medium</title>
            <link>https://medium.com/botisan-ai?source=rss----6679d6fe6a41---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 18:40:14 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/botisan-ai" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Building Rasa’s DIET Classifier from Scratch using PyTorch — Part 2]]></title>
            <link>https://medium.com/botisan-ai/building-rasas-diet-classifier-from-scratch-using-pytorch-part-2-677659f889e7?source=rss----6679d6fe6a41---4</link>
            <guid isPermaLink="false">https://medium.com/p/677659f889e7</guid>
            <dc:creator><![CDATA[Simon Liang 西门良]]></dc:creator>
            <pubDate>Thu, 01 Dec 2022 15:46:42 GMT</pubDate>
            <atom:updated>2022-12-01T15:43:06.180Z</atom:updated>
            <content:encoded><![CDATA[<h3>Building Rasa’s DIET Classifier from Scratch using PyTorch — Part 2</h3><p>I am working on re-building Rasa’s DIET Classifier from the ground up using PyTorch. In <a href="https://medium.com/botisan-ai/building-rasas-diet-classifier-from-scratch-using-pytorch-part-1-a5f2a71982ac">Part 1</a> of the article, I built an intent classifier using pre-trained sentence features, which is essentially the “I” part of “DIET”. Before I get to the more interesting “T” part (”T” stands for “Transformers”), I realized that I missed an important detail, which is the loss calculations. As I re-watched the Rasa algorithm series for DIET and TED, I realized that both of these machine learning algorithms use an idea as backbone to calculate loss — Starspace.</p><p><a href="https://arxiv.org/pdf/1709.03856.pdf">Starspace</a> is an algorithm originally devised by Facebook for computing similarities across various types of embeddings, and what Rasa got out of this paper is mainly how the loss is computed. Instead of just calculating cross entropy loss between the correct input and label embedding similarities, it adds cross entropy loss between:</p><ul><li>positive input and negative label embedding similarities</li><li>positive input and negative input embedding similarities</li><li>positive label and negative label embedding similarities</li><li>positive label and negative input embedding similarities</li></ul><p>This combined loss function is designed to push/pull the embeddings so that similar ones are closer to each other and dissimilar ones are further apart.</p><p>Once this loss function is figured out, all I need to do is to implement it using PyTorch. Rasa’s implementation has a lot of branches when it builds the neural network and loss, since it has a lot of different configurations. Here I am implementing the main default branch for the loss — CrossEntropyLoss with all the positive / negative input / label embedding combinations, which is also described in the <a href="https://arxiv.org/pdf/2004.09936.pdf">DIET classifier paper</a>. Interestingly enough, while the margin ranking loss function used by Starspace is still in the DIET classifier source code, it is no longer the default for DIET.</p><p>I have updated the source code <a href="https://github.com/botisan-ai/diet-classifier-pytorch">on GitHub</a> so you guys can check it out (see the added code between part 1 and part 2 <a href="https://github.com/botisan-ai/diet-classifier-pytorch/compare/part-1...part-2?expand=1#diff-40e797082a0693f38a9ae0a78f8fe79678c9f0eb1dc2435829eaa372920d9e6a">here</a>). To demonstrate the difference, I am plotting the embeddings in 2D space between the old and new loss functions. Thankfully, Jina’s DocArray package has a handy plot_embeddings method in the DocumentArray class, which helps me visualize the embeddings very easily. The difference is quite big between these two loss functions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TiRn75ZMiNnExEW8LthDAQ@2x.png" /><figcaption>Embedding Clustering using just standard CrossEntropyLoss in Part 1</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6NY35oTVAABtqFj77Y7Oyw@2x.png" /><figcaption>Embedding Clustering using the newly implemented loss function in Part 2</figcaption></figure><p>Now with the properly implemented loss function, I can comfortably work on the transformer encoding layers in Part 3!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=677659f889e7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/botisan-ai/building-rasas-diet-classifier-from-scratch-using-pytorch-part-2-677659f889e7">Building Rasa’s DIET Classifier from Scratch using PyTorch — Part 2</a> was originally published in <a href="https://medium.com/botisan-ai">Botisan AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Building Rasa’s DIET Classifier from Scratch using PyTorch — Part 1]]></title>
            <link>https://medium.com/botisan-ai/building-rasas-diet-classifier-from-scratch-using-pytorch-part-1-a5f2a71982ac?source=rss----6679d6fe6a41---4</link>
            <guid isPermaLink="false">https://medium.com/p/a5f2a71982ac</guid>
            <category><![CDATA[nlp]]></category>
            <category><![CDATA[rasa]]></category>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[chatbot-development]]></category>
            <category><![CDATA[pytorch]]></category>
            <dc:creator><![CDATA[Simon Liang 西门良]]></dc:creator>
            <pubDate>Fri, 26 Aug 2022 05:32:54 GMT</pubDate>
            <atom:updated>2022-08-26T13:59:05.023Z</atom:updated>
            <content:encoded><![CDATA[<h3>Building Rasa’s DIET Classifier from Scratch using PyTorch — Part 1</h3><p>I love using <a href="https://rasa.com/">Rasa</a>, a Python chatbot framework that is powerful and modular, and at the same time, it just works out of the box without much tinkering. Everything about this framework is great. I have spent the past 3 years using the framework since its v0.10 release and now it is at v3.</p><p>After using it for a few years, I have found a few things I like and keep coming back to the framework for:</p><ol><li>Rasa’s modular NLU pipeline is super powerful, especially the DIET (Dual Intent-Entity Transformer) Classifier. I didn’t know at first, but you can actually control how the model is configured before the training, and you can make the model either as lightweight as you can (to fit in embedded devices like a Raspberry Pi), or as powerful as you need (by leveraging large pre-trained language models, adding transformer layers etc). Rasa also allows us to add custom components in the NLU pipeline so we can combine the best in the industry (like Facebook’s duckling project, and Microsoft’s Recognizers-Text project).</li><li>The configuration is straightforward. All we need to do is to fill in a few YAML files for training data and configuration, and Rasa will just work. In fact the default configuration usually gets us very far, even in production.</li><li>When it deploys, it becomes an HTTP service, so it is easy to integrate. They also provide an API for us to add custom action webhooks. We created a <a href="https://github.com/botisan-ai/rasa-action-server">TypeScript Rasa SDK</a> to we can interact with Rasa with the programming language we feel most comfortable with.</li></ol><p>I have also found a few things that we didn’t like about the framework:</p><ol><li>The startup time is a bit slow, since there are a lot of knobs inside the framework that it needs to dial on/off at startup.</li><li>The overall package is a bit large, and not easy to break apart. We wanted to set up a flexible NLU service that is preferably serverless, and having large Python dependencies does not allow us to do so.</li><li>We didn’t have to use TEDPolicy that much in our chatbot projects with clients, and a lot of our chatbots are still slot-filling-based and it is easier if we rely on forms. If we were to do that, then Rasa Core becomes a bit too bloated to use, and it is easier if we make Rasa do just one thing and one thing well — converting text into intent and entity labels (a.k.a NLU).</li></ol><p>For a while I was afraid of the framework, since I don’t have a lot of experience building ML models, the Rasa codebase has always been daunting to me. But after going through the DIET architecture explanation videos and read the specific source code around the Tensorflow Keras Layers and DIET Classifier definitions, I decide to try to move the DIET Classifier to PyTorch, as my deep learning study project.</p><p>By moving the model into PyTorch, it also allows for our team to ship the model directly with our <a href="https://github.com/botisan-ai/xstate-chatbot">NodeJS chatbot codebase</a> using ONNX runtime. It is going to help us build out lighter chatbots for clients.</p><p>If we look at the <a href="http://bl.ocks.org/koaning/raw/f40ca790612a03067caca2bde81e7aaf/">architecture blocks</a>, we found that the DIET Classifier is rather complex with a lot of layers. However, there are a lot of components we can strip out, if we just want a dead simple intent classifier. We just need to run through the sentence feature and the intent label feature through an embedding layer, and calculate the similarity between them. This way we got ourselves the simplest DIET Classifier architecture — the “I” (stands for “intent”) Classifier.</p><p>Since Rasa’s DIET Classifier is already filled with all sorts of configurations and code branches, it is hard to identify just the layers we need. Thankfully the architecture blocks diagram is interactive with configuration information, so I can search these configuration variables to track down the model layers.</p><p>After grokking around the Rasa codebase, I found the embedding layer and the loss function used by Rasa. The embedding layer is a simple Keras Dense layer to help change the input dimensions. This fully-connected layer will just get directly translated to a Linear module in PyTorch. For the similarity calculation, it is using a dot product, so if we have a sentence embedding, we want to calculate the dot product against all of the available intent labels, getting similarity scores between the sentence and all the intents.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/be77f573d00aa2fa4d32dafcbeede5a3/href">https://medium.com/media/be77f573d00aa2fa4d32dafcbeede5a3/href</a></iframe><p>This concludes the intent classification model. Now to train it we need to define a loss function. Since we are trying to identify a single intent from a sentence, it is best if we use cross-entropy loss. I put together a PyTorch Lightning Module for the classifier and I can define the training step directly in the module.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/212b052e41c9ad243700e7e82366a896/href">https://medium.com/media/212b052e41c9ad243700e7e82366a896/href</a></iframe><p>Before we are able to train, we need to provide a dataset to the trainer. I created a LightningDataModule, and I needed to read in the NLU training sentences (using a YAML file very similar to Rasa’s NLU training data format), turn them into feature vectors using a pre-trained model, and then run through the model. I am a big fan of PolyAI’s ConveRT model for English conversations, and in order to use it in both training time and make the NLU pipeline composable in inference time, I set up <a href="https://github.com/botisan-ai/convert-featurizer">an executor using Jina</a>. It provides a “DocArray in, DocArray out” executor contract, making it easy to combine executors together in a single request. I can easily isolate all the Tensorflow Hub dependencies from ConveRT in a Docker container, and reference it using a Jina Client.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d59ccba853451d3c5206202dee83464f/href">https://medium.com/media/d59ccba853451d3c5206202dee83464f/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5154162b9bf2bb15ae819ad25e1284da/href">https://medium.com/media/5154162b9bf2bb15ae819ad25e1284da/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f3ee3e2728547af5487079665f82273b/href">https://medium.com/media/f3ee3e2728547af5487079665f82273b/href</a></iframe><p>Training is very fast using PyTorch. I have an equivalent configuration in Rasa (turned off transformer layers, entity recognition and masking), and the training time is a bit slower. But I wasn’t sure if I am building the correct model, and whether or not the model would do its job — classify intents. I quickly put together an inference executor for running the trained classifier model in Jina, and set up a flow to test it.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/59a7a8f711ded847f39ae06c4ce3953e/href">https://medium.com/media/59a7a8f711ded847f39ae06c4ce3953e/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/46a05015e5710da6e6016b310b40d206/href">https://medium.com/media/46a05015e5710da6e6016b310b40d206/href</a></iframe><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f97a84fce63e2b510a9eaffd3c8648ba/href">https://medium.com/media/f97a84fce63e2b510a9eaffd3c8648ba/href</a></iframe><p>And voila! I was able to get pretty much the same result as the equivalent Rasa trained model! I checked the model checkpoints vs Rasa’s packaged model files, and mine is only a few hundred kilobytes vs Rasa’s 1.6MB in a tar archive. Obviously the comparison is unfair, but I can already start picturing that we could use a much lighter-weight model and directly ship alongside of the browser. Imagine that we replace ConveRT featurizer with GPT-3 embeddings, and we can have a full JavaScript-based smart AI chatbot ready to go!</p><p>Interestingly enough, there are actually a few DIET Classifier PyTorch implementations out there on GitHub, and after implementing it myself, I can safely say that these existing implementations are all valid, just that they are much slimmed down than Rasa’s own implementations.</p><p>I am glad to share my learning journey with you, and it doesn’t stop here! That’s why I have put a “Part 1” in the title. I plan on adding most of the other components from the DIET architecture into the model, and making mine composable as well. This will help me learn a lot about how to set up deep learning models myself, and hopefully I am able to get to a full DIET implementation soon! But for now, the intent classifier is ready to go!</p><p><a href="https://github.com/botisan-ai/diet-classifier-pytorch">GitHub - botisan-ai/diet-classifier-pytorch: PyTorch Implementation of Rasa&#39;s DIET Classifier.</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a5f2a71982ac" width="1" height="1" alt=""><hr><p><a href="https://medium.com/botisan-ai/building-rasas-diet-classifier-from-scratch-using-pytorch-part-1-a5f2a71982ac">Building Rasa’s DIET Classifier from Scratch using PyTorch — Part 1</a> was originally published in <a href="https://medium.com/botisan-ai">Botisan AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deploy Slack Bot on Vercel using NextJS for Free]]></title>
            <link>https://medium.com/botisan-ai/deploy-slack-bot-on-vercel-using-nextjs-for-free-17f8f63b480d?source=rss----6679d6fe6a41---4</link>
            <guid isPermaLink="false">https://medium.com/p/17f8f63b480d</guid>
            <category><![CDATA[slack]]></category>
            <category><![CDATA[nextjs]]></category>
            <category><![CDATA[chatbots]]></category>
            <category><![CDATA[vercel]]></category>
            <category><![CDATA[slackbot]]></category>
            <dc:creator><![CDATA[Simon Liang 西门良]]></dc:creator>
            <pubDate>Fri, 26 Aug 2022 02:24:13 GMT</pubDate>
            <atom:updated>2022-04-12T08:34:02.242Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*cSnKl5uCf5raRgBvnC4qTg.jpeg" /><figcaption>Image by <a href="https://unsplash.com/@austindistel">Austin Distel</a></figcaption></figure><p>I have been writing Slack Bots / Slack Apps for the past two years and I have been using @slack/bolt NodeJS framework for my apps and bots. It is rather easy to start with since it is no different from an Express application, and it provides a lot of middlewares for reacting to slash commands, incoming messages, and interactions (button clicks, dropdown selection, etc).</p><p>I am also a huge fan of Serverless. I started writing my first lambda to download DraftKing’s NBA draft information for a side project of mine, and it has been running a few times a day restlessly for the past 7 years. At X-Tech we also help teams and enterprises embrace Serverless as much as possible.</p><p>The Slack App I have been writing is <a href="https://x-late.x-tech.io/">X-late</a>, a translation bot that can help multi-lingual teams quickly connect and work together without any language barrier. Our team is multi-lingual and sometimes we need to connect our clients and teammate who are outside of China, to our developer and designer teammates in China, and most of the translation bots on Slack didn’t provide good enough translations to do daily collaboration. So I decided to build our own and it has been working very well for us. It was quickly put together as a NodeJS app based on @slack/bolt and it was deployed on my AWS account.</p><p>Lately I am in love with the <a href="https://vercel.com/home">Vercel platform</a>. It provides a way for me and my team to quickly put up websites written in React for free. Recently I wonder if there is a way I can move X-late into a Serverless app on Vercel. So I decided to take a crack at it.</p><p>The Slack Bolt documentation already provides a boilerplate for making the bot run on AWS Lambda. But since it is not compatible with Vercel, so I decided to see if I can hack my way around it. I came across an NPM package called next-connect which can set up express-like routers in a NextJS project. I then forked the ExpressReceiver from @slack/bolt and turn it into NextConnectReceiver which is powered by next-connect, and Voila! I can now set up a simple Slack Bot and deploy on Vercel for free!</p><p>I have most of my work open sourced <a href="https://github.com/lhr0909/nextjs-slack-bolt-example">on my GitHub</a> so please feel free to fork and try it out. I am going to publish the NextConnectReceiver to a standalone NPM package soon, but for now you can just take my repository as a template and start hacking!</p><p>The only caveat I want to remind everyone when building a Slack Bot serverlessly, is the fact that the “server” is short-lived, which means that, when a request is done, the app will turn off, so any logic that is outside of the request handler will not be run. Also, Slack has a request timeout, so your bot is supposed to acknowledge and respond to the request as soon as it is received. With this in mind, you will want to make sure that your bot is not doing anything too intensive for it to be serverless-compatible.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17f8f63b480d" width="1" height="1" alt=""><hr><p><a href="https://medium.com/botisan-ai/deploy-slack-bot-on-vercel-using-nextjs-for-free-17f8f63b480d">Deploy Slack Bot on Vercel using NextJS for Free</a> was originally published in <a href="https://medium.com/botisan-ai">Botisan AI</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>