How to make rasa chatbot Fetching csv file

I am building a Chinese chatbot for rasa

How can I make rasa read the answers in the csv?

My csv file looks like this:

question,answer

q1,answer1

q2,answer2

q3,answer3

…

When I said q1, let rasa answer answer 1

@dan246

Note: your excel file should have two fields i.e question | answer or what response you want to return but should be synchronized and even it should have variations of the question with answers.

@dan246 updated code on 23-Nov-2021

  1. Please see this

     class GetAnswer(Action):
         def __init__(self):
             self.faq_d = pd.read_csv('./data/faq_d.csv')
             qs = list(self.faq_d['question'])
             with open("./data/nlu/faq.yml", "wt", encoding="utf-8") as f:
                 f.write('version: "2.0"\n')
                 f.write("nlu: \n- intent: question\n  examples: | \n")
                 for q in qs:
                     f.write(f"    - {q}\n") 
         return []
    
  2. When the above piece of code fetches the data i,e question part and make the faq.yml file of that data which you want to train and then later it will go into the second code that’s FuzzyWuzzy and then return your response. For me, it’s a clean process, but yes you need to fine-tune. Do try this code and search the FuzzyWuzzy process.

The advantage of this process is just added and run and that in excel/csv.

I hope this will solve your query and give you idea. Good Luck!

ok

let me try

thanks!!

@dan246 No worries, any time you are stuck please do tag me @ and nik202. I know you can do it. Good Luck!

@nik202 Hi, should I put the program in actions.py?

Because I noticed that there is a code similar to transcoding

which means I don’t need to fill in each piece of data with nlu, or do I need to set something?

@dan246 yes you need to mention the code in action.py as it and try run you will able to see the file. in data.

1 Like

I am now extracting the data in the csv file like this:

擷取

(csv data)

use python to output :

擷取

Then put it in rasa for training

@dan246 I will recommend to create a proper question answers ie.

file name for this is faq_d.csv

question |answer

How we can install rasa and rasa x | Please visit this link for installation Installation Guide How are the different types of slots | Domain

etc etc

When you run the above custom code, it will create the faq.yml file in the data folder which we will used to train our mode as we do manually and then you need a fuzzywuzzy logic to fetch the answers.

Confirm me:

  1. Are you able to do this?
  2. Do you understand this logic of fuzzywuzzy?

I’d would recommend to first do this process which I mentioned.

@dan246 can you explain more what you actually want?

The code I suggested will only create the intent and training examples, it will not create the domain and stories data.

@nik202 What could we do to let the robot answer the question beyond the data we input though it had not be trained before, even if the answer is not correct at all

I know fuzzywuzzy, it’s hard for me, but I want to do it well

(Second, the third sentence has not been trained, so the robot did not answer)

the robot can’t answer the qustion beyond its content and trainig examples. If it encounters the condition mentioned above, it just answer nothing. I hope it could answer the question beyond its content and trainig examples, even if the answer isn’t correct .

@dan246 how bot will answer the question which not reside in database (.csv) or training examples?

I dnt know your language.

I not get you what you try to do? and is code running which I suggested? Please confirm me that first.

@nik202 I put it in actions.py and trained rasa, but he did not run out of faq.yml

Do i need to add something else?

@dan246 dan I thought you will mention this in action.py mate?

Update code on 23-Nov-2021 @dan246

class GetAnswer(Action):
     def __init__(self):
         self.faq_d = pd.read_csv('./data/faq_d.csv')
         qs = list(self.faq_d['question'])
         with open("./data/nlu/faq.yml", "wt", encoding="utf-8") as f:
             f.write('version: "2.0"\n')
             f.write("nlu: \n- intent: question\n  examples: | \n")
             for q in qs:
                 f.write(f"    - {q}\n") 

    def name(self) -> Text:
        return "action_get_answer"

        return []

and you need to mention action_get_answer in domain.yml too?

I can share you complete code and with running commands but it will kill your learning bro.

Try this please and then do let me know.

Hope you willl understand this. Thanks.

@nik202 hi,I’m sorry for the late response.

I tried the above method but still can’t succeed

I tried rasa shell and he ran the following error

I checked rasa run actions

I tried to find a solution by myself but failed :sob:

Can you tell me how to improve? thank you

擷取

@dan246 Dan check the indentation, can you put 1 space before

def name(self) -> Text:

It should be alined with above def

Try

Screenshot 2021-11-22 at 5.33.59 PM

To confirm code is running properly . Good Luck!

@nik202 Good afternoon, it ran out of new errors :cry:

Is my file format wrong?

Or am I not setting it somewhere?

@dan246 I have shared you everything bro, please share me the code again or just copy and paste it action.py file which I wrote and just run rasa run actions.

@nik202 sorry

擷取

My csv data looks like this

csv

domain

image

Where do I need to add anything (ex. nlu or stories)

@dan246 send me your action.py file, your .csv file.