bb_chatbot / actions /actions.py
gneya-bacancy's picture
Upload 19 files
3060f32 verified
# This files contains your custom actions which can be used to run
# custom Python code.
#
# See this guide on how to implement these action:
# https://rasa.com/docs/rasa/custom-actions
# This is a simple example for a custom action which utters "Hello World!"
from typing import Any, Text, Dict, List
import os
from rasa_sdk import Action, Tracker
from rasa_sdk.events import SlotSet
from rasa_sdk.executor import CollectingDispatcher
from actions.ChatBot import ChatBot
from actions.free_flow import FormFillingBot
from typing import Text, List, Any, Dict
import json
import psycopg2
from rasa_sdk.events import SlotSet, ActiveLoop
from rasa_sdk import Tracker, FormValidationAction, Action
from rasa_sdk.events import EventType, UserUtteranceReverted, SlotSet
from rasa_sdk.executor import CollectingDispatcher
from langchain_mongodb.chat_message_histories import MongoDBChatMessageHistory
# from langchain.chains import ConversationalRetrievalChain
from langchain_openai import ChatOpenAI
from langchain.memory import ConversationBufferMemory
from rasa_sdk.events import SlotSet, FollowupAction
from rasa_sdk.executor import CollectingDispatcher
from pymongo.mongo_client import MongoClient
from typing import Any, Text, Dict, List
from rasa_sdk import Action, Tracker
from langchain.chains import LLMChain
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
)
class ActionChat(Action):
def name(self) -> Text:
return "action_chat"
def run(self,dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
print("In action chat")
current_state = tracker.current_state()
print(current_state["latest_message"])
print(f"sender_id: {tracker.sender_id}")
latest_msg = current_state["latest_message"]['text']
cb = ChatBot(tracker.sender_id)
cb.create_llm_chain()
res = cb.get_response(latest_msg)
print(res)
dispatcher.utter_message(res)
SlotSet("data", None)
return [UserUtteranceReverted()]
class ActionFreeFlow(Action):
def name(self) -> Text:
return "action_free_flow"
def run(self,dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
print("FREE FLOW ACTION CALLED")
current_state = tracker.current_state()
print(current_state["latest_message"])
latest_msg = current_state["latest_message"]['text']
slot_value = tracker.get_slot("data")
print(slot_value)
uuid = tracker.sender_id
cb = FormFillingBot()
default_json=cb.get_current_form_json(uuid)
res= cb.form_filling(latest_msg,default_json)
dispatcher.utter_message(res)
return [SlotSet("data", None)]
#rutvi's code
class ActionAskDetails(Action):
def name(self):
return "action_ask_details"
def run(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: dict):
slot_value = tracker.get_slot('data')
if slot_value is None:
dispatcher.utter_message(template="utter")
return [FollowupAction('action_check_details')]
else:
dispatcher.utter_message(template="utter_thanks")
return []
class ActionSubmitForm(Action):
def name(self) -> Text:
return "action_submit_form"
def run(self, dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: Dict[Text, Any]) -> List[Dict[Text, Any]]:
try:
# Retrieve slot values
first_name = tracker.get_slot('first_name')
middle_name = tracker.get_slot('middle_name')
last_name = tracker.get_slot('last_name')
email = tracker.get_slot('email')
us_number = tracker.get_slot('us_number')
phone = tracker.get_slot('phone')
confirm_location = tracker.get_slot('confirm_location')
confirm_mail_address = tracker.get_slot('confirm_mail_address')
preferred_language = tracker.get_slot('preferred_language')
able_to_read_write = tracker.get_slot('able_to_read_write')
born_in_us = tracker.get_slot('born_in_us')
become_citizen_us = tracker.get_slot('become_citizen_us')
legal_resident_us = tracker.get_slot('legal_resident_us')
permanent_resident_number = tracker.get_slot('permanent_resident_number')
serve_military = tracker.get_slot('serve_military')
military_benefit = tracker.get_slot('military_benefit')
medicare_benefit = tracker.get_slot('medicare_benefit')
social_security_benefit = tracker.get_slot('social_security_benefit')
supplemental_security_income = tracker.get_slot('supplemental_security_income')
current_spouse_first_name = tracker.get_slot('current_spouse_first_name')
current_spouse_last_name = tracker.get_slot('current_spouse_last_name')
current_spouse_social_security_number = tracker.get_slot('current_spouse_social_security_number')
prior_marriages = tracker.get_slot('prior_marriages')
prior_marriages_duration = tracker.get_slot('prior_marriages_duration')
prior_marriages_death = tracker.get_slot('prior_marriages_death')
have_children = tracker.get_slot('have_children')
parent_support = tracker.get_slot('parent_support')
used_other_names = tracker.get_slot('used_other_names')
ssn = tracker.get_slot('ssn')
used_other_ssn = tracker.get_slot('used_other_ssn')
bank_account_for_deposit = tracker.get_slot('bank_account_for_deposit')
routing_number = tracker.get_slot('routing_number')
account_number = tracker.get_slot('account_number')
able_to_work = tracker.get_slot('able_to_work')
perform_usual_job_responsibilities = tracker.get_slot('perform_usual_job_responsibilities')
condition_last_14_months = tracker.get_slot('condition_last_14_months')
condition_end_in_death = tracker.get_slot('condition_end_in_death')
condition_require_work_changes = tracker.get_slot('condition_require_work_changes')
earned_more_than_1470_in_2024 = tracker.get_slot('earned_more_than_1470_in_2024')
intend_to_work_in_2024 = tracker.get_slot('intend_to_work_in_2024')
received_money_from_employer = tracker.get_slot('received_money_from_employer')
expect_to_receive_money_from_employer = tracker.get_slot('expect_to_receive_money_from_employer')
school_name = tracker.get_slot('school_name')
school_city = tracker.get_slot('school_city')
school_state = tracker.get_slot('school_state')
school_country = tracker.get_slot('school_country')
special_education = tracker.get_slot('special_education')
specialized_job_training = tracker.get_slot('specialized_job_training')
company_name = tracker.get_slot('company_name')
business_type = tracker.get_slot('business_type')
end_date_last_worked = tracker.get_slot('end_date_last_worked')
job_title = tracker.get_slot('job_title')
pay_amount = tracker.get_slot('pay_amount')
lifting_ability = tracker.get_slot('lifting_ability')
supervised_others = tracker.get_slot('supervised_others')
cared_for_children = tracker.get_slot('cared_for_children')
self_employed_last_15_years = tracker.get_slot('self_employed_last_15_years')
self_employed_2023 = tracker.get_slot('self_employed_2023')
profit_self_employed_2023 = tracker.get_slot('profit_self_employed_2023')
self_employed_2024 = tracker.get_slot('self_employed_2024')
profit_self_employed_2024 = tracker.get_slot('profit_self_employed_2024')
worked_outside_us = tracker.get_slot('worked_outside_us')
worked_outside_us_2024 = tracker.get_slot('worked_outside_us_2024')
illness_expected_duration = tracker.get_slot('illness_expected_duration')
worked_outside_2023 = tracker.get_slot('worked_outside_2023')
earnings_subject_us_social_security = tracker.get_slot('earnings_subject_us_social_security')
eligible_foreign_social_security = tracker.get_slot('eligible_foreign_social_security')
special_payments = tracker.get_slot('special_payments')
no_social_security_taxes = tracker.get_slot('no_social_security_taxes')
spouse_receiving_pension = tracker.get_slot('spouse_receiving_pension')
future_pension_annuity = tracker.get_slot('future_pension_annuity')
government_pension_or_annuity = tracker.get_slot('government_pension_or_annuity')
lump_sum_payment = tracker.get_slot('lump_sum_payment')
vision_impairment = tracker.get_slot('vision_impairment')
impaired_vision_with_glasses_contacts = tracker.get_slot('impaired_vision_with_glasses_contacts')
partial_or_total_deafness = tracker.get_slot('partial_or_total_deafness')
use_hearing_aids = tracker.get_slot('use_hearing_aids')
history_of_seizures = tracker.get_slot('history_of_seizures')
mental_illness_treatment = tracker.get_slot('mental_illness_treatment')
physical_illness_treatment = tracker.get_slot('physical_illness_treatment')
emergency_room_visit = tracker.get_slot('emergency_room_visit')
medical_tests_or_medications = tracker.get_slot('medical_tests_or_medications')
mental_health_treatment = tracker.get_slot('mental_health_treatment')
inpatient_stays = tracker.get_slot('inpatient_stays')
outpatient_visits = tracker.get_slot('outpatient_visits')
had_surgeries = tracker.get_slot('had_surgeries')
had_physical_therapy = tracker.get_slot('had_physical_therapy')
medical_records = tracker.get_slot('medical_records')
doctor_in_prison = tracker.get_slot('doctor_in_prison')
public_welfare = tracker.get_slot('public_welfare')
attorney_records = tracker.get_slot('attorney_records')
disability_insurance = tracker.get_slot('disability_insurance')
vocational_rehab = tracker.get_slot('vocational_rehab')
workers_compensation = tracker.get_slot('workers_compensation')
other_records = tracker.get_slot('other_records')
medical_tests_completed = tracker.get_slot('medical_tests_completed')
taking_medications = tracker.get_slot('taking_medications')
summary_message = (
f"Thank you {first_name} {middle_name} {last_name}. "
f"We have recorded your email as {email} and phone number as {phone}. "
f"US Number: {us_number}, Confirm Location: {confirm_location}, "
f"Confirm Mail Address: {confirm_mail_address}. "
f"Preferred Language: {preferred_language}, Able to Read/Write: {able_to_read_write}, "
f"Born in US: {born_in_us}, Becoming Citizen: {become_citizen_us}, "
f"Legal Resident: {legal_resident_us}, Permanent Resident Number: {permanent_resident_number}, "
f"Served in Military: {serve_military}, Military Benefit: {military_benefit}, "
f"Social Security Benefit: {social_security_benefit}, Supplemental Security Income: {supplemental_security_income}, "
)
dispatcher.utter_message(text=summary_message)
except Exception as e:
dispatcher.utter_message(text=f"An error occurred: {e}")