Communicating with Rasa in a local Python file

Dear all,

Is it possible to communicate with Rasa via a simple Python file?

My situation is the following: I have created a local python project and initated a rasa project in it and configured everything accordingly. I now want to take text inputs from my python application and send these to the Rasa chatbot and receive the responses in return, while following the stories I defined. Is there any easy way to achieve this?

I have my localhost action server running with various custom actions, which are implemented in my stories. But can’t seem to find the linkage how to connect all of this with my own python file.

Could someone please provide a simple example?

Thanks in advance and best regards,

Timothy

Hi Timothy,

I was interested in setting this up myself just as a way to test external clients connecting with Rasa. I wrote a very basic python script that uses the requests library. I set up my project to just use a rest endpoint. Basically you just need to create a credentials.py file and put rest: as the first line in the file.

Then you can start up Rasa with: rasa run -m models --endpoints endpoints.yml --port 5002 --credentials credentials.yml

This will start up Rasa up on port 5002 as a rest endpoint, and it will be connected to your actions server which you should have configured in endpoints.yml.

Afterwards just start up your actions server, and then you run your python script that connects to the Rasa rest endpoint.

Here’s my “test client” script:

# Test client for interacting with Rasa bot

import requests

sender = input("What is your name?\n")

bot_message = ""
while bot_message != "Bye":
	message = input("What's your message?\n")

	print("Sending message now...")

	r = requests.post('http://localhost:5002/webhooks/rest/webhook', json={"sender": sender, "message": message})

	print("Bot says, ")
	for i in r.json():
		bot_message = i['text']
		print(f"{i['text']}")

It’s not perfect, but I just use it for my own tests. Hope this helps!

1 Like

Thank you very much for your effort!

I seemed to be able to run the server with your command, but in the test script I was not able to get Rasa to execute my custom actions. I do not see any requests being made to the action server. So basically, as soon as my stories reach the part where they need to execute a custom action, your test script does not return anything.

Do you mind sharing your endpoints.yml and credentials.yml?

Thanks in advance

Sorry for the late reply!
Here’s my endpoints.yml:

action_endpoint:
  url: 'http://localhost:5055/webhook'

Here’s my credentials.yml:

rest:

Are you seeing errors in Rasa when attempting to execute an action? It’s possible that your endpoints isn’t configured to the correct port for your actions server. If there are errors in Rasa, no message will get sent back to the test script that I provided.

Another possibility could be that there are some errors within your actions that is also causing it to fail. I believe if there are errors within your action, it will not return back to Rasa. I would check the debug messages for both to see why you’re not getting a response back.

Hi! Using this method can I save the slots extracted with Rasa in the Python file? How can I do it? Thanks