Sandeep Bhutani
Sandeep Bhutani

Reputation: 629

rasa syntax to predict sentence by sentence

I want to use rasa like the normal model, where I can load the model, and predict the intents of each sentence individually, rather than calling some rasa test or rasa test prebuilt apis.

Can someone please let me know how to do like this:

rasa_model.load("x.tgz")
rasa_model.predict("Predict for this sentence!!")

Upvotes: 0

Views: 215

Answers (1)

Shaida Muhammad
Shaida Muhammad

Reputation: 1650

The optimal solution is to do with an NLU server.

1). Run rasa from your terminal/command prompt

rasa run --enable-api -m models/x.tar.gz

2). Send a request to your local server from another terminal/command prompt

curl localhost:5005/model/parse -d '{"text":"Predict for this sentence!!"}'

Of course, you can do step 2 in python script.

import requests


url = 'http://127.0.0.1:5005/model/parse'
myobj = {'text': 'Predict for this sentence!!'}

x = requests.post(url, json = myobj)

print(x.json())

You can extract just the intent by changing the last sentence in the python script.

print(x.json()['intent']['name'])

Another way of getting Rasa NLU outputs only with Rasa core agent:

import asyncio
from rasa.core.agent import Agent

class Model:

    def __init__(self, model_path: str) -> None:
        self.agent = Agent.load(model_path)
        print("NLU model loaded")


    def message(self, message: str) -> str:
        message = message.strip()
        return asyncio.run(self.agent.parse_message(message))

mdl = Model("models/x.tar.gz")
sentence = "Predict for this sentence!!"
print(mdl.message(sentence))

Again change the last line if you're only interested in the name of intent only.

print(mdl.message(sentence(['intent']['name'])))

The above code worked fine with Rasa 3.3.0.

Upvotes: 1

Related Questions