Using Rasa in a local Python file

Rasa: Hi everyone,

First of all, I’m new to Rasa.

I want to build a chatbot that takes inputs such as a String and replies to it, all directly in a local python file. With the command ,rasa init’’ I was able to get all the files with the sample intents/stories etc. But how do I send a message to Rasa and receive a response?

In the documentation I found the following (Agent)

  • from rasa.core.agent import Agent
  • from rasa.core.interpreter import RasaNLUInterpreter
  • agent = Agent.load(“examples/restaurantbot/models/current”)
  • await agent.handle_text(“hello”)

I pointed the Agent.load to ("models), as that’s where my model.tar.gz has been saved after running the ,rasa train’’ command. However, when I run this in my local python file, it returns ,None’’. If I run ,rasa shell’’ I communicate via the command line, I get the expected response (“Hey! How are you?”)

Could someone please advise what is missing here? I noticed that the second import rasa.core.interpreter is unused, so I already suspect that might be related to my problem.

Some ,starter help’’ would be much appreciated. :slight_smile:

Thanks in advance & with best regards,

Timothy

Hm, does it work if you put the path to the actual tar file in there, like

agent = Agent.load(“examples/restaurantbot/models/current/model.tar.gz”)

Dear Ella,

Thank you for your message. Fortunately I found the error, I just had to unpack the .tar.gz file in the “models” directory and it worked. I was able to get this output:

The output seems to be returned as a list. I was not able to just extract ,Hey! How are you?’’, as that would be the only part I need and the list seems to only contain one entry. Do you have any suggestions of how this could be done simply? Otherwise I’ll just convert to a String and cut the part myself.

Many thanks & Best regards, Tim

1 Like

I would say e.g. if you get the output as a list and call it output, you can get the message and store it by calling

message = output[0]["text"]

this will index the first item of the list, which is a dictionary, and pull the value of the key “text” from the dict.

That worked. Thank you so much for help and I apologize for the newbie questions. :slight_smile:

Best regards, Tim

No problem, happy to help! :smiley:

If you get the chance, we’d love your feedback on your experience with Rasa X – feedback from new users is especially valuable as it’s a different group from people who already know how to use rasa and are just upgrading. Here’s the link: Product Feedback - Rasa X