<commit_before>:desc: Extend your Rasa conversational AI assistant using Rasa-SDK to connect to
       external APIs or improve dialogue with custom actions written in Python.

.. _rasa-sdk:

Rasa SDK
========

.. edit-link::

Rasa SDK provides the tools you need to write custom actions in python.

.. contents::
   :local:

Installation
------------

Use ``pip`` to install ``rasa-sdk`` on your action server.

.. code-block:: bash

    pip install rasa-sdk

.. note::

    You do not need to install ``rasa`` for your action server.
    E.g. if you are running Rasa in a docker container, it is recommended to
    create a separate container for your action server. In this
    separate container, you only need to install ``rasa-sdk``.

Running the Action Server
-------------------------

If you have ``rasa`` installed, run this command to start your action server:

.. code-block:: bash

    rasa run actions

Otherwise, if you do not have ``rasa`` installed, run this command:

.. code-block:: bash

    python -m rasa_sdk --actions actions

You can verify that the action server is up and running with the command:

.. code-block:: bash

    curl http://localhost:5055/health

You can get the list of registered custom actions with the command:

.. code-block:: bash

    curl http://localhost:5055/actions


The file that contains your custom actions should be called ``actions.py``.
Alternatively, you can use a package directory called ``actions`` or else
manually specify an actions module or package with the ``--actions`` flag.

The full list of options for running the action server with either command is:

.. program-output:: rasa run actions --help

Actions
-------

The ``Action`` class is the base class for any custom action. It has two methods
that both need to be overwritten, ``name()`` and ``run()``.

.. _custom_action_example:

In a restaurant bot, if the user says "show me a Mexican restaurant",
your bot could execute the action ``ActionCheckRestaurants``,
which might look like this:

.. testcode::

   from rasa_sdk import Action
   from rasa_sdk.events import SlotSet

   class ActionCheckRestaurants(Action):
      def name(self) -> Text:
         return "action_check_restaurants"

      def run(self,
              dispatcher: CollectingDispatcher,
              tracker: Tracker,
              domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

         cuisine = tracker.get_slot('cuisine')
         q = "select * from restaurants where cuisine='{0}' limit 1".format(cuisine)
         result = db.query(q)

         return [SlotSet("matches", result if result is not None else [])]


You should add the the action name ``action_check_restaurants`` to
the actions in your domain file. The action's ``run()`` method receives
three arguments. You can access the values of slots and the latest message
sent by the user using the ``tracker`` object, and you can send messages
back to the user with the ``dispatcher`` object, by calling
``dispatcher.utter_message``.

Details of the ``run()`` method:

.. automethod:: rasa_sdk.Action.run

Details of the ``dispatcher.utter_message()`` method:

.. automethod:: rasa_sdk.executor.CollectingDispatcher.utter_message


.. _custom_session_start:
<commit_msg>fixing typo<commit_after>:desc: Extend your Rasa conversational AI assistant using Rasa-SDK to connect to
       external APIs or improve dialogue with custom actions written in Python.

.. _rasa-sdk:

Rasa SDK
========

.. edit-link::

Rasa SDK provides the tools you need to write custom actions in python.

.. contents::
   :local:

Installation
------------

Use ``pip`` to install ``rasa-sdk`` on your action server.

.. code-block:: bash

    pip install rasa-sdk

.. note::

    You do not need to install ``rasa`` for your action server.
    E.g. if you are running Rasa in a docker container, it is recommended to
    create a separate container for your action server. In this
    separate container, you only need to install ``rasa-sdk``.

Running the Action Server
-------------------------

If you have ``rasa`` installed, run this command to start your action server:

.. code-block:: bash

    rasa run actions

Otherwise, if you do not have ``rasa`` installed, run this command:

.. code-block:: bash

    python -m rasa_sdk --actions actions

You can verify that the action server is up and running with the command:

.. code-block:: bash

    curl http://localhost:5055/health

You can get the list of registered custom actions with the command:

.. code-block:: bash

    curl http://localhost:5055/actions


The file that contains your custom actions should be called ``actions.py``.
Alternatively, you can use a package directory called ``actions`` or else
manually specify an actions module or package with the ``--actions`` flag.

The full list of options for running the action server with either command is:

.. program-output:: rasa run actions --help

Actions
-------

The ``Action`` class is the base class for any custom action. It has two methods
that both need to be overwritten, ``name()`` and ``run()``.

.. _custom_action_example:

In a restaurant bot, if the user says "show me a Mexican restaurant",
your bot could execute the action ``ActionCheckRestaurants``,
which might look like this:

.. testcode::

   from rasa_sdk import Action
   from rasa_sdk.events import SlotSet

   class ActionCheckRestaurants(Action):
      def name(self) -> Text:
         return "action_check_restaurants"

      def run(self,
              dispatcher: CollectingDispatcher,
              tracker: Tracker,
              domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:

         cuisine = tracker.get_slot('cuisine')
         q = "select * from restaurants where cuisine='{0}' limit 1".format(cuisine)
         result = db.query(q)

         return [SlotSet("matches", result if result is not None else [])]


You should add the action name ``action_check_restaurants`` to
the actions in your domain file. The action's ``run()`` method receives
three arguments. You can access the values of slots and the latest message
sent by the user using the ``tracker`` object, and you can send messages
back to the user with the ``dispatcher`` object, by calling
``dispatcher.utter_message``.

Details of the ``run()`` method:

.. automethod:: rasa_sdk.Action.run

Details of the ``dispatcher.utter_message()`` method:

.. automethod:: rasa_sdk.executor.CollectingDispatcher.utter_message


.. _custom_session_start:

{'fraction_non_alphanumeric': 0.10575139146567718, 'fraction_numerical': 0.0030921459492888066, 'mean_word_length': 3.298338870431894, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6096923', 'n_tokens_mistral': 1817, 'n_tokens_neox': 1742, 'n_words': 796}