How to get dataframe as output in actions.py

I wanted to print dataframe of products in the actions.py with the help of a dispatcher.utter_message or any other method in bot. I am getting an error is mentioned below:

Blockquote ERROR rasa.core.processor - Encountered an exception while running action ‘action_get_accessories’.Bot will continue, but the actions events are lost. Please check the logs of your action server for more information. Traceback (most recent call last): File “e:\anaconda\envs\chatbot\lib\site-packages\rasa\core\actions\action.py”, line 686, in run json=json_body, method=“post”, timeout=DEFAULT_REQUEST_TIMEOUT File “e:\anaconda\envs\chatbot\lib\site-packages\rasa\utils\endpoints.py”, line 173, in request response.status, response.reason, await response.content.read() rasa.utils.endpoints.ClientResponseError: 500, Internal Server Error, body=‘b’\xe2\x9a\xa0\xef \xb8\x8f 500 \xe2\x80\x94 Internal Server Error\n\n html { font-family: sans-serif }\n h2 { color: #888; }\n .tb-w rapper p { margin: 0 }\n .frame-border { margin: 1rem }\n .frame-line > * { padding: 0.3rem 0.6rem }\n .frame-line { margin-botto m: 0.3rem }\n .frame-code { font-size: 16px; padding-left: 4ch }\n .tb-wrapper { border: 1px solid #eee }\n .tb-header { backgrou nd: #eee; padding: 0.3rem; font-weight: bold }\n .frame-descriptor { background: #e2eafb; font-size: 14px }\n \n

\xe2\x9a\xa0\xef \xb8\x8f 500 \xe2\x80\x94 Internal Server Error

The server encountered an internal error and cannot complete your request.\n’’

The above exception was the direct cause of the following exception:

Traceback (most recent call last): File “e:\anaconda\envs\chatbot\lib\site-packages\rasa\core\processor.py”, line 773, in _run_action output_channel, nlg, temporary_tracker, self.domain File “e:\anaconda\envs\chatbot\lib\site-packages\rasa\core\actions\action.py”, line 709, in run raise RasaException(“Failed to execute custom action.”) from e rasa.shared.exceptions.RasaException: Failed to execute custom action.

Also in the actions.py file I am getting this issue.

Blockquote Exception occurred while handling uri: ‘http://localhost:5055/webhook’ Traceback (most recent call last): File “e:\anaconda\envs\chatbot\lib\site-packages\sanic\app.py”, line 931, in handle_request response = await response File “e:\anaconda\envs\chatbot\lib\site-packages\rasa_sdk\endpoint.py”, line 114, in webhook return response.json(result, status=200) File “e:\anaconda\envs\chatbot\lib\site-packages\sanic\response.py”, line 205, in json dumps(body, **kwargs),

is not JSON serializable

Welcome to the forum!

I assume it’s a Pandas dataframe, which is not a string and you should not use it as one.

dispatcher.utter_message() takes in a string, therefore you need to convert the dataframe to a string.

As per the Pandas documentation, use dataframe.to_string().

@gaurav21s Hello, can you please share the rasa --version, are you fetching the data from .csv or .xls file? can you share the action.py code for our reference?

I have already tried that but the output is in not a readable format. I want it in a table-like format or in a better format.

Hey @nik202 my rasa version is 2.8.1 and I am fetching the data from .csv. I can’t share the whole code but I am sharing you the a part of it. ans = df[(df[‘gender_m’].str.lower() == Gender.lower()) & ( df[‘product_specific_type’].str.contains(Product.lower(), case=False)) & (df[‘color_m’].str.lower() == Colour.lower())]

    message = "Here are the products based on your choice, " + "\n" + "Name:" + json.dumps(
        ans['NAME'].values.tolist()) \
              + "\n" + "Product URL:" + json.dumps(ans['PRODUCT URL'].values.tolist()) \
              + "\n" + "Product Picture:" + json.dumps(ans['prod_pic'].values.tolist())
    # data = ans[['NAME', 'PRODUCT URL', 'prod_pic']]
    if ans["NAME"].empty:
        message = "The product is currently not available. You can enter your email id and we will notify you once " \
                  "the product is back in stock."

    print(ans.head())

    dispatcher.utter_message(message)
    return []

#This output is not very readable. Here is the data which I want as output.

@gaurav21s as expected, I just want to see the function of how are you fetch the .csv file using custom actions, can you share that and please do format the code for better understanding :slight_smile:

This is an issue with Pandas and/or general Python, not Rasa :slight_smile:

As you can see in their docs, the function has a lot of customization options.

You have a string and you want to properly format it, this is not Rasa-related and you would have better chances of finding someone who knows in places like Stack Overflow :slight_smile:

But, you can still show us your function and we’ll try to help you with Python.

image

you can use iloc or loc inside the .py file and a for loop to output the utter_message

class ActionGetAccessories(Action): def name(self) → Text: return “action_get_accessories”

def run(
        self,
        dispatcher,
        tracker: Tracker,
        domain: "DomainDict",
) -> List[Dict[Text, Any]]:
    Product = tracker.get_slot("product")
    Gender = tracker.get_slot("gender")
    Colour = tracker.get_slot("colour")
    df = pd.read_csv("sample_product_db_v2.csv")
    ans = df[(df['gender_m'].str.lower() == Gender.lower()) & (
            df['product_specific_type'].str.contains(Product.lower(), case=False)) & (df['color_m'].str.lower() == Colour.lower())]

    message = "Here are the products based on your choice, " + "\n" + "Name:" + json.dumps(
        ans['NAME'].values.tolist()) \
              + "\n" + "Product URL:" + json.dumps(ans['PRODUCT URL'].values.tolist()) \
              + "\n" + "Product Picture:" + json.dumps(ans['prod_pic'].values.tolist())
    # data = ans[['NAME', 'PRODUCT URL', 'prod_pic']]
    if ans["NAME"].empty:
        message = "The product is currently not available. You can enter your email id and we will notify you once " \
                  "the product is back in stock."

    print(ans.head())

    dispatcher.utter_message(message)

Is there any other way to show data in the bot? Also, how can I show the previous orders in a button format so when the user asks for return products or change the address for the last order, he can select by button for which order he wants to see the details?

return dados_tabela will return the value in actions.py or in the bot?

It will return in the actions, there you fill the slots as needed or send the msg to the user with this data

@gaurav21s hi RodrigoLima did you find the solution for this , I am also facing the same issue , not able to display table in bot ,but it is coming in command prompt

1 Like

Sorry, i didnt see your question

Yes Do you need help?

Hi I’m looking to diaplay a table format for the dataframe in rasa x,have you got experience with this? thanks

Hi! I use two python libraries to do this. What exactly do you need?

I used tabulate but the output text looks congested on the rasa x UI, it there an y better library which can be used to display the table in a neat format ?

I can use a dataframe_image