How to run Custom action in jupyter notebook

hi , here is murugan , i using jupyter notebook . i was working chatbot in RASA stack . my bot working fine … but i want to move forward to get custom actions response from bot side. how to hit an REST API using custom action , how configuration may define …any one ? thanks in advance

You can call a REST API from a custom action just like you would call it from any Python code in general.

@msamogh thanks for your reply , could u pls share some sample python code . and how python code would be cal from rasa core …thanks in advance .

You can find an example here. Specifically the part with Custom Actions Written in Python

If you are looking for code (inside the custom action) to hit an API, below snippet might be of help:

    def hit_custom_action_api(id, your_query_string, url_and_port):
    '''
      This is just an example. Your endpoint will determine the structure of the request.
      Here I assumed some parameters for content, json format of data jsut to give an idea

      eg. url_and_port= 'http://localhost:3000/api/name_of_endpoint_method' 
    '''
         content={}
         content['body'] = your_query_string
         data = {"id": id, "content": content}
         response = requests.post(url_and_port, json=data)
         repsonse_content = json.loads(response.content)

         return repsonse_content

custom_action = “”"

from rasa_core_sdk import Action from rasa_core_sdk.events import SlotSet

import requests

class ApiAction(Action): def name(self): return “action_paper_search”

def run(self, dispatcher, tracker, domain):

    paper_type = tracker.get_slot('paper_type')
    
    response = requests.get('http://dblp.org/search/publ/api?q={}&format=json&h=1'.format(paper_type)).json()
    title = response['result']['hits']['hit'][0]['info']['title']
    authors = response['result']['hits']['hit'][0]['info']['authors']['author'][0]
    link = response['result']['hits']['hit'][0]['info']['url']

    dispatcher.utter_message("I found a paper called {}".format(title))
    return [SlotSet("link",link), SlotSet("authors",authors)]

“”" %store custom_action > actions.py


from rasa_core.agent import Agent from rasa_core.utils import EndpointConfig import time

messages = [“Hi! you can chat in this window. Type ‘stop’ to end the conversation.”] interpreter = NaturalLanguageInterpreter.create(‘models/nlu/default/current’) endpoint = EndpointConfig(‘http://127.0.0.1:5000/home’) agent = Agent.load(‘models/dialogue’, interpreter=interpreter, action_endpoint = endpoint)

print("********** Bot is ready to talk! Type your messages here or send ‘stop’ ******* ") while True: a = input("user : ") if a == ‘stop’: break responses = agent.handle_text(a) for response in responses: print(“Bot :”,response[“text”]) print()

Above is my code …while i am run the code , getting respose is good , but in custom action it wornt works its shows error : , i am using jupyter notebook …and i dont know what is endpoint (“endpoint = EndpointConfig(‘http://127.0.0.1:5000/home’)”) which address i want to use it , basically i am from electronics background ,…help me , thanks in advance

Looks to me some sort of port issue… there is RASA server, there is Action server and jupyter notebook also runs its own server. Make sure none of that(or anything else you are running simultaneously) has same port.

no @samrudh my notebook is running http://localhost:8888/notebooks .and i did not run any server …but error showing same , i attached below

chatbot_customLivewire_testing.ipynb (155.8 KB)

Hi this is my jupyter notebook file , full code , pls check and verify why my custom action are not working …thanks in advance

1 Like

How does your policy.yml look like?

Also I would suggest to add some time delay after you load the agent. (i.e before the while True block of handling chat)It might take few seconds to actually start the agent server.

policy.yml (249 Bytes)

this is my policy file

Actually my problem is custom action is not triggering … when i am gives a input as " looking for paper about Physics" bot could not be trigger the custom file …its shows error

Okay. I was able to run the action server with your code. I believe default port for action server is 5055. It gave output as below:

python -m rasa_sdk.endpoint --actions actions`                                                                                   ✔  1459  14:18:21 

2019-09-26 14:18:37 INFO rasa_sdk.endpoint - Starting action endpoint server… /Users/samrudha/anaconda3/envs/help_rasa/lib/python3.7/site-packages/rasa_core_sdk/init.py:12: UserWarning: The ‘rasa_core_sdk’ package has been renamed. You should change your imports to use ‘rasa_sdk’ instead. UserWarning, 2019-09-26 14:18:37 INFO rasa_sdk.executor - Registered function for ‘action_paper_search’. 2019-09-26 14:18:37 INFO rasa_sdk.endpoint - Action endpoint is up and running. on (‘0.0.0.0’, 5055)

yes , i run abouve command in my terminal , but its shows error …help me

Thanks for your help @samrudh , working fine …mistake is running this command in "python -m rasa_sdk.endpoint --actions actions" in different location , that is the reason …thank you …

1 Like

Good @murugan… If possible, for helping someone reading this thread in the later point of time, it would be great if you can add the final working Notebook for reference.

@samrudh sure , it will be available in our conversation…