How to link NLU and domain files which is stored in DB with rasa agent

Hi All,

I am new to rasa development. I want to load my nlu.yml, story.yml and domain.yml from DB to train the rasa bot. Can anybody please help me with this?

Thanks in Advance.

There’s no built-in way for this in Rasa. What you can do is write a script that downloads the database, convert the files to YAML, and train the bot.

Hi @ChrisRahme thank you for your reply. Can you please tell me in which file I have to write that script in rasa agent?. And it’s more helpful to me if you share some sample script code.

Thanks in Advance

This would be a script unrelated to Rasa, you can put it anywhere. I don’t have such a script since I don’t pull my data from a database, but since it’s unrelated to Rasa, you can do it any way you want in any language you want.

Of course, the output directory should have a particular format:

.
├── actions
│   ├── __init__.py
│   └── actions.py
├── config.yml
├── credentials.yml
├── data
│   ├── nlu.yml
│   ├── rules.yml
│   └── stories.yml
├── domain.yml
├── endpoints.yml
├── models
│   └── <timestamp>.tar.gz
└── tests
   └── test_stories.yml

as well as the training data format, which you can see here.

Just write any code that pulls from a database with the format you want, as long as you transform the data into the correct YAML format. To be sure the format is correct, you can use the command rasa data validate.

Hi @ChrisRahme thank you for your quick reply. I will try to implement how you have explained. I have checked Custom Importer(TrainingDataImporter) in rasa documentation. Can I use the below code to load nlu, domain and config files from different location by specifying the file path? def __init__( self, config_file: Optional[Text] = None, domain_path: Optional[Text] = None, training_data_paths: Optional[Union[List[Text], Text]] = None, **kwargs: Dict ): And can you please tell me in which file, I need to write this TrainingDataImporter code?

Thanks in Advance.

Ohhh I completely forgot about that! Sorry, but I have no information other than what’s in here.

Hi @ChrisRahme, Thanks for your reply. I have checked [ TrainingDataImporter] (Training Data Importers). But I don’t know in which file I have to write that code and how to run that in my rasa agent. if you guide me on this then it’s more helpful to me.

Thanks in Advance.

I think you can just put your file at the root of the chatbot’s directory, and then, in your config.yml, do this:

importers:
- name: "CustomImporter" <-- Name of your class
- name: "RasaFileImporter"

You can also read the blog post here, it will help!

Hi @ChrisRahme, Thanks a lot for your reply. I have tried. But I am getting errors while running rasa train.

Error:- InvalidDomain: No domain file was specified. Please specify a path to a valid domain file.

And I have written my code like below,

  def __init__(
    self,
    config_file: Optional[Text] = 'D:/rasaagent/config.yml',
    domain_path: Optional[Text] = **'D:/rasaagent/domain.yml',**
    training_data_paths: Optional[Union[List[Text], Text]] = 'D:/rasaagent/data/nlu.yml',
    **kwargs: Dict
):

Can you please tell me what I am doing wrong while specifying the file path in my training data importer code?

Thanks in Advance.

Try to set the paths as config.yml and domain.yml only, without D:/rasaagent/.

Maybe you should even leave them as None, since config.yml and domain.yml will be taken as default values unless specified otherwise on the command line interface.

Try both of these because I’m not sure anyway.

You solved it ?

Hi @Amandeep ,

If you want to train a RASA model then you would need domain.yml, nlu.yml and stories.yml with you in the project directory.

In Rasa 3.x, If you want to train a model manually using python then you can import the following class from rasa inside your python application

from rasa.model_training import train

You can use it to train your model

I have intents and responses in a database and i have a function which can collect the data from a database and form it into the yaml format as required like the nlu.yml file. Now i want to train those intents in the database whenever i run rasa train so should i write a custom importer for it ? If yes, then can you give me a working example for it. Thank You !

You just want to train a NLU model?

Apologoes for not conveying my question properly . let me ask it again My client has a database in which there are questions and answers so what should i do to automate the process of copy and pasting the intents and responses from the database to the nlu, domain, rules files ? Can i write a custom importer which will fetch the data from the database and write the domain , nlu, rules files ?

If you are training a Rasa model inside a python application such as flask/FastAPI or any other framework

Then while initializing the project, You can fetch the files from the DB and prepare YML files for them and then use this from rasa.model_training import train to train NLU+ core or if you only want to train NLU part only the use this from rasa.model_training import train_nlu

This will help you to create Rasa model and to load them in your python application [ This will load NLU and NLU + CORE model ]

from fastapi import FastAPI
from rasa.core.agent import Agent
 
app = FastAPI()

 
 
@app.get("/predictText")
async def read_item(modelId: str, query: str):
    
    modelName = f'{modelId}_nlu.tar.gz'
    agent_nlu = Agent.load(modelName)
    message = await agent_nlu.parse_message(query)
    # print(message)

    return {"prediction_info": message}

So this means we need to load the model every time we want to parse a message?

Is there some way to keep a model running and access it?

A blog or documentation of how an agent work is very appreciated. Thank you in advance.

Hi @namph-sgn

You don’t have to load model every time, While starting the server you can load the model in a class variable

Giving you the small sketch on that

class LoadRasaModel:
       rasamodel = None
def init():
      load the model  here

Now in app.py or any file which loads first do this

add the following line

do proper import

Then load the init method of Rasa class, It will load the model in that class variable

Now you’ll be interacting with the model via an API right, simply use the class variable in which the mdoel is loaded

I will write a descriptive blog on this and will share here this weekend, Till then this is the sketch on how to achieve this

Thanks @anoopshrma for the swift response, I will try out this solution. I’m looking forward to the blog this weekend.

Hi @namph-sgn

I have added the blog on how to create NLU model and how to sustain them in the server for all the time Please have a look - Rasa NLU implementation with Python

Feedbacks are highly appreciated