<?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[Stories by bharath reddy on Medium]]></title>
        <description><![CDATA[Stories by bharath reddy on Medium]]></description>
        <link>https://medium.com/@bharathreddy028?source=rss-fee736304085------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*V32d4Wu16pTU0lnfjpl0Aw.jpeg</url>
            <title>Stories by bharath reddy on Medium</title>
            <link>https://medium.com/@bharathreddy028?source=rss-fee736304085------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 02:25:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@bharathreddy028/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Rasa chatbot concepts & Implementation]]></title>
            <link>https://medium.com/@bharathreddy028/rasa-chatbot-concepts-implementation-8cacc3aade29?source=rss-fee736304085------2</link>
            <guid isPermaLink="false">https://medium.com/p/8cacc3aade29</guid>
            <dc:creator><![CDATA[bharath reddy]]></dc:creator>
            <pubDate>Fri, 01 Sep 2023 08:27:16 GMT</pubDate>
            <atom:updated>2023-09-01T08:27:16.981Z</atom:updated>
            <content:encoded><![CDATA[<p>Before we dive into the details of how we were building our driver’s assistant agent, it’s useful to define the key concepts of chatbot development:</p><ul><li><strong>Utterance </strong>is anything the user says to the bot, for example, “Get me to 221b Baker St”, “Drive from my current location to Sherlock Holmes museum avoiding the center of the city”, “Start navigation”.</li><li><strong>Intent </strong>is the user’s goal when they are saying an utterance, for example in the examples above, the user wants to be navigated to a place, and we could call the intent “navigate_to”.</li><li><strong>Entities</strong> can be seen as “parameters” of an intent, making it specific and allowing to understand the exact action the user wants to be performed. Entities typically have types assigned to them. In the examples above, “221b Baker St”, ”my current location”, “Sherlock Holmes museum” and “the center of the city” are all entities. Their types could be “destination” for “221b Baker St” and “Sherlock Holmes museum”; “origin” for ”my current location”; and “avoid_point” for “the center of the city”.</li><li><strong>Response </strong>is anything the bot says in reaction to the user’s utterances, e.g. “you are welcome” in response to “thank you”.</li><li><strong>Action </strong>is something that the bot needs to do to adequately fulfill the goal of the user. For many intents, a simple response is not enough: the bot should not just say “starting navigation” in response to “navigate_to” intent, it should also memorise the destination, origin, points to be included or avoided, and actually connect to a maps API, calculate the route according to the given parameters, and start navigating.</li><li><strong>Conversation flow</strong> is the way a conversation develops over a few conversational turns. We want the interaction of the user and the chatbot to be smooth and natural, which would indicate that we designed the conversation flow well.</li></ul><p>The scheme below shows an example of steps involved in a user/bot interaction:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*zIcPBh1ANx2CymMU.jpg" /></figure><p><em>Fig. 1. An example scheme of bot-user interaction</em></p><h3>How do you develop a chatbot?</h3><p>One reason why developing a chatbot is a heavy task for a beginner is that it consists of two large, equally complex components.</p><p>First, there is the <strong>conversation design</strong> part, which includes answering the following questions:</p><ul><li>What is the user expected to say/request?</li><li>Which variants of a request are the same request and which should be treated differently?</li><li>How is the bot expected to reply and act?</li><li>What are the possible ways a conversation can go?</li></ul><p>and more. This part is largely independent of the chatbot tool. Working on the conversation design of your bot is an iterative process: there will always be some unexpected requests or turns or conversation when your users are interacting with the bot. So, you start with it, but don’t expect to finish the conversation design of your bot before you proceed.</p><p>Second there is <strong>implementation </strong>which deals with the following questions:</p><ul><li>How do we formalise the behaviour we want?</li><li>What data do we need, in which formats? How do we obtain the data?</li><li>Which packages do we need?</li><li>Which model do we train?</li><li>How do we test?</li><li>What code do we need to write?</li></ul><p>This part depends on the tool you choose, and will also impact the conversation design as you identify wrong conversation during testing.</p><h3>Choice of a chatbot tool</h3><p>There are many tools for chatbot development: Google’s <a href="https://cloud.google.com/dialogflow/"><strong>Dialogflow</strong></a>, Amazon <a href="https://aws.amazon.com/lex/"><strong>Lex</strong></a>, <a href="https://rasa.com/"><strong>Rasa</strong></a>, and more. All tools have their own advantages and disadvantages with regards to being open- or closed-source, price, necessity (or absence thereof) to write any code, complexity of installation and getting started, and so on.</p><p>No one would like to realise that the tool they have been using for months does not have the desired functionality, so we all want to get it right from the beginning. But when you are just starting your chatbot journey and still defining what exactly you want from your bot, it can be daunting to define which tool would suit your needs best. After a good deal of research, we chose Rasa because:</p><ul><li>its core component is open-source and free</li><li>it allows to incorporate many external state-of-the-art NLP models</li><li>it is highly customizable and flexible (more on that later)</li><li>it uses Python which is our language of choice</li><li>It has an easy-to-use command-line interface for common tasks such as training, testing or running models</li><li>it has great documentation, written and video tutorials (check out “<a href="https://www.youtube.com/watch?v=Ap62n_YAVZ8">Conversational AI with Rasa</a>” series on Youtube).</li></ul><h3>Creating a Rasa bot</h3><h3>Installing Rasa</h3><p>Rasa Open Source packages are available in Python Package Index Repository (PyPI). The default installation can be performed by using pip install rasa command and it requires python version &gt;=3.7, &lt;3.9. But some of the most useful functionality, such as the most state-of-the-art transformer-based NLP models, are not included in the default installation, so we’d recommend using pip install rasa[full] instead.</p><h3>Rasa models</h3><p>It’s very important to understand that Rasa agents consist of two models: an <strong>NLU model</strong> and a <strong>core model</strong>. The NLU model is responsible for recognising the <strong>intents </strong>of the user’s utterances and for extracting the <strong>entities </strong>present in them. The core model is responsible for managing the <strong>conversation flow</strong>: memorising the entities, producing the correct response and/or action, understanding when it’s time to wait for the next user’s utterance, and so on. The two models are trained on different data. It is possible to train NLU and core models separately with Rasa, but to get a complete agent capable of interacting with a user, we need to have both components.</p><p>Although the core model decides what is the correct action to perform, it does not actually perform it. There is a separate <strong>action server </strong>that is in charge of running actions.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/278/0*lB8d1kEhhYah9dVe.jpg" /></figure><p><em>Fig.2. Structure of a Rasa bot</em></p><h3>Creating a project</h3><p>To train a Rasa model, we need the data for the NLU component, for the core component, and also we need to define responses and actions the bot is expected to perform. Hence, the file structure needed to train a Rasa bot is quite complex. But luckily, you do not need to set it up manually, you can run rasa init in a terminal to create your first project. Although you would get the correct folder structure, there are a lot of sub-folders and files, and figuring out what to do next can be overwhelming. So let’s take a closer look at what’s in there.</p><h3>What does a Rasa bot consist of?</h3><p>The <strong>folder structure</strong> of our newly created agent is as follows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/327/0*N9WYVLdal1TKknJr.jpg" /></figure><p><em>Fig.3. Folder structure of a Rasa project</em></p><p>You’d typically start creating a bot from some amount of NLU training data, then core data and actions. But we are going to talk about actions first because we’ll need some concepts for explaining training data later on.</p><h4>Custom actions</h4><p>actions.py file defines <strong>custom actions</strong> that a bot can perform in reply to user’s commands. This is one of Rasa’s most powerful <strong>customisation </strong>capabilities: you can really do a lot with custom actions.</p><p>Two key components used in actions are the <a href="https://rasa.com/docs/action-server/sdk-dispatcher/"><strong>dispatcher</strong></a>, which can generate <strong>responses</strong>, and the <a href="https://rasa.com/docs/action-server/sdk-tracker/"><strong>tracker</strong></a> which stores the bot’s <strong>memory</strong>. The bot’s memory contains the information about what the user said: text of utterances, intents, entities.</p><p>A separate part of Rasa bot’s memory are <strong>slots </strong>— variables where you can store important information. Slots can be of various types such as string, list, bool, etc. You don’t always need custom actions to work with slots: in Rasa 2.x, a slot is filled automatically with an entity of the same name (in Rasa 3.x, you need to specify which entity should fill which slot), e.g. a “destination” slot will be filled when a “destination” entity is extracted from the input utterance.</p><p>However, in some cases, you might want to fill slots in a different way. In this case, you can use slot names that differ from entity names, and fill them through actions.</p><p>For example, when a user gives the bot a destination, we might want to check whether the “main_destination” slot is already filled: if it is, we add the new destination to the “include-point” list, instead of rewriting the main destination:</p><pre>class ActionAddDestinaton(Action):<br>    def name(self) -&gt; Text:<br>        return &quot;action_add_destination&quot;<br>    def run(self, dispatcher: CollectingDispatcher,<br>            tracker: Tracker,<br>            domain: Dict[Text, Any]) -&gt; List[Dict[Text, Any]]:<br>        entity_to = next(tracker.get_latest_entity_values(entity_type=&quot;location&quot;, entity_role=&quot;to&quot;), None)<br>        slot_main_destination = tracker.get_slot(&#39;main_destination&#39;)<br>        slot_include_point = tracker.get_slot(&#39;include-point&#39;)<br>        if slot_main_destination is None:<br>            return [SlotSet(&quot;main_destination&quot;, entity_to)]<br>        else:<br>         if slot_include_point is not None:<br>             slot_include_point.append(entity_include_point)<br>else:<br>slot_include_point = [entity_include_point]<br>            return [SlotSet(&quot;include-point&quot;, entity_to)]</pre><p>A few points to note:</p><ul><li>We would use return [SlotSet(&quot;main_destination&quot;, entity_to), SlotSet(&quot;include-point&quot;, slot_include_point ), …] to fill several slots at a time.</li><li>To add a new item to a list slot (include-point), we first read the slot value by slot_include_point = tracker.get_slot(&#39;include-point&#39;), then append items to slot_include_point, and then running return [SlotSet(&quot;include-point&quot;, entity_to)].</li><li>name() function defines the name we use to refer to the action in core training data (action_add_destination in this example).</li></ul><p>Another cool feature that can be implemented using custom actions is using a <strong>database </strong>to store <strong>user-specific information</strong>, e.g. a home address. We can write an action saving items to a database:</p><pre>class ActionAddToDatabase(Action):<br>    def name(self) -&gt; Text:<br>        return &quot;action_add_to_database&quot;<br>    def run(self, dispatcher: CollectingDispatcher,<br>            tracker: Tracker,<br>            domain: Dict[Text, Any]) -&gt; List[Dict[Text, Any]]:<br>        entity_location = next(tracker.get_latest_entity_values(entity_type=&quot;location&quot;), None)<br>        entity_db_item_name = next(<br>            tracker.get_latest_entity_values(entity_type=&quot;location&quot;, entity_role=&quot;to_database&quot;), None)<br>        with open(&#39;utils/database.json&#39;, &#39;r&#39;, encoding = &#39;utf-8&#39;) as db:<br>            database = json.load(db)<br>        database.update({entity_db_item_name: entity_location})<br>        with open(&#39;utils/database.json&#39;, &#39;w&#39;, encoding = &#39;utf-8&#39;) as db:<br>            json.dump(database, db)<br>        return []</pre><p>When a user triggers the corresponding intent, database files gets updated:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nwS3Esa8t7sclj8v.jpg" /></figure><p><em>Source: Omdena</em></p><p>After that, we just need to add a few more lines to our ActionAddDestinaton to read that information back:</p><pre>with open(&#39;utils/database.json&#39;, &#39;r&#39;, encoding = &#39;utf-8&#39;) as db:<br>            database = json.load(db)<br>        if entity_to in database:<br>            entity_to = database[entity_to]</pre><p>The user can now ask to go home, instead of providing the full address each time:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/922/0*yLUt-1I3MevxnXYn.jpg" /></figure><p><em>Source: Omdena</em></p><p>We can further use a database to store the user’s <strong>navigation history</strong>, e.g:</p><pre>database = {<br>    &quot;destinations&quot;: [<br>        [&quot;5 main street&quot;, &quot;17.50 23.11.21&quot;],<br>        [&quot;12 royal court drive&quot;, &quot;17.45 16.11.21&quot;],<br>        [&quot;12 royal court drive&quot;, &quot;17.51 09.11.21&quot;],<br>        [&quot;5 main street&quot;, &quot;17.48 02.11.21&quot;],<br>        [&quot;5 main street&quot;, &quot;17.49 26.10.21&quot;],<br>    ]<br>}</pre><p>We can notice that our user tends to drive to the same places at similar times. If he triggers the navigate_to intent at one of those times without giving a destination, it would make sense to <strong>suggest </strong>the place where he usually goes at this time. We add a function to do this:</p><pre>def get_frequent_destination(<br>   database, weeks_limit=5, weekly_events_min_count=3, daily_events_min_count=10<br>):<br>   time_now = datetime.now()<br>   weekday_now = time_now.weekday()<br>   hour_now = time_now.hour<br>   time_limit = time_now - timedelta(weeks=weeks_limit)<br>   time_limit = time_limit<br>   destinations_by_day_and_time = Counter()<br>   destinations_by_time = Counter()<br>   if &quot;destinations&quot; in database:<br>       for destination in database[&quot;destinations&quot;]:<br>           address, trip_date = destination<br>           address = address.lower()<br>           trip_date = datetime.strptime(trip_date, date_string_pattern)<br>           weekday = trip_date.weekday()<br>           hour = trip_date.hour<br>           if trip_date &gt;= time_limit:<br>               if weekday == weekday_now and hour == hour_now:<br>                   destinations_by_day_and_time[address] += 1<br>               if hour == hour_now:<br>                   destinations_by_time[address] += 1<br>   if len(destinations_by_day_and_time) &gt; 0 or len(destinations_by_time) &gt; 0:<br>       top_destination, count = destinations_by_day_and_time.most_common(1)[0]<br>       if count &gt;= weekly_events_min_count:<br>           return top_destination<br>       else:<br>           top_destination, count = destinations_by_time.most_common(1)[0]<br>           if count &gt;= daily_events_min_count:<br>               return top_destination<br>   return</pre><p>Now we see the following behaviour — just what we wanted:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*AXh_DLirbDhbIV30.jpg" /></figure><p><em>Source: Omdena</em></p><p>Finally, one more fun feature we created using actions and slots is <strong>customising</strong> the bot’s <strong>response </strong>to some parameters of the user that are calculated based on his previous utterance(s). A very simple implementation of this feature is to store <strong>average length </strong>of every intent, mark the user as “talkative” if his/her utterance is longer than average, “not talkative” otherwise, and return a longer/shorter response accordingly. We take entities’ lengths to be 1: “Drive to 221B Baker Street” and “Drive to London” should be considered the same length. We use a custom action to access the text of the last user’s message, entities in it, do some length calculations (tokenising simply by white space), and fill the is_talkative slot:</p><pre>class ActionSetUserFeats(Action):<br>    def name(self) -&gt; Text:<br>        return &quot;action_set_user_feats&quot;<br>    def run(self, dispatcher: CollectingDispatcher,<br>            tracker: Tracker,<br>            domain: Dict[Text, Any]) -&gt; List[Dict[Text, Any]]:<br>        user_message = tracker.latest_message[&#39;text&#39;]<br>        entities = tracker.latest_message[&#39;entities&#39;]<br>        entity_length = sum([len(e[&#39;value&#39;].split()) for e in entities])<br>        intent = tracker.latest_message[&#39;intent&#39;].get(&#39;name&#39;)<br>        l = len(user_message.split()) - entity_length + len(entities) # count each entity as one word<br>        if l &gt; avg_lengths[intent]:<br>            is_talkative = True<br>        else:<br>            is_talkative = False<br>        return [SlotSet(&quot;is_talkative&quot;, is_talkative)]</pre><p>To choose a longer response if the user is talkative, and a default response otherwise, we use a <strong>conditional response</strong> (responses are specified in domain.yml — see below):</p><pre>utter_navigation_started:<br>  - condition:<br>    - type: slot<br>      name: is_talkative<br>      value: true<br>    text: Hey, sure. Starting navigation from {from} to {main_destination}<br>  - text: Navigating from {from} to {main_destination}</pre><p>Now you know how to write custom actions, but one thing is missing: how does the bot know which actions to perform? Read on to the next sessions to learn that!</p><h4>Training data</h4><p><strong>Data </strong>folder contains data for both <strong>NLU </strong>and <strong>core </strong>model training.</p><p><strong>NLU training data</strong></p><p>The data for training the NLU model is contained in nlu.yml. Its main part is <strong>intent names</strong> and examples of <strong>intent sentences</strong> with <strong>annotated entities</strong>:</p><pre>- intent: Navigate<br>  examples: |<br>    - navigate me to [london](to)<br>- navigate to [Barrgatan 7](to) from [Moskosel](from)<br>- navigate to [Birmingham](to) from [London](from)</pre><p>Note: entity text is enclosed in square brackets, and entity type in parentheses.</p><p>After testing, we realised that “to” and “from” entity types often get mixed up: e.g. in “Navigate from Birmingham to London”, both cities would be recognised as the entity type “to”. It makes sense: without context, entities “Birmingham” and “London” are very similar. Entity type “to” is much more frequent in navigation than entity type “from”, which would typically be assumed to be the user’s current location. Hence, “to” is predicted more frequently. Rasa has a solution for this problem: we can assign the<strong> same type</strong>, but <strong>different roles</strong>, to the origin and destination, which resolves the issue:</p><pre>- intent: Navigate<br>  examples: |<br>    - navigate me to [london]{&quot;entity&quot;: &quot;location&quot;, &quot;role&quot;: &quot;to&quot;}<br>- navigate to [Barrgatan 7]{&quot;entity&quot;: &quot;location&quot;, &quot;role&quot;: &quot;to&quot;} from [Moskosel]{&quot;entity&quot;: &quot;location&quot;, &quot;role&quot;: &quot;from&quot;}<br>- navigate to [Birmingham]{&quot;entity&quot;: &quot;location&quot;, &quot;role&quot;: &quot;to&quot;} from [London]{&quot;entity&quot;: &quot;location&quot;, &quot;role&quot;: &quot;from&quot;}</pre><p>Some more options for extracting entities include:</p><ul><li><strong>synonyms</strong>: variants of an entity that would all be mapped to a standard variant. In the navigation bot, we had an intent that would save an address to a database, and we’d like the versions like “My address is 6 London drive”, “Save 6 London drive as home”, “I live at 6 London drive”, etc. to be all saved under “home”. Hence we used a synonym mapping:</li></ul><pre>- synonym: home<br>  examples: |<br>    - I live at<br>    - my home address<br>    - my address<br>    - my home</pre><p>Note that you do not need to include the standard variant (‘home) in the synonyms list, it only needs to be mentioned at the first line: — synonym: home. Also note that all variants need to be present in the training examples for intents. Finally, make sure to include the following into your config.yml (more about the config file later):</p><pre>- name: EntitySynonymMapper</pre><ul><li><strong>regular expressions</strong>: you can define a regex that will be used for rule-based entity extraction:</li></ul><pre>- intent: call_number<br>  examples: |<br>    - call [07869613248](phone_number)<br>    - dial [7869613248](phone_number)<br>- regex: account_number<br>  examples: |<br>    - \d{10,12}</pre><ul><li><strong>Lookup tables</strong>: you can define a list of words and phrases belonging to an entity:</li></ul><pre>- lookup: city<br>  examples: |<br>    - London<br>    - Birmingham<br>    - ...</pre><p>Note that this would only work well if an entity contains a closed list of words. Otherwise you need to train an NER (Named Entity Recognition) model or use an existing one (see Configuration section).</p><p>To use regexes and lookup tables,you need to add <strong>RegexEntityExtractor </strong>and <strong>RegexFeaturizer </strong>to your config.yml.</p><p><strong>Core training data</strong></p><p>Core training data is vital for the core component of a Rasa model — the one that handles the <strong>conversation flow</strong>. A core model consists of <strong>rules </strong>and an <strong>ML model</strong>. The data is stored in rules.yml and stories.yml, correspondingly.</p><p><strong>Rules </strong>define <strong>short </strong>dialogue fragments that are <strong>fixed</strong>: typically it is one intent and a response and/or action that should always follow that intent. For example, if the user asks what the bot can do (what_scope intent), the bot always explains the scope of its capabilities (utter_scope response):</p><pre>- rule: what_scope<br>  steps:<br>  - intent: what_scope<br>  - action: utter_scope</pre><p><strong>Stories </strong>are used to train the <strong>ML </strong>part of the core model. They describe <strong>longer, more complex</strong> dialogue fragments that will help the bot to learn how to handle more varied conversations and generalise to unseen data.</p><p>We described above the action_set_user_feats action that compares the length of the user’s input to the average length of the corresponding intent and sets is_talkative slot. We defined a conditional response utter_navigation_started that would return a longer reply if is_talkative slot is set to True. To teach the bot to run this action after the user triggers the navigate_to intent and before returning the utter_navigation_started response, we write the following story:</p><pre>- story: Navigate<br>  steps:<br>    - intent: navigate_to<br>      entities:<br>      - location: &quot;221B Baker street London&quot;<br>        role: to<br>    - action: action_add_destination<br>    - slot_was_set:<br>      - main_destination: &quot;221B Baker street London&quot;<br>    - action: action_set_user_feats<br>    - slot_was_set:<br>      - is_talkative: true<br>    - action: action_start_navigation<br>    - action: utter_navigation_started<br>    - action: action_listen</pre><p>Note that, unlike rules, in stories we provide examples of entities and reflect slot filling.</p><p>If the user triggers navigate_to intent but does not provide a destination, the main_destination slot would not be filled after the first message. The bot would need to additionally ask the user to provide a destination. This scenario needs to be described by a separate story:</p><pre>- story: Open_gps<br>  steps:<br>    - intent: navigate_to<br>      entities:<br>      - location: null<br>    - action: action_add_destination<br>    - slot_was_set:<br>      - main_destination: null<br>    - action: action_set_user_feats<br>    - slot_was_set:<br>      - is_talkative: true<br>    - action: utter_ask_for_destination<br>    - action: action_listen<br>    - intent: navigate_to<br>      entities:<br>      - location: &quot;221B Baker street London&quot;<br>        role: to<br>    - action: action_add_destination<br>    - slot_was_set:<br>      - main_destination: &quot;221B Baker street London&quot;<br>    - action: action_start_navigation<br>    - action: utter_navigation_started<br>    - action: action_listen</pre><p>In this case, the value of the main_destination slot (None vs filled) impacts the conversation. When we are specifying slots in the domain file (see below), we need to set the influence_conversation flag to True for this kind of slots.</p><p>Creating stories manually can be a pain, particularly when you are just getting familiar with Rasa. An extremely useful tool for creating stories (and better understanding them) is the command line rasa interactive option: it launches an interactive interface where you can type messages as a user, check whether the intent and entities are recognised correctly, fix them if not, track the slot filling, check and correct the next action:</p><p>Be careful: rasa interactive will retrain your models every time you launch it if the data has changed! Using rasa interactive to write just one story is not time-efficient, it’s best to create several stories at a time and retrain after that. To start a new story, run /restart in rasa interactive.</p><p>As conversation flow management in Rasa is mainly ML-based, its behaviour can change every time you add new data and retrain your model. It means that something that used to work perfectly can stop working at some iteration. What does this mean? We need <strong>testing</strong>! Luckily, Rasa provides possibilities for <strong>automated testing</strong> using <strong>test stories</strong>. They are very similar to the training stories, except for the need to spell out the whole user’s input (instead of just entities):</p><pre>- story: Navigate to location<br>  steps:<br>  - user: |<br>      hello<br>    intent: hello<br>  - action: utter_hello<br>  - user: |<br>     navigate to [7 Victor Hugo drive]{&quot;entity&quot;: &quot;location&quot;, &quot;role&quot;: &quot;to&quot;} please<br>    intent: navigate_to<br>  - action: action_add_destination<br>  - slot_was_set:<br>    - main_Destination: 7 Victor Hugo drive<br>  - action: action_set_user_feats<br>  - slot_was_set:<br>    - is_talkative: false<br>  - action: action_start_navigation<br>  - action: utter_navigation_started</pre><p>To run the test, use rasa test in the command line.</p><h4>Domain</h4><p>Finally, the <strong>domain </strong>file lists all the information about training data and actions: <strong>intents</strong>, <strong>entities</strong>, <strong>slots </strong>with their types, default values and influence_conversation flag, <strong>forms</strong>, <strong>actions</strong>:</p><pre>version: &#39;2.0&#39;<br>session_config:<br>  session_expiration_time: 60<br>  carry_over_slots_to_new_session: true<br>intents:<br>- ...<br>entities:<br>- ...<br>slots:<br>  &lt;slot name&gt;:<br>    type: text<br>    influence_conversation: true<br>  &lt;slot name&gt;:<br>    type: list<br>    influence_conversation: false<br>responses:<br>  utter_&lt;response name&gt;:<br>  - text: ...<br>actions:<br>- action_&lt;action name&gt;</pre><p><strong>Forms </strong>in Rasa provide a way to collect some <strong>required information</strong> from the user, for example the destination for “navigate_to”intent”. Forms are defined in the domain file as follows:</p><pre>forms:<br>  navigate_form:<br>    required_slots:<br>        - main_destination</pre><p>You can use the forms in stories and rules by referring to the form’s name (navigate_form in our example):</p><pre>- rule: Activate form<br>  steps:<br>  - intent: navigate_to<br>  - action: navigate_form<br>  - active_loop: navigate_form</pre><p>active_loop states that the form should be activated at this point of the dialogue. The form gets deactivated automatically when all required slots are filled.</p><h4>Configuration</h4><p>Now it’s time to look at the <strong>configuration </strong>file used for training models: config.yml. It includes two parts: configuration for the <strong>NLU model </strong>and configuration for the <strong>core model</strong>. As we have already mentioned, Rasa models are highly customisable, and we can see it now: you can choose between a number of <strong>pre-processing</strong> options, <strong>featurisers</strong>, <strong>ML models</strong> for intent classification and named entity recognition (NER). <strong>Pre-trained NER models</strong> from <a href="https://spacy.io/">spacy</a> and <a href="https://github.com/facebook/duckling">Duckling</a> are available. You can use classic ML or state-of-the-art deep learning models, including transformers (Rasa’s <strong>DIETClassifier</strong>). <strong>Look-up dictionaries</strong> and <strong>regular expressions</strong> can also be used for NER. The full list of pipeline components can be found at <a href="https://rasa.com/docs/rasa/components/">https://rasa.com/docs/rasa/components/</a>.</p><p>It’s good to remember that, though transformers are incredibly powerful, training a transformer model takes a while, and you need to have a decent amount of data to get the desired results. It is worth trying out spacy and/or Duckling NER models before training your own: they might already have all you need.</p><p>In the case of our navigation bot, we needed a module for <strong>address extraction</strong>. Spacy recognises locations such as cities and countries, but not full addresses such as “221B Baker street London”, so we had to collect some examples of addresses and train our own NER model. We started with the following config.yml that showed a good performance:</p><pre># Configuration for Rasa NLU.<br># https://rasa.com/docs/rasa/nlu/components/<br>language: &quot;en&quot;<br>pipeline:<br>- name: &quot;HFTransformersNLP&quot;<br>  model_weights: &quot;distilbert-base-uncased&quot;<br>  model_name: &quot;distilbert&quot;<br>- name: &quot;LanguageModelTokenizer&quot;<br>- name: &quot;LanguageModelFeaturizer&quot;<br>- name: &quot;DIETClassifier&quot;<br>  random_seed: 42<br>  intent_classification: True<br>  entity_recognition: True<br>  use_masked_language_model: True<br>  epochs: 5<br>  number_of_transformer_layers: 4<br>  transformer_size: 256<br>  drop_rate: 0.2<br>  batch_size: 32<br>  embedding_dimension: 50<br># other components<br>- name: FallbackClassifier<br>  threshold: 0.5<br>- name: EntitySynonymMapper</pre><p>configuration for the NLU model</p><pre>Policies:<br># # See https://rasa.com/docs/rasa/policies for more information.<br>   - name: MemoizationPolicy<br>   - name: RulePolicy<br>   - name: UnexpecTEDIntentPolicy<br>     max_history: 5<br>     epochs: 100<br>   - name: TEDPolicy<br>     max_history: 5<br>     epochs: 100<br>     constrain_similarities: true</pre><p>configuration for the core model</p><p>Table. 1. Example of config.yml</p><p>Note that you can run into <strong>memory </strong>issues while training a transformer. We found that experimenting with the following parameters: number_of_transformer_layers, transformer_size, batch_size, embedding_dimension can help solve the problem.</p><p>After we started adding intents for calling/messaging, we realised we needed entities such as <strong>names </strong>and <strong>phone numbers</strong>. Names can be very diverse, and phone numbers in different countries follow different patterns (and we can never be sure of the exact formatting we get as the input), so it proved more reasonable to use spacy’s PERSON entity and a few Duckling’s entity including distance, duration, and phone-number. To incorporate these models, we changed the NLU configuration as follows (note how we add our own preprocessor):</p><pre># Configuration for Rasa NLU.<br># https://rasa.com/docs/rasa/nlu/components/<br>language: en<br>pipeline:<br>- name: ourpreprocessor.OurPreprocessor<br># language model<br>- name: WhitespaceTokenizer<br>- name: LanguageModelFeaturizer<br>  model_weights: distilbert-base-uncased<br>  model_name: distilbert<br># Regex for phone numbers<br>- name: RegexFeaturizer<br># dual intent and entity<br>- name: DIETClassifier<br>  random_seed: 42<br>  batch_size: 32<br>  intent_classification: True<br>  entity_recognition: True<br>  use_masked_language_model: False<br>  constrain_similarities: True<br>  epochs: 35<br>  evaluate_on_number_of_examples: 200<br>  evaluate_every_number_of_epochs: 1<br>  tensorboard_log_directory: &quot;./tbdiet&quot;<br>  tensorboard_log_level: &quot;epoch&quot;<br># pretrained spacy NER for PERSON<br>- name: SpacyNLP<br>  model: en_core_web_md<br>- name: SpacyEntityExtractor<br>  dimensions: [PERSON]<br>- name: DucklingEntityExtractor<br>  url: &quot;http://localhost:8000&quot;<br>  locale: &quot;en_GB&quot;<br>  timezone: &quot;UTC&quot;<br>  dimensions: [distance, duration, number, ordinal, phone-number, time, temperature]<br># other components<br>- name: FallbackClassifier<br>  threshold: 0.4<br>- name: EntitySynonymMapper</pre><p>An important part of the configuration is handling of <strong>fallbacks </strong>which can be of two types:</p><ul><li><strong>NLU fallback</strong>: the bot does not reliably recognise the user’s intent. This happens when the confidence of predicted intents is below the threshold set by:</li></ul><pre>- name: FallbackClassifier<br>  threshold: 0.4</pre><p>In this case, by default, action_default_fallback will be performed, sending utter_default to the user. But we did not want our bot to keep repeating the same default fallback response in case it does not understand the user repeatedly — that’s quite unnatural. We wanted to have several options of the reprompt that would be selected randomly every time, making sure that they do not repeat. We achieved this by writing a custom action:</p><pre>class ActionReprompt(Action):<br>    &quot;&quot;&quot;Executes the fallback action and goes back to the previous state<br>    of the dialogue&quot;&quot;&quot;<br>    def name(self) -&gt; Text:<br>        return &#39;action_reprompt&#39;<br>    async def run(<br>        self,<br>        dispatcher: CollectingDispatcher,<br>        tracker: Tracker,<br>        domain: Dict[Text, Any],<br>    ) -&gt; List[Dict[Text, Any]]:<br>        reprompts = [&quot;I&#39;m sorry, I didn&#39;t quite understand that. Could you rephrase?&quot;,<br>                     &quot;Sorry, I didn&#39;t catch that, can you rephrase?&quot;,<br>                     &quot;Apologies, I didn&#39;t understand. Could you please rephrase it?&quot;]<br>        last_reprompt = tracker.get_slot(&#39;last_reprompt&#39;)<br>        if last_reprompt in reprompts:<br>            reprompts.remove(last_reprompt)<br>        reprompt = random.choice(reprompts)        <br>        dispatcher.utter_message(text=reprompt)<br>        return [SlotSet(&quot;last_reprompt&quot;, reprompt)]</pre><p>To use this action instead of the default one, we introduce a new rule:</p><pre>- rule: Implementation of Fallback<br>  steps:<br>  - intent: nlu_fallback<br>  - action: action_reprompt</pre><ul><li><strong>Core fallback</strong>: the bot can not reliably predict the next action. action_default_fallback will be performed by default. Threshold for this fallback is set in the core model configuration:</li></ul><pre>- name: &quot;RulePolicy&quot;<br>    core_fallback_threshold: 0.3<br>    core_fallback_action_name: action_default_fallback<br>    enable_fallback_prediction: true<br>    restrict_rules: true<br>    check_for_contradictions: true</pre><h4>How to train and run Rasa models?</h4><p>You can train and run models locally via command line:</p><ul><li>rasa train to train an NLU and core model</li><li>rasa train nlu to train an NLU model</li><li>rasa train core to train a core model</li></ul><p>To launch your Rasa agent in the command line, use rasa shell. If you have custom actions, you need to launch rasa run actions in a separate terminal window first to start the Rasa action server (keep it open while running rasa shell).</p><h3>Hands-on: your first agent</h3><p>That was a long read! You see now why it’s hard to get started with Rasa?</p><p>There is a little trick that can be used as a first learning project with Rasa: use an existing chatbot from the list of Dialogflow pre-built <a href="https://dialogflow.cloud.google.com/#/agents">agents</a> (under the ‘Pre-built agents’ tab in the left sidebar menu) and convert it to Rasa. This allows you to get familiar with the development process without thinking of the conversational design part too much. You can create a free Dialogflow account, create a new agent, and import one of pre-built agents into your agent. Then, going to the Settings, you can export a ZIP archive with the agent’s data:</p><p>Unzip the downloaded archive to a directory. Create a new folder where you want to create your Rasa bot. To convert Dialogflow NLU data to Rasa NLU data, use:</p><pre>rasa data convert nlu -f yaml --data &lt;Dialogflow agent folder&gt; --out &lt;Rasa agent folder&gt;</pre><p>Now you have your NLU data! You can train an NLU model using rasa train nlu. What’s missing is rules and stories: Dialogflow has a different system of dialogue management that can’t be converted directly to Rasa format.</p><p>Don’t worry: you only need a<strong> couple of short stories</strong> to train your first core model. Choose the simplest conversations that your agent should handle and try to write a couple of stories with 1–2 conversation turns. You can check out some examples of stories for various use cases (booking an appointment, reporting that something is broken, FAQs, etc) here: <a href="https://github.com/RasaHQ/rasa-demo/tree/main/data/stories">rasa-demo/data/stories at main · RasaHQ/rasa-demo · GitHub</a>. You can train a full model now! From here, you can work iteratively: test your NLU and core components, see what intents and entities are not recognised correctly, add more examples. Write more rules and stories (remember that rasa interactive is of great help here) for the core model. Define your custom actions and add them to stories to make sure the model predicts them at the right time.</p><h3>Conclusions</h3><p>In this blog, we wanted to provide an introduction to chatbots in general and to show how to create Rasa agents, using an example of an AI driving assistant we developed. We hope that it will help you get through a few important steps of creating a Rasa chatbot: installation, creating a project, preparing data, choosing a config, training, adding custom actions. Enjoy developing your first Rasa bot!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8cacc3aade29" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[REST API’S]]></title>
            <link>https://medium.com/@bharathreddy028/rest-apis-4eb7b3b8c479?source=rss-fee736304085------2</link>
            <guid isPermaLink="false">https://medium.com/p/4eb7b3b8c479</guid>
            <dc:creator><![CDATA[bharath reddy]]></dc:creator>
            <pubDate>Wed, 30 Aug 2023 14:08:37 GMT</pubDate>
            <atom:updated>2023-08-30T14:08:37.588Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article</p><ol><li>History of Rest API</li><li>How does the Rest API work?</li><li>Principles of Rest API</li><li>Methods of Rest API</li><li>Challenges of using Rest API</li><li>Rest API Examples</li><li>Conclusion</li><li>Frequently Asked Questions (FAQs)</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/849/0*sFD_ZQ9iPT1mcXaM" /></figure><p>Sharing data between systems is more critical than ever in this age of interconnections. An eCommerce website that lacks integration in today’s market is unimaginable. In addition to managing product listings, your site would need to develop facilities for payment processing, shipping, email automation, user accounts, and other services on its own. It is not a scalable approach; instead, outsourcing these activities to other providers.<br>To communicate, software applications use Application Programming Interfaces or APIs. APIs allow two programs to share data consistently. Your eCommerce site may connect with payment software, shipping software, and other essential integrations via their APIs. There are several techniques for developing an API, but if you want to integrate software into your product, you need to know what is REST API.</p><h3>History of Rest API</h3><p>There was no agreement on how to develop or use an API before 2000. Protocols like Simple Object Access Protocol (SOAP), which were notoriously difficult to build, administer, and debug, were required for its integration.</p><p>APIs were then developed to be adaptive rather than accessible. After realizing the true potential of Web APIs in 2000, things started to shift: Roy Fielding and a team of experts created Representational State Transfer or REST, irreversibly altering the API landscape. The objective was to create a standard that would allow two servers to communicate and exchange data from anywhere in the world. As a result, they created REST, a resource-oriented architecture that includes the following concepts, attributes, and constraints:</p><ul><li>Interface uniformity</li><li>Client/server architecture</li><li>No state or session retention</li><li>Resource representation caching</li><li>Usage of the HTTP protocol and its methods</li></ul><p>Although there were numerous regulations, the majority of them were universal. Therefore, APIs had to be essential and make integration much more accessible.</p><h3>What is Rest API?</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/848/0*ds5F77QZdxDi8g6a.png" /></figure><p>It is an architectural design that outlines guidelines for developing Web Services. In a client-server connection, REST recommends creating an object of the data requested by the client and sending the object’s values back to the user.</p><p>For example, if a user requests a movie in Bangalore at a specific time and location, you may construct an object on the server side. So, you have an item transmitting its status over here. REST’s architectural style aids in utilizing reduced bandwidth to make an application more acceptable for the internet. It is sometimes referred to as the “internet language” and depends entirely on resources. Now that you know what REST API stands for, let us proceed to comprehend the significance of REST API.</p><h3>How does the Rest API work?</h3><p>REST APIs employ HTTP requests to communicate with databases to do routine tasks like creating, reading, updating, and deleting entries (also known as CRUD) within a resource. For example, a REST API may use a GET request to get a record, a POST request to create one, a PUT request to update one, and a Destroy request to remove one. One can make API queries using any HTTP method. A well-designed REST API is similar to a website that runs in a web browser and supports HTTP.</p><p>Its resource represents the resource at any particular moment or date. This data may be sent to a client in almost any format, including HTML, XLT, Python, PHP, JavaScript Object Notation (JSON), or plain text. JSON is popular because it is understandable by both people and machines and is independent of programming languages.</p><h3>Features of Rest API</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/848/0*u8qwtWL1VkSCwqK8" /></figure><p>We have understood what is Rest API, and how it works. Now, we will see the features of Rest API.</p><ul><li><strong>Scalability: </strong>Development teams may quickly scale the solution due to the separation of client and server.</li><li><strong>Flexibility &amp; Portability:</strong> Data from one of the queries must be accurate for REST-style APIs; thus, it is conceivable to transition from one server to another. It is also possible to alter the database at any moment.</li><li><strong>Independence:</strong> Through the isolation of the client and server, the protocol allows for autonomous development across a project. REST APIs are also adaptive to the working syntax and platform, enabling you to test many environments simultaneously while building.</li></ul><h3>Principles of Rest API</h3><h3>1. Client-Server decoupling</h3><p>In a REST API design, client and server programs must be independent. The client software should only know the URI of the requested resource; it should have no additional interaction with the server application.</p><h3>2. Uniform Interface</h3><p>All API queries for the same resource should look the same regardless of where they come from. The REST API should ensure that similar data, such as a user’s name or email address, is assigned to just one uniform resource identifier (URI).</p><h3>3. Statelessness</h3><p>REST APIs are stateless, meaning each request must contain all the information needed to process it.</p><h3>4. Layered System architecture</h3><p>REST API requests and responses are routed through many tiers. REST APIs must be designed so neither the client nor the server can tell whether they communicate with the final application or an intermediary.</p><h3>5. Cacheable</h3><p>Wherever feasible, resources should be cacheable on the client or server side. Server responses must additionally indicate if caching is authorized for the offered assistance. The objective is to boost client-side speed while enhancing server-side scalability.</p><h3>6. Code on Demand</h3><p>REST APIs typically provide static resources, but in rare cases, responses may include executable code (such as Java applets). In these cases, perform the code when necessary.</p><h3>Methods of Rest API</h3><p>REST determines the structure of an API. Therefore, developers must follow a set of guidelines when building an API. One piece of regulation, for example, requires that pointing to a URL contains specific information. Every URL is known as a Request, and the data returned is known as a Response. The REST API breaks down a transaction into its constituent parts. Each component is responsible for a particular side of marketing. It is a flexible development technique due to its modularity. REST API uses HTTP methods defined in the RFC 2616 standard. It employs the HTTP requests listed below:</p><h3>1. GET</h3><p>The most popular HTTP method, GET, returns a representative view of the information and data contained in a resource. For data security and resource idempotence, the Use of GET should only be in read-only mode. If another client hasn’t changed this technique in the interim, you should always obtain the same results regardless of how many times you use it.</p><h3>2. POST</h3><p>POST is the sole HTTP method for RESTful APIs that focuses on resource collections. Applying POST to the parent resource causes it to build a new resource, add it to the correct hierarchy, and return a specific URL for future use for establishing a subordinate resource in a collection. However, remember that POST is not idempotent; you cannot repeatedly employ this technique and anticipate a consistent result.</p><h3>3. PUT</h3><p>The PUT command modifies a resource by completely replacing its content, making it the single-resource counterpart of POST. As a result, PUT is the most popular technique for updating resource data in a RESTful API.</p><h3>4. PATCH</h3><p>Another HTTP method for updating resources is PATCH. PATCH alters resource contents instead of replacing resources, like the PUT method. These updates should often be conveyed using a common format, such as JSON or XML.</p><h3>5. DELETE</h3><p>DELETE is the final HTTP method to look at. Targeting a single resource with a DELETE method results in the complete deletion of that resource.</p><p>Implementations of DELETE are frequently a little inconsistent: If you remove the URL for the resource, it can still be accessible. In this case, it’s probable that the server or resource implementation will still alter the status of the deleted resource using the URL and will probably respond differently to subsequent DELETE requests.</p><h4>FAQ’s<br>1. What is the use of REST API?</h4><p>A RESTful API is an architectural approach for an application program interface (API) that accesses and utilizes data via HTTP requests. It may use that data to GET, PUT, POST, and DELETE data, which correspond to reading, modifying, creating, and removing resource-related activities.</p><p>2. What is the difference between REST API and API?</p><p>REST (Representational State Transfer) is the API that uses a set of rules to communicate between applications and servers.</p><p>3. What is the URL in REST API?</p><p>An API URL Path is a URL that allows you to access an API’s numerous functionalities. Using one is as easy as entering any URL into your web browser.</p><p>4. How to design a REST API?</p><p>The most common methods to design are GET, POST, PUT, and DELETE.</p><p>5. What is the difference between SOAP and REST API?</p><p>SOAP solely utilizes XML for sharing information in its message format, but REST is not limited to XML and allows the implementer to choose which Media-Type to use, such as XML, JSON, or plain text. Furthermore, whereas REST may utilize the SOAP protocol, SOAP cannot use REST.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4eb7b3b8c479" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What Is RASA?]]></title>
            <link>https://medium.com/@bharathreddy028/what-is-rasa-c6f2790a38b7?source=rss-fee736304085------2</link>
            <guid isPermaLink="false">https://medium.com/p/c6f2790a38b7</guid>
            <dc:creator><![CDATA[bharath reddy]]></dc:creator>
            <pubDate>Wed, 30 Aug 2023 13:57:34 GMT</pubDate>
            <atom:updated>2023-08-30T13:57:34.882Z</atom:updated>
            <content:encoded><![CDATA[<p>Rasa is an open-source framework for building conversational AI chatbots and assistants. It provides tools and libraries for developing and deploying AI-driven, text-based chatbots that can handle natural language conversations with users. Rasa enables developers to create interactive and dynamic chatbot experiences that can understand user intents, extract entities, and provide meaningful responses.</p><p>Rasa consists of two main components:</p><p>Rasa NLU: This component focuses on understanding and extracting the meaning of user messages. It performs tasks like intent recognition (identifying the user’s intent) and entity recognition (extracting relevant information from the user’s input).</p><p>Rasa Core: This component handles the dialogue management of the conversation. It decides how the chatbot should respond based on the user’s input, the context of the conversation, and predefined actions.</p><p>Rasa is typically used for building AI chatbots in a developer-friendly way, allowing for more customization and control compared to using pre-built platforms.</p><p>Here are the basic installation steps for Rasa:</p><ol><li>Install Python and pip: Make sure you have Python (version 3.6 or later) and pip (Python package manager) installed on your system.</li><li>Create a Virtual Environment (Recommended): It’s a good practice to create a virtual environment to isolate your project’s dependencies. Navigate to your project directory and create a virtual environment using the following command:</li></ol><pre>python3 -m venv venv_name</pre><p>3. Activate the Virtual Environment: Activate the virtual environment using the appropriate command for your operating system:</p><ul><li>On macOS and Linux:</li></ul><pre>python3 -m venv rasa-env <br>source rasa-env/bin/activate</pre><p>4. Install Rasa: With your virtual environment activated, install Rasa using pip:</p><pre>pip install rasa</pre><p>5. Initialize a New Rasa Project: Navigate to the directory where you want to create your Rasa project and run:</p><pre>rasa init</pre><p>This command will set up a basic Rasa project structure with example files and training data.</p><p>6. Train the Model: Train your Rasa model by running:</p><pre>rasa train</pre><p>This command will train both the NLU and Core components using the provided training data.</p><p>7. Interact with Your Chatbot:</p><p>After training, you can interact with your chatbot using the command line interface (CLI). Run:</p><pre>rasa shell</pre><p>This will initiate a conversation with your chatbot in the terminal.</p><p>These are the fundamental steps to get started with Rasa. The framework offers extensive capabilities for customizing your chatbot’s behavior, connecting it to various channels, and deploying it to different platforms.</p><p>For more advanced features and detailed instructions, refer to the official Rasa documentation: <a href="https://rasa.com/docs/">https://rasa.com/docs/</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c6f2790a38b7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>