I am making a chatbot which fetch data from datatable, but when i query for example "What is SEX of MS203T008-408-010 ?" i am getting weird response. Is something wrong with code i am not able to figure out

actions.py

import pandas as pd import numpy as np from collections import OrderedDict import os import random

import logging, io, json, warnings logging.basicConfig(level=“INFO”) warnings.filterwarnings(‘ignore’)

import rasa_nlu import rasa_core import spacy

from rasa_nlu.training_data import load_data from rasa_nlu.model import Trainer from rasa_nlu import config from rasa_nlu.model import Interpreter from rasa_core_sdk import Action

class demographicsprog(Action): def name(self): return “action_demo” def run(self, dispatcher, tracker, domain): dispatcher.utter_message(“Sure, On it!”) return [] def init (self, datafilename,primarycolumnname): self.datafilename = datafilename self.primarycolumnname = primarycolumnname self.nlu_data_filename = “data/nlu.md” self.nlu_model_dirname = “./models/nlu” self.nlu_model_name = “current” self.config_filename = “config.yml” self.model_directory = self.nlu_model_dirname + “/default/” + self.nlu_model_name self.nlu_model = None self.df = None self.d = OrderedDict() self.d = self.add_std_intents(self.d) self.build_model()

def add_std_intents(self,d):
    d['greet'] = ["hey","howdy","hey there","hello","hi"]
    d['affirm'] = ["yes","yep","yeah","indeed","that's right","ok","great","right, thank you","correct","great choice", "sounds really good"]
    d["goodbye"] = ["bye","goodbye","good bye","See you","CU","Chao","Bye bye","have a good one"]
    return d
    
def populate_dataframe(self):
    self.df = pd.read_csv(self.datafilename, encoding="ISO-8859-1")

    self.primary_key_values_list = self.df[self.primarycolumnname].tolist()
    
def populate_nlu_training_data(self):

    if(os.path.exists(self.nlu_data_filename)):
        return
    
    n_sample_countries = 5
    n_sample_columns = 5
    self.all_columns = [col for col in list(self.df.columns) if col != self.primarycolumnname]
    sample_sentences = []
    for c in self.primary_key_values_list[:n_sample_countries]:
        for col in self.all_columns[:n_sample_columns]:
            if "Unnamed" in col or "NaN" in c:
                continue
            sentenceformat1 = "What is [" + col + "](column) for [" + c + "](row) ?"
            sentenceformat2 = "For  [" + c + "](row), what is the [" + col + "](column) ?"
            sample_sentences.append(sentenceformat1)
            sample_sentences.append(sentenceformat2)
        
    self.d['query'] = sample_sentences
    
    with open(self.nlu_data_filename, 'w') as f:
        for k,v in self.d.items():
            f.write("## intent:" + k)
            for it in v:
                f.write("\n- " + it)
            f.write("\n\n")
    
def pprint(self,o):   
    print(json.dumps(o, indent=2))
        
    
def train_nlu_model(self):
    if(os.path.exists(self.model_directory)):
        return
  
    training_data = load_data(self.nlu_data_filename)
   
    trainer = Trainer(config.load(self.config_filename))

    self.interpreter = trainer.train(training_data)
    # store it for future use
    self.model_directory = trainer.persist(self.nlu_model_dirname, fixed_model_name=self.nlu_model_name)
    self.nlu_model = Interpreter.load(self.model_directory)


def build_model(self):
    if self.nlu_model == None:
        self.populate_dataframe()
        self.populate_nlu_training_data()
        self.train_nlu_model()
    
def process_query_intent(self,entities):
               
    row = None
    col = None
    for ent in entities:
        if ent["entity"] == "column":
            col = ent["value"].title()
        if ent["entity"] == "row":
            row = ent["value"].title()
    if row != None and col != None:
        value = self.df.loc[self.df[self.primarycolumnname] == row,col].item()
        return value.replace(";",",")
    return "Could not follow your question. Try again"

def process_other_intents(self,intent):
    values = self.d[intent]
    return random.choice(values)
                
