How to use models outside of Rasa Shell?

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.

@abb Hi, Share rasa documentation link

Here is the link: rasa.core.agent

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!

That code yields the same error as before

@abb share code

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.

@abb try provide absolute path

I tried providing the absolute path, /home/rasa_testing/rasat/models/20210804-120105.tar.gz

but had the same syntax error.

@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

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!

@abb you are welcome :slight_smile: Happy to help you!