abb
(Alex Bush)
August 5, 2021, 4:53pm
1
I originally saw a previous forum post about this same topic posted here: Is there any way to use trained rasa model outside rasa shell - #2 by vp109 and decided to try it out for my self. I made a little python script and copied the code posted by user @vp109 , but was then met with the following:
await agent.handle_message("hello")
^
SyntaxError: invalid syntax
Does anyone know what is causing this issue? I’ve tried checking the rasa documentation but haven’t figured out much.
nik202
(NiK202)
August 5, 2021, 6:52pm
2
@abb Hi, Share rasa documentation link
abb
(Alex Bush)
August 5, 2021, 6:54pm
3
Here is the link: rasa.core.agent
nik202
(NiK202)
August 5, 2021, 6:57pm
4
Try, Just for demo purpose:
from rasa.core.agent import Agent
from rasa.core.interpreter import RasaNLUInterpreter
agent = Agent.load("examples/restaurantbot/models/current")
await agent.handle_text("hello")
Hope this solved your query!
abb
(Alex Bush)
August 5, 2021, 7:01pm
5
That code yields the same error as before
abb
(Alex Bush)
August 5, 2021, 7:04pm
7
Sure, here it is:
from rasa.core.agent import Agent
from rasa.core.interpreter import RasaNLUInterpreter
agent = Agent.load("./models/20210804-120105.tar.gz")
await agent.handle_text("hello")
where the tar.gz is my model.
nik202
(NiK202)
August 5, 2021, 7:08pm
8
@abb try provide absolute path
abb
(Alex Bush)
August 5, 2021, 7:11pm
9
I tried providing the absolute path,
/home/rasa_testing/rasat/models/20210804-120105.tar.gz
but had the same syntax error.
nik202
(NiK202)
August 5, 2021, 7:30pm
10
@abb unzip the model as its tar.gz ?
@abb see from stackoverflow hope it will help
In that case you don’t need to pass an interpreter. If you used rasa train
without the nlu
or core
mode then the full stacked model will be saved as a .tar.gz
in the models
directory. Load that with agent = Agent.load('path/to/model.tar.gz')
and then you can handle text with asyncio.run(agent.handle_text("hello"))
(or await agent.handle_text("hello")
if it’s inside a function)
@abb check this link also https://codechina.csdn.net/ras/rasa/-/blob/266b3d30655248a2a9bdb95268caaab80cd5e564/tests/core/test_agent.py
abb
(Alex Bush)
August 5, 2021, 7:57pm
11
It worked! I had to change it a little since asyncio.run isn’t available on python 3.6, instead I wrote this:
loop = asyncio.get_event_loop()
result = loop.run_until_complete(agent.handle_text(“hello”))
print(result)
to which the output was
[{‘recipient_id’: ‘default’, ‘text’: ‘Hey! How are you?’}]
obviously I’ll have to parse this for just the text output, but this is exactly what I was looking for! Thanks so much!
nik202
(NiK202)
August 5, 2021, 7:59pm
12
@abb you are welcome Happy to help you!