def query(self, usr):
  
    if self.nlu_model == None:
        self.nlu_model = Interpreter.load(self.model_directory)
        self.populate_dataframe()
                
    try:
        response_json = self.nlu_model.parse(usr)
        entities = response_json["entities"]
        intent = response_json['intent']['name']
        if intent == 'query':
            return self.process_query_intent(entities)
        else:
            return self.process_other_intents(intent)
    except Exception as e:
        print(e)
        return "Could not follow your question [" + usr + "], Try again"

if name == " main ": datafilename = “data/DM.csv” primarycolumnname = “USUBJID” dfmodel = demographicsprog(datafilename, primarycolumnname)

response = dfmodel.query("Hi")
print(response)
    
response = dfmodel.query("What is RACE of MS203T008-408-015?")
print(response)

response = dfmodel.query("What is SEX of MS203T008-408-010?")
print(response.encode("ISO-8859-1"))

response = dfmodel.query("Bye")
print(response)

nlu.md

intent:greet

  • hey
  • howdy
  • hey there
  • hello
  • hi
  • good morning
  • good evening
  • dear sir

intent:affirm

  • yes
  • yep
  • yeah
  • indeed
  • that’s right
  • ok
  • great
  • right, thank you
  • correct
  • great choice
  • sounds really good

intent:goodbye

  • bye
  • goodbye
  • good bye
  • See you
  • CU
  • Chao
  • Bye bye
  • have a good one

intent:demographics

stories.md

happy path

  • greet
    • utter_greet
  • mood_great
    • utter_happy

sad path 1

  • greet
    • utter_greet
  • mood_unhappy
    • utter_cheer_up
    • utter_did_that_help
  • affirm
    • utter_happy

sad path 2

  • greet
    • utter_greet
  • mood_unhappy
    • utter_cheer_up
    • utter_did_that_help
  • deny
    • utter_goodbye

say goodbye

  • goodbye
    • utter_goodbye

bot challenge

  • bot_challenge
    • utter_iamabot

demographics_happy

  • greet
    • utter_greet
  • demographics
    • action_demo
  • goodbye
    • utter_goodbye

endpoints.yml

action_endpoint: url: “http://localhost:5055/webhook

domains.yml

session_config: session_expiration_time: 60.0 carry_over_slots_to_new_session: true intents:

  • greet
  • goodbye
  • affirm
  • deny
  • mood_great
  • mood_unhappy
  • bot_challenge
  • demographics entities:
  • column
  • row responses: utter_greet:
    • text: Hey! How are you? utter_cheer_up:
    • text: ‘Here is something to cheer you up:’ image: https://i.imgur.com/nGF1K8f.jpg utter_did_that_help:
    • text: Did that help you? utter_happy:
    • text: Great, carry on! utter_goodbye:
    • text: Bye utter_iamabot:
    • text: I am a bot, powered by Rasa. actions:
  • utter_greet
  • utter_cheer_up
  • utter_did_that_help
  • utter_happy
  • utter_goodbye
  • utter_iamabot
  • action_demo

config.yml

language: ‘en’ pipeline: pretrained_embeddings_spacy

policies:

  • name: MemoizationPolicy
  • name: KerasPolicy
  • name: MappingPolicy

@siddharthchauhan can you post the response you’re getting?

Here are the following ouput Logs from action server: Your input -> hi Hey! How are you? Your input -> what is SEX of MS203T008-408-001? 2020-02-21 15:30:38 ERROR rasa.core.actions.action - The model predicted the custom action ‘action_demo’, but you didn’t configure an endpoint to run this custom action. Please take a look at the docs and set an endpoint configuration via the --endpoints flag. Actions 2020-02-21 15:30:38 ERROR rasa.core.processor - Encountered an exception while running action ‘action_demo’. Bot will continue, but the actions events are lost. Please check the logs of your action server for more information. Your input ->

So, My Problem is to convert natural language to database. I wrote a custom code for it. I am trying to debug it but it’s not working.

@siddharthchauhan did you specify the action_endpoint in the endpoints.yml file?

Yes.

action_endpoint: url: “http://localhost:5055/webhook”