Please tell in which city, Hubli or Dharwar? hubli {‘intent’: {‘name’: ‘confirm_specialization’, ‘confidence’: 0.40392767206040286}, ‘entities’: [], ‘intent_ranking’: [{‘name’: ‘confirm_specialization’, ‘confidence’: 0.40392767206040286}, {‘name’: ‘confirm_date’, ‘confidence’: 0.37037497733595404}, {‘name’: ‘greet’, ‘confidence’: 0.085348703255119174}, {‘name’: ‘confirm_city’, ‘confidence’: 0.085215702698709567}, {‘name’: ‘goodbye’, ‘confidence’: 0.023905681599622659}, {‘name’: ‘affirm’, ‘confidence’: 0.014307775582506817}, {‘name’: ‘bye’, ‘confidence’: 0.0082719908852013461}, {‘name’: ‘confirm_time’, ‘confidence’: 0.0055622524680621682}, {‘name’: ‘create_apt’, ‘confidence’: 0.0030852441144210963}], ‘text’: ‘hubli’} Traceback (most recent call last): File “train_rasa_nlu_model.py”, line 178, in responses = agent.handle_message(a) File “C:\Users\MS066115\AppData\Local\Continuum\anaconda3\lib\site-packages\rasa_core\agent.py”, line 114, in handle_message UserMessage(text_message, output_channel, sender_id)) File “C:\Users\MS066115\AppData\Local\Continuum\anaconda3\lib\site-packages\rasa_core\processor.py”, line 84, in handle_message self._predict_and_execute_next_action(message, tracker) File “C:\Users\MS066115\AppData\Local\Continuum\anaconda3\lib\site-packages\rasa_core\processor.py”, line 262, in _predict_and_execute_next_action dispatcher) File “C:\Users\MS066115\AppData\Local\Continuum\anaconda3\lib\site-packages\rasa_core\processor.py”, line 311, in _run_action self._log_action_on_tracker(tracker, action.name(), events) File “C:\Users\MS066115\AppData\Local\Continuum\anaconda3\lib\site-packages\rasa_core\processor.py”, line 369, in _log_action_on_tracker tracker.update(e) File “C:\Users\MS066115\AppData\Local\Continuum\anaconda3\lib\site-packages\rasa_core\trackers.py”, line 296, in update raise ValueError("event to log must be an instance " ValueError: event to log must be an instance of a subclass of Event.
(base) C:\Users\MS066115\Documents\chatbot\doctor_appoint>
################ Domain file
actions:
- utter_greet
- utter_bye
- utter_ask_city
- utter_ask_specialization
- utter_ask_date
- utter_ask_time
- utter_doctor_details
- actions.SaveCity
- actions.SaveSpecialization
- actions.SaveDate
- actions.SaveTime
- actions.ListOfDoctors
- utter_unclear entities:
- dr
- date
- city
- time
- specialist intents:
- create_apt
- confirm_city
- confirm_specialization
- confirm_date
- confirm_dr
- confirm_time
- greet
-
goodbye slots: city: type: text date: type: text specialist: type: text time: type: text templates: utter_ask_city:
text: “In which city would you like to book appointment Hubli or Dharwar?”
text: “Please tell in which city, Hubli or Dharwar?” utter_ask_date:
text: “When do you want to schedule this appointment?”
text: “Ok, for what day?” utter_ask_specialization:
text: “Which specialist are you looking for?”
text: “Please mention your specialist or Specialization?” utter_ask_time:
text: “please select the time?”
text: “Ok, for what time?” utter_bye:- “text:It’s My pleasure to help you”
- text: “Thak you” utter_doctor_details:
- text: “Here i found similar results: Name,Specialization,Address,City,Phone number,Rating” utter_greet:
- text: “Hello, how can I help you?”
- text: “Hello, welcome to the Doctor Appointment Bot!” utter_unclear:
- text: “I am not sure what you are aiming for.Kindly try it again”
####################################################################
code
from future import absolute_import from future import division from future import unicode_literals from rasa_nlu.training_data import load_data from rasa_nlu.model import Trainer from rasa_nlu import config from rasa_core.policies import FallbackPolicy, KerasPolicy, MemoizationPolicy from rasa_core.agent import Agent from rasa_core.actions import Action from rasa_core.events import SlotSet
from rasa_nlu.evaluate import run_evaluation
import requests
from rasa_core.domain import Domain #from rasa_core.trackers import EventVerbosity
import logging logger = logging.getLogger(name)
#import requests #import json
from rasa_core_sdk import Action from rasa_core_sdk.events import SlotSet from rasa_core_sdk.events import UserUtteranceReverted from rasa_core_sdk.events import AllSlotsReset from rasa_core_sdk.events import Restarted
nlu training data sample to be loaded
train_data = load_data(“data/data.json”)
#loading trainer to teach existing flow trainer = Trainer(config.load(“configuration.yml”))
model training
interpreter = trainer.train(train_data)
store model
model_directory = trainer.persist("./models/nlu", fixed_model_name=“doctornlu”)
“”"
here you can perform task of calling third party api if you integrate it for accessing thier data.
class ApiAction(Action): def name(self): return “action_retrieve_image”
def run(self, dispatcher, tracker, domain):
dispatcher.utter_message("looking for a good img")
url = "https://picsum.photos/200/300/?random"
return [SlotSet("img_api_response", url)]
"""
################################################################################################################ class SaveCity(Action): def name(self): return ‘action_save_city’ def run(self, dispatcher, tracker, domain): city_name = next(tracker.get_latest_entity_values(“city”), None) return [SlotSet(‘city’,city_name)]
class SaveSpecialization(Action): def name(self): return ‘action_save_specialization’
def run(self, dispatcher, tracker, domain):
specialization_in = next(tracker.get_latest_entity_values("specialist"), None)
return [SlotSet('specialist',specialization_in)]
class SaveDate(Action): def name(self): return ‘action_save_date’
def run(self, dispatcher, tracker, domain):
dates = next(tracker.get_latest_entity_values("date"), None)
return [SlotSet('date',dates)]
class SaveTime(Action): def name(self): return ‘action_save_time’
def run(self, dispatcher, tracker, domain):
timing= next(tracker.get_latest_entity_values("time"), None)
return [SlotSet('time',timing)]
#import re import pandas as pd
class ListOfDoctors(Action):
def __init__(self):
#with open("C:/Users/MS066115/Documents/chatbot/doctor_appoint/data/dlist.csv",'rb') as f:
# self.Doctors_data = f.read()
self.Doctors_data = pd.read_csv("C:/Users/MS066115/Documents/chatbot/doctor_appoint/data/dlist.csv",encoding='ISO-8859-1')
def name(self):
return 'action_list_doctors'
def run(self, dispatcher, tracker, domain):
city_name=tracker.get_slot('city')
specialization_in=tracker.get_slot('specialist')
#dates=tracker.get_slot('date')
timing=tracker.get_slot('time')
## create objects of the each class and access each the functions
#SaveCityObj = SaveCity()
#SaveSpecializationObj = SaveSpecialization()
#SaveTimeObj= SaveTime()
#cityx = SaveCityObj.run()
#data1= data[(data['time'].str.contains(y) == True) & (data['specialization'].str.contains(spec) == True) & (data['location'].str.contains(loc) == True)]
filterd_data=self.Doctors_data[(self.Doctors_data['City'].str.contains(city_name)==True) & (self.Doctors_data['Specialization'].str.contains(specialization_in)==True)&(self.Doctors_data['Time'].str.contains(timing)==True)]
if filterd_data is None:
response = "Sorry I couldn't find anything relevant to your query!"
else:
res_name=filterd_data['Name'].head(5).to_string(index=False)
res_specialization=filterd_data['Specialization'].head(5).to_string(index=False)
res_address=filterd_data['Address'].head(5).to_string(index=False)
res_city=filterd_data['Location'].head(5).to_string(index=False)
res_phonenum=filterd_data['Phone number'].head(5).to_string(index=False)
res_timing=filterd_data['Time'].head(5).to_string(index=False)
res_rating=filterd_data['Rating'].head(5).to_string(index=False)
response = "Here's something I found, \n\n res_name:\n{} \t res_specialization: {} \t res_address: {} \t res_city: {} \t res_phonenum: {} \t res_timing{} \t res_rating{}\n".format(res_name, res_specialization, res_address, res_city,res_phonenum,res_timing,res_rating)
dispatcher.utter_message(response)
"""
res_name=self.Doctors_data['Name']
res_specialization=self.Doctors_data['Specialization']
res_address=self.Doctors_data['Address']
res_city=self.Doctors_data['City']
res_phonenum=self.Doctors_data['Phone number']
res_timing=self.Doctors_data['Time']
res_rating=self.Doctors_data['Rating']
response = "Here's something I found, \n\n res_name: {} \n res_specialization: {} \n res_address: {} \n res_city: {} \n res_phonenum: {}
\n res_timing{} \n res_rating{}\n".format(res_name, res_specialization, res_address, res_city,res_phonenum,res_timing,res_rating)
else:
response = "Sorry I couldn't find anything relevant to your query!"
"""
#############################################################################################################################
this will catch predictions the model isn’t very certain about
there is a threshold for the NLU predictions as well as the action predictions
fallback = FallbackPolicy(fallback_action_name=“utter_unclear”, core_threshold=0.1, # this core threshold limits the fallback_Action you can change it. nlu_threshold=0.15) # nlu threshold help model to choose reply based on identification of intent and only responses above the specified range.
agent = Agent(‘doctor_domain.yml’, policies=[MemoizationPolicy(), KerasPolicy(), fallback])
loading our neatly defined training dialogues
training_data = agent.load_data(‘data/stories.md’)
agent.train(training_data,validation_split=0.1,epochs=800, batch_size=1)
agent.persist(‘models/dialogue’)
agent = Agent.load(‘models/dialogue’, interpreter=model_directory)
print(“Your bot is ready to talk! Type your messages here or send ‘stop’”) while True: a = input() print(interpreter.parse(a)) if a == ‘stop’: break responses = agent.handle_message(a) for response in responses: print(response[“text”])