Code Monkey home page Code Monkey logo

rasa-faq-bot's Introduction

rasa-faq-bot

Using Rasa to build a FAQ bot 中文版本

GitHub stars GitHub issues GitHub license

Introduction

Rasa is a powerful dialog framework that we can easily use it to build a practical dialog system.

FAQ(Frequently Asked Questions) bot is the most important part of the smart customer service products, which help users to solve general problems.

Usually we collect common questions and answers from actual scenarios and we call these standard question-and-answer pairs for the knowledge base. FAQ-bot will intelligently answer user questions based on this knowledge base.

So, in this project, we will use Rasa to build a smart faq-bot!

Get Started

1. Run the shell and activate bert-service or computer sentence embedding with sentence-transformers

We store the knowledge in data/nlu/faq.json,which includes lots of question-and-answer pairs. We use bert-service to calculate the similarity between the user question and the questions in the knowledge base. Then we choose the most similar question and return the corresponding answer to the user.

  • Specific operation:
    • Install bert-serving-server and bert-serving-client,More information about bert-as-service
    • Download pre-trained BERT model,unzip it(for example, cased_L-24_H-1024_A-16.zip).
    • Next, you need to change "BERT_ENGLISH_MODEL_DIR" into your model path
       -bert-serving-start \
       -pooling_layer -4 -3 -2 -1 \
       -model_dir=BERT_ENGLISH_MODLE_DIR \
       -num_worker=8 \
       -max_seq_len=16
    • Run the shell
    ./run_bert_service.sh
  • Alternatively, you can also choose to use sentence transformer to coputer sentence embedding. Later, sentence embeeding can be used to calculate the similarity between the user question and the questions in the knowledge base and thus you can skip the earlier option bert-as-service. The original repo of sentence transformer is available at here.
  • In order to use this, please modify the action.py file. Enable the sentence_transformer_select flag as True and chose pretrained_model for sentence embedding with different pooling schemes are available, please refer to this link for more details.
sentence_transformer_select=True
pretrained_model='bert-base-nli-mean-tokens'
  • There are several options for pre-trained BERT models with different pooling schemes are:

    • bert-base-nli-mean-tokens: BERT-base model with mean-tokens pooling.
    • bert-base-nli-max-tokens: BERT-base with max-tokens pooling.
    • bert-base-nli-cls-token: BERT-base with cls token pooling.
    • bert-large-nli-mean-tokens: BERT-large with mean-tokens pooling.
    • bert-large-nli-max-tokens: BERT-large with max-tokens pooling.
    • bert-large-nli-cls-token: BERT-large with CLS token pooling.
  • Also, note that you can modify the matching score threshold criteria by changing the score_threshold in actions.py file based on your need.

2. Run Rasa custom actions

We design our own customer actions in actions.py which which is how the dialog responds to the user when it receives the customer's information. If the user's intent is faq, then go to the knowledge base to find the best match and give the user answers. If there is no match to the right question, we will also tell the user "Sorry, this question is beyond my ability."

  • Change the port in endpoints.yml to keep the port the same as the port of rasa actions(default port is 5055):
action_endpoint:
  url: "http://localhost:5055/webhook"
  • Run rasa actions:
rasa run actions

Then you can get a log like this:

│2019-08-09 11:10:32 INFO     rasa_sdk.endpoint  - Starting action endpoint server...
│(1000, 3072)
│2019-08-09 11:10:33 INFO     rasa_sdk.executor  - Registered function for 'action_get_answer'.
│2019-08-09 11:10:33 INFO     rasa_sdk.endpoint  - Action endpoint is up and running. on ('0.0.0.0', 5055)

3. Run Rasa X

rasa x

And you can get a log like this:

Starting Rasa X in local mode... �🚀                                                                                               
 
The server is running at http://localhost:5005/login?username=me&password=zrjV0BwYSzYP

Copy the link into your browser, then you can access your rasa x page.

Dialog example

4. Note

  • Sometimes when the service is started, the port is already in use. At this time, it is better to kill the process of the relevant port directly.
  • If the error is about the database is locked,just delete rasa.db and tracker.db.
  • You'd better use compatible rasa and rasa x,this project uses rasa 1.2.2 and rasa x 0.20.1(they are compatible)
  • Use your own data to train a model:
    • Follow the data foemat in data/nlu/faq.json,replace it by your own data
    • Run process.py(Default data size is at most 1000 pieces,you can modify the process.py on your own)
    • Run actions.py
    • Please tune the matching threshold score as per your requirement.

rasa-faq-bot's People

Contributors

1120172175 avatar maulikmadhavi avatar nghuyong avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

rasa-faq-bot's Issues

rasa run actions resulted following error

2021-05-16 19:35:30 INFO rasa_sdk.endpoint - Starting action endpoint server...
2021-05-16 19:35:31 INFO root - Load pretrained SentenceTransformer: bert-base-nli-mean-tokens
2021-05-16 19:35:31 INFO root - Load SentenceTransformer from folder: bert-base-nli-mean-tokens
2021-05-16 19:35:34 INFO root - Use pytorch device: cpu
Standard question size 1000
Start to calculate encoder....
Batches: 0%| | 0/125 [00:00<?, ?it/s]Truncation was not explicitly activated but max_length is provided a specific value, please use truncation=True to explicitly truncate examples to max length. Defaulting to 'longest_first' truncation strategy. If you encode pairs of sequences (GLUE-style) with the tokenizer you can select this strategy more precisely by providing a specific strategy to truncation.
/opt/rasa/venv/lib/python3.6/site-packages/transformers/tokenization_utils_base.py:2143: FutureWarning: The pad_to_max_length argument is deprecated and will be removed in a future version, use padding=True or padding='longest' to pad to the longest sequence in the batch, or use padding='max_length' to pad to a max length. In this case, you can give a specific length with max_length (e.g. max_length=45) or leave max_length to None to pad to the maximal input size of the model (e.g. 512 for Bert).
FutureWarning,
Batches: 0%| | 0/125 [00:00<?, ?it/s]
Traceback (most recent call last):
File "/opt/rasa/venv/bin/rasa", line 8, in
sys.exit(main())
File "/opt/rasa/venv/lib/python3.6/site-packages/rasa/main.py", line 92, in main
cmdline_arguments.func(cmdline_arguments)
File "/opt/rasa/venv/lib/python3.6/site-packages/rasa/cli/run.py", line 52, in run_actions
sdk.main_from_args(args)
File "/opt/rasa/venv/lib/python3.6/site-packages/rasa_sdk/main.py", line 21, in main_from_args
args.auto_reload,
File "/opt/rasa/venv/lib/python3.6/site-packages/rasa_sdk/endpoint.py", line 137, in run
action_package_name, cors_origins=cors_origins, auto_reload=auto_reload
File "/opt/rasa/venv/lib/python3.6/site-packages/rasa_sdk/endpoint.py", line 80, in create_app
executor.register_package(action_package_name)
File "/opt/rasa/venv/lib/python3.6/site-packages/rasa_sdk/executor.py", line 250, in register_package
self._import_submodules(package)
File "/opt/rasa/venv/lib/python3.6/site-packages/rasa_sdk/executor.py", line 206, in _import_submodules
package = self._import_module(package)
File "/opt/rasa/venv/lib/python3.6/site-packages/rasa_sdk/executor.py", line 227, in _import_module
module = importlib.import_module(name)
File "/usr/local/lib/python3.6/importlib/init.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 994, in _gcd_import
File "", line 971, in _find_and_load
File "", line 955, in _find_and_load_unlocked
File "", line 665, in _load_unlocked
File "", line 678, in exec_module
File "", line 219, in _call_with_frames_removed
File "/opt/rasa/rasa-faq/actions.py", line 94, in
encode_standard_question(sentence_transformer_select,pretrained_model)
File "/opt/rasa/rasa-faq/actions.py", line 86, in encode_standard_question
standard_questions_encoder = torch.tensor(bc.encode(standard_questions)).numpy()
File "/opt/rasa/venv/lib/python3.6/site-packages/sentence_transformers/SentenceTransformer.py", line 150, in encode
out_features = self.forward(features)
File "/opt/rasa/venv/lib/python3.6/site-packages/torch/nn/modules/container.py", line 119, in forward
input = module(input)
File "/opt/rasa/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "/opt/rasa/venv/lib/python3.6/site-packages/sentence_transformers/models/BERT.py", line 33, in forward
output_states = self.bert(**features)
File "/opt/rasa/venv/lib/python3.6/site-packages/torch/nn/modules/module.py", line 889, in _call_impl
result = self.forward(*input, **kwargs)
File "/opt/rasa/venv/lib/python3.6/site-packages/transformers/models/bert/modeling_bert.py", line 912, in forward
batch_size, seq_length = input_shape
ValueError: not enough values to unpack (expected 2, got 1)

error after executing rasa run actions

(venv) root@rasadev:/opt/rasa/rasa-faq# rasa run actions
2021-05-13 20:01:39 INFO rasa_sdk.endpoint - Starting action endpoint server...
2021-05-13 20:01:39 ERROR rasa_sdk.executor - Failed to register package 'actions'.
Traceback (most recent call last):
File "/opt/rasa/venv/lib/python3.8/site-packages/rasa_sdk/executor.py", line 262, in register_package
self._import_submodules(package)
File "/opt/rasa/venv/lib/python3.8/site-packages/rasa_sdk/executor.py", line 218, in _import_submodules
package = self._import_module(package)
File "/opt/rasa/venv/lib/python3.8/site-packages/rasa_sdk/executor.py", line 239, in _import_module
module = importlib.import_module(name)
File "/usr/lib/python3.8/importlib/init.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "", line 1014, in _gcd_import
File "", line 991, in _find_and_load
File "", line 975, in _find_and_load_unlocked
File "", line 671, in _load_unlocked
File "", line 783, in exec_module
File "", line 219, in _call_with_frames_removed
File "/opt/rasa/rasa-faq/actions.py", line 11, in
import torch
ModuleNotFoundError: No module named 'torch'

Cannot install rasa x 0.20.1

There is lots of compatibility issues here

I cannot install rasa x 0.20.1 so I had to install newer version, which eventually leads to install Tensorflow 2.x, and bert as a service cannot run on TF >2, only can run with TF <1.15

It's a myth how you can install rasa x 0.20.1...

Updated: I didnt install rasa x anymore but instead use rasa train normally, and then use rasa shell to interact with the chatbot. However, Segmentation Fault appears. I tried on both my Mac machines, same fault

installation issue with deep learning linux ami ec2

Hello Team,

I have to install rasa-faq-bot to amazon ec2 but tensorflow have issues to install while install rasa. Can you send some guidelines to properly install this in linux ami. Looking forward to hear from you.

Bot in Rasa X doesn't answer

I try to run a simple example with the default data. However, when I use the interactive bot, no matter what I write, the bot stuck after action_listen and it doesn't return any response.

The steps I followed in a Windows laptop:

  1. Downloaded this repository
  2. Install Bert as A service and run the .sh file

The command shows this

I:WORKER-0:ready and listening!
I:WORKER-1:ready and listening!
I:VENTILATOR:all set, ready to serve request!
I:VENTILATOR:new config request req id: 1       client: b'8e0660af-7981-49c0-a31                           7-b620f0c8fe91'
I:SINK:send config      client b'8e0660af-7981-49c0-a317-b620f0c8fe91'
  1. Then I installed both Rasa and Rasa X. For Rasa X, I had to do it with multipass as the official documentation says.
  2. Then on the folder, I run the command rasa run actions

I get this response

2020-05-14 11:33:38 INFO     rasa_sdk.endpoint  - Starting action endpoint server...
(1000, 3072)
2020-05-14 11:33:44 INFO     rasa_sdk.executor  - Registered function for 'action_get_answer'.

Then on Rasa X, I added the FAQ.md into the NLU tab and train the model. I set it as active and tried the interactive bot. But as I said, no response with the dots just hanging there as someone is writing.

Operand/shape error

Sending a query from Rasa to the action server generates the following error:
score = np.sum((self.standard_questions_encoder * query_vector), axis=1) / (self.standard_questions_encoder_len * (np.sum(query_vector * query_vector) ** 0.5)) ValueError: operands could not be broadcast together with shapes (1000,3072) (768,)
The very similar code from the bert-as-service example,
score = np.sum(query_vec * doc_vecs, axis=1) / np.linalg.norm(doc_vecs, axis=1)
works as expected.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.