Commit 433ed50f authored by IT19965864's avatar IT19965864

rasa bot

parent e4377533
from typing import Text, List, Any, Dict
from rasa_sdk import Tracker, FormValidationAction, Action
from rasa_sdk.events import EventType
from rasa_sdk.executor import CollectingDispatcher
from rasa_sdk.types import DomainDict
ALLOWED_PIZZA_SIZES = ["small", "medium", "large", "extra-large", "extra large", "s", "m", "l", "xl"]
ALLOWED_USER_AGES = ["10", "11", "12", "13","14","15","16","17"]
ALLOWED_MENARCH_AGE = ["10", "11", "12", "13"]
ALLOWED_PIZZA_TYPES = ["mozzarella", "fungi", "veggie", "pepperoni", "hawaii"]
VEGETARIAN_PIZZAS = ["mozzarella", "fungi", "veggie"]
MEAT_PIZZAS = ["pepperoni", "hawaii"]
class ValidateSimplePizzaForm(FormValidationAction):
def name(self) -> Text:
return "validate_simple_pizza_form"
def validate_user_age(
self,
slot_value: Any,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: DomainDict,
) -> Dict[Text, Any]:
"""Validate `user_age` value."""
if slot_value.lower() not in ALLOWED_USER_AGES:
dispatcher.utter_message(text=f"Only accept age 1-120")
return {"user_age": None}
dispatcher.utter_message(text=f"OK! Your age is {slot_value}")
return {"user_age": slot_value}
def validate_menarche_age(
self,
slot_value: Any,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: DomainDict,
) -> Dict[Text, Any]:
"""Validate `menarche_age` value."""
if slot_value.lower() not in ALLOWED_MENARCH_AGE:
dispatcher.utter_message(text=f"Only accept age 1-120")
return {"menarche_age": None}
dispatcher.utter_message(text=f"OK! your menarche age is {slot_value}.")
return {"menarche_age": slot_value}
def validate_pizza_size_chata(
self,
slot_value: Any,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: DomainDict,
) -> Dict[Text, Any]:
"""Validate `pizza_size_chata` value."""
if slot_value.lower() not in ALLOWED_PIZZA_SIZES:
dispatcher.utter_message(text=f"We only accept pizza sizes: s/m/l/xl.")
return {"pizza_size_chata": None}
dispatcher.utter_message(text=f"OK! You want to have a {slot_value} pizza.")
return {"pizza_size_chata": slot_value}
def validate_pizza_type(
self,
slot_value: Any,
dispatcher: CollectingDispatcher,
tracker: Tracker,
domain: DomainDict,
) -> Dict[Text, Any]:
"""Validate `pizza_type` value."""
if slot_value not in ALLOWED_PIZZA_TYPES:
dispatcher.utter_message(text=f"I don't recognize that pizza. We serve {'/'.join(ALLOWED_PIZZA_TYPES)}.")
return {"pizza_type": None}
dispatcher.utter_message(text=f"OK! You want to have a {slot_value} pizza.")
return {"pizza_type": slot_value}
class AskForVegetarianAction(Action):
def name(self) -> Text:
return "action_ask_vegetarian"
def run(
self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
) -> List[EventType]:
dispatcher.utter_message(text="Would you like to order a vegetarian pizza?",
buttons=[
{"title": "yes", "payload": "/affirm"},
{"title": "no", "payload": "/deny"}
])
return []
class AskForPizzaTypeAction(Action):
def name(self) -> Text:
return "action_ask_pizza_type"
def run(
self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict
) -> List[EventType]:
if tracker.get_slot("vegetarian"):
dispatcher.utter_message(text=f"What kind of pizza do you want?",
buttons=[{"title": p, "payload": p} for p in VEGETARIAN_PIZZAS])
else:
dispatcher.utter_message(text=f"What kind of pizza do you want?",
buttons=[{"title": p, "payload": p} for p in MEAT_PIZZAS])
return []
\ No newline at end of file
language: en
pipeline:
- name: WhitespaceTokenizer
- name: RegexFeaturizer
- name: LexicalSyntacticFeaturizer
- name: CountVectorsFeaturizer
- name: CountVectorsFeaturizer
analyzer: char_wb
min_ngram: 2
max_ngram: 4
- name: DIETClassifier
epochs: 100
constrain_similarities: true
model_confidence: linear_norm
policies:
- name: MemoizationPolicy
- name: TEDPolicy
max_history: 5
epochs: 50
constrain_similarities: true
- name: RulePolicy
# This file contains the credentials for the voice & chat platforms
# which your bot is using.
# https://rasa.com/docs/rasa/messaging-and-voice-channels
rest:
# # you don't need to provide anything here - this channel doesn't
# # require any credentials
#facebook:
# verify: "<verify>"
# secret: "<your secret>"
# page-access-token: "<your page access token>"
#slack:
# slack_token: "<your slack token>"
# slack_channel: "<the slack channel>"
# slack_signing_secret: "<your slack signing secret>"
#socketio:
# user_message_evt: <event name for user message>
# bot_message_evt: <event name for bot messages>
# session_persistence: <true/false>
#mattermost:
# url: "https://<mattermost instance>/api/v4"
# token: "<bot token>"
# webhook_url: "<callback URL>"
# This entry is needed if you are using Rasa X. The entry represents credentials
# for the Rasa X "channel", i.e. Talk to your bot and Share with guest testers.
rasa:
url: "http://localhost:5002/api"
version: "2.0"
nlu:
- intent: greet
examples: |
- hey
- hello
- hi
- hello there
- good morning
- good evening
- moin
- hey there
- let's go
- hey dude
- goodmorning
- goodevening
- good afternoon
- yo
- hello world
- hi there
- intent: goodbye
examples: |
- cu
- good by
- cee you later
- good night
- bye
- goodbye
- have a nice day
- see you around
- bye bye
- see you later
- thanks, goodbye
- intent: affirm
examples: |
- yes
- y
- indeed
- of course
- that sounds good
- correct
- /affirm
- intent: deny
examples: |
- no
- n
- never
- I don't think so
- don't like that
- no way
- not really
- intent: mood_great
examples: |
- perfect
- great
- amazing
- feeling like a king
- wonderful
- I am feeling very good
- I am great
- I am amazing
- I am going to save the world
- super stoked
- extremely good
- so so perfect
- so good
- so perfect
- intent: mood_unhappy
examples: |
- my day was horrible
- I am sad
- I don't feel very well
- I am disappointed
- super sad
- I'm so sad
- sad
- very sad
- unhappy
- not good
- not very good
- extremly sad
- so saad
- so sad
- intent: stop
examples: |
- stop
- nevermind
- please stop
- i want to stop
- stop the form
- intent: bot_challenge
examples: |
- are you a bot?
- are you a human?
- am I talking to a bot?
- am I talking to a human?
- by the way, are you a human?
- intent: buy_fancy_pizza
examples: |
- i'd like to buy a fancy pizza
- i want a fancy pizza
- can i buy a fancy pizza
- I'm interested in a fancy flattened bread of Italian origin
- buy a fancy pizza
- intent: inform
examples: |
- I can co operate with you
- I can co operate.
- I would like to co operate with you
- yes, I would
- I would like to give my personal data
- I would like
- I can give you my personal data
- I like to give my personal data
- It is okay, I can give my personal data
- I will give you my personal data
- I like to give my data
- intent: inform_menarche_age
examples: |
- When I was [10](menarche_age) years
- When I was [11](menarche_age) years
- When I was [12](menarche_age) years
- When I was [13](menarche_age) years
- When I was [14](menarche_age) years
- When I was [15](menarche_age) years
- When I was [16](menarche_age) years
- When I was [17](menarche_age) years
- When I was [18](menarche_age) years
- When I was [19](menarche_age) years
- When I was [20](menarche_age) years
- When I was [21](menarche_age) years
- When I was [22](menarche_age) years
- When I was [23](menarche_age) years
- When I was [24](menarche_age) years
- When I was [25](menarche_age) years
- intent: inform_age
examples: |
- I am [10](user_age) years
- I am [11](user_age) years
- I am [12](user_age) years
- I am [13](user_age) years
- I am [14](user_age) years
- I am [15](user_age) years
- I am [16](user_age) years
- I am [17](user_age) years
- I am [18](user_age) years
- I am [19](user_age) years
- I am [20](user_age) years
- I am [21](user_age) years
- I am [22](user_age) years
- I am [23](user_age) years
- I am [24](user_age) years
- I am [25](user_age) years
- intent: inform_chata
examples: |
- i'd like a [large](pizza_size_chata) pizza
- i want to order a [xl](pizza_size_chata) [hawai](pizza_type) pizza
- [medium](pizza_size_chata) pizza
- [xl](pizza_size_chata)
- [small](pizza_size_chata)
- [s](pizza_size_chata)
- [pepperoni](pizza_size_chata)
- give me a [mozerella](pizza_size_chata) pizza
- [hawaii](pizza_size_chata) pizza
- [smol](pizza_size_chata)
- [hawaii](pizza_size_chata)
- i want a [large](pizza_size_chata) pizza
- [mozzarella](pizza_size_chata)
- [hawai](pizza_size_chata)
- [veggie](pizza_size_chata)
- [fungi](pizza_size_chata)
- [vegggie](pizza_size_chata)
- i want to buy a [large](pizza_size_chata) pizza
version: "2.0"
rules:
- rule: Say goodbye anytime the user says goodbye
steps:
- intent: goodbye
- action: utter_goodbye
- rule: Say 'I am a bot' anytime the user challenges
steps:
- intent: bot_challenge
- action: utter_iamabot
- rule: Activate Pizza Form
steps:
- intent: inform
- action: simple_pizza_form
- active_loop: simple_pizza_form
- rule: Submit Pizza Form
condition:
- active_loop: simple_pizza_form
steps:
- action: simple_pizza_form
- active_loop: null
- slot_was_set:
- requested_slot: null
- action: utter_submit
- action: utter_pizza_slots
- rule: Interruption in Pizza Form
condition:
# Condition that form is active.
- active_loop: simple_pizza_form
steps:
# This unhappy path handles the case of an intent `bot_challenge`.
- intent: bot_challenge
- action: utter_iamabot
# Return to form after handling the `bot_challenge` intent
- action: simple_pizza_form
- active_loop: simple_pizza_form
version: "2.0"
stories:
- story: happy path
steps:
- intent: greet
- action: utter_greet
- intent: mood_great
- action: utter_happy
- story: sad path 1
steps:
- intent: greet
- action: utter_greet
- intent: mood_unhappy
- action: utter_cheer_up
- action: utter_did_that_help
- intent: affirm
- action: utter_happy
- story: sad path 2
steps:
- intent: greet
- action: utter_greet
- intent: mood_unhappy
- action: utter_cheer_up
- action: utter_did_that_help
- intent: deny
- action: utter_goodbye
- story: interactive_story_1
steps:
- intent: greet
- action: utter_greet
- intent: inform
- action: simple_pizza_form
- active_loop: simple_pizza_form
- slot_was_set:
- requested_slot: user_age
- slot_was_set:
- user_age: 10
- slot_was_set:
- user_age: 10
- slot_was_set:
- requested_slot: menarche_age
- slot_was_set:
- menarche_age: 12
- slot_was_set:
- menarche_age: 12
- slot_was_set:
- requested_slot: pizza_size_chata
- slot_was_set:
- pizza_size_chata: large
- slot_was_set:
- pizza_size_chata: large
- slot_was_set:
- requested_slot: pizza_type
- slot_was_set:
- pizza_type: hawaiii
- slot_was_set:
- pizza_type: null
- slot_was_set:
- requested_slot: pizza_type
- slot_was_set:
- pizza_type: hawai
- slot_was_set:
- pizza_type: null
- slot_was_set:
- requested_slot: pizza_type
- slot_was_set:
- pizza_type: hawaii
- slot_was_set:
- pizza_type: hawaii
- slot_was_set:
- requested_slot: null
- active_loop: null
- action: utter_submit
- action: utter_pizza_slots
- intent: goodbye
- action: utter_goodbye
- story: User interrupts the form and doesn't want to continue
steps:
- intent: greet
- action: utter_greet
- intent: inform
- action: simple_pizza_form
- active_loop: simple_pizza_form
- intent: stop
- action: utter_ask_continue
- or:
- intent: stop
- intent: affirm
- action: action_deactivate_loop
- active_loop: null
- story: interactive_story_2
steps:
- intent: greet
- action: utter_greet
- intent: inform
- action: simple_pizza_form
- active_loop: simple_pizza_form
- slot_was_set:
- requested_slot: menarche_age
- intent: stop
- action: utter_ask_continue
- intent: stop
- action: action_deactivate_loop
- active_loop: null
- slot_was_set:
- requested_slot: null
- intent: goodbye
- action: utter_goodbye
- story: interactive_story_3
steps:
- intent: greet
- action: utter_greet
- intent: inform
- action: simple_pizza_form
- active_loop: simple_pizza_form
- slot_was_set:
- requested_slot: menarche_age
- slot_was_set:
- menarche_age: 12
- slot_was_set:
- requested_slot: pizza_type
- intent: stop
- action: utter_ask_continue
- intent: affirm
- action: action_deactivate_loop
- active_loop: null
- slot_was_set:
- requested_slot: null
- intent: goodbye
- action: utter_goodbye
- story: interactive_story_4
steps:
- intent: greet
- action: utter_greet
- intent: inform
- action: simple_pizza_form
- active_loop: simple_pizza_form
- slot_was_set:
- requested_slot: menarche_age
- intent: stop
- action: utter_ask_continue
- intent: affirm
- action: action_deactivate_loop
- active_loop: null
- slot_was_set:
- requested_slot: null
- intent: inform
- action: simple_pizza_form
- active_loop: simple_pizza_form
- slot_was_set:
- requested_slot: menarche_age
- slot_was_set:
- menarche_age: 12
- slot_was_set:
- menarche_age: 12
- slot_was_set:
- requested_slot: pizza_type
- slot_was_set:
- pizza_type: pepperoni
- slot_was_set:
- pizza_type: pepperoni
- slot_was_set:
- requested_slot: null
- active_loop: null
- action: utter_submit
- action: utter_pizza_slots
- story: interactive_story_5
steps:
- intent: greet
- action: utter_greet
- intent: inform
- action: simple_pizza_form
- active_loop: simple_pizza_form
- slot_was_set:
- requested_slot: menarche_age
- intent: bot_challenge
- action: utter_iamabot
- action: simple_pizza_form
- slot_was_set:
- requested_slot: menarche_age
- intent: stop
- action: utter_ask_continue
- intent: affirm
- action: action_deactivate_loop
- active_loop: null
- slot_was_set:
- requested_slot: null
- intent: goodbye
- action: utter_goodbye
version: '2.0'
config:
store_entities_as_slots: true
session_config:
session_expiration_time: 60
carry_over_slots_to_new_session: true
intents:
- goodbye:
use_entities: true
- affirm:
use_entities: true
- greet:
use_entities: true
- inform:
use_entities: true
- stop:
use_entities: true
- bot_challenge:
use_entities: true
- inform_menarche_age:
use_entities: true
- inform_age:
use_entities: true
- inform_chata:
use_entities: true
- buy_fancy_pizza:
use_entities: true
- deny:
use_entities: true
- mood_great:
use_entities: true
- mood_unhappy:
use_entities: true
- request_fancy_form:
use_entities: true
- request_pizza_form:
use_entities: true
entities:
- menarche_age
- user_age
- pizza_size_chata
- pizza_type
- vegetarian
slots:
menarche_age:
type: rasa.shared.core.slots.TextSlot
initial_value: null
auto_fill: true
influence_conversation: true
user_age:
type: rasa.shared.core.slots.TextSlot
initial_value: null
auto_fill: true
influence_conversation: true
pizza_size_chata:
type: rasa.shared.core.slots.TextSlot
initial_value: null
auto_fill: true
influence_conversation: true
pizza_type:
type: rasa.shared.core.slots.TextSlot
initial_value: null
auto_fill: true
influence_conversation: true
vegetarian:
type: rasa.shared.core.slots.BooleanSlot
initial_value: null
auto_fill: true
influence_conversation: true
requested_slot:
type: rasa.shared.core.slots.UnfeaturizedSlot
initial_value: null
auto_fill: true
influence_conversation: false
responses:
utter_greet:
- text: "Hello \nThanks for you interest in hope chat. \nCan I help you with further information about breast cancer?\n Whould you like to co-operate with me?"
utter_cheer_up:
- image: https://i.imgur.com/nGF1K8f.jpg
text: 'Here is something to cheer you up:'
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.
utter_submit:
- text: I will now order a pizza for you!
utter_pizza_slots:
- text: I will order a {menarche_age} {pizza_type} pizza.
utter_ask_continue:
- text: Are you sure you want to stop the form?
utter_ask_user_age:
- text: What is your age
utter_ask_menarche_age:
- text: Tell me your menarche age
utter_ask_pizza_size_chata:
- text: What size would you like your pizza to be? pizza chata ?
utter_ask_pizza_type:
- text: What kind of pizza would you like to buy?
actions:
- action_ask_vegetarian
- action_ask_pizza_type
- utter_ask_continue
- utter_goodbye
- utter_greet
- utter_iamabot
- utter_pizza_slots
- utter_submit
- validate_simple_pizza_form
forms:
simple_pizza_form:
required_slots:
user_age:
- entity: user_age
type: from_entity
menarche_age:
- entity: menarche_age
type: from_entity
pizza_size_chata:
- entity: pizza_size_chata
type: from_entity
pizza_type:
- entity: pizza_type
type: from_entity
e2e_actions: []
# This file contains the different endpoints your bot can use.
# Server where the models are pulled from.
# https://rasa.com/docs/rasa/model-storage#fetching-models-from-a-server
#models:
# url: http://my-server.com/models/default_core@latest
# wait_time_between_pulls: 10 # [optional](default: 100)
# Server which runs your custom actions.
# https://rasa.com/docs/rasa/custom-actions
action_endpoint:
url: "http://localhost:5055/webhook"
# Tracker store which is used to store the conversations.
# By default the conversations are stored in memory.
# https://rasa.com/docs/rasa/tracker-stores
#tracker_store:
# type: redis
# url: <host of the redis instance, e.g. localhost>
# port: <port of your redis instance, usually 6379>
# db: <number of your database within redis, e.g. 0>
# password: <password used for authentication>
# use_ssl: <whether or not the communication is encrypted, default false>
#tracker_store:
# type: mongod
# url: <url to your mongo instance, e.g. mongodb://localhost:27017>
# db: <name of the db within your mongo instance, e.g. rasa>
# username: <username used for authentication>
# password: <password used for authentication>
# Event broker which all conversation events should be streamed to.
# https://rasa.com/docs/rasa/event-brokers
#event_broker:
# url: localhost
# username: username
# password: password
# queue: queue
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>TEST bot</title>
</head>
<body>
<script>
!(function () {
let e = document.createElement("script"),
t = document.head || document.getElementsByTagName("head")[0];
(e.src =
"https://cdn.jsdelivr.net/npm/rasa-webchat@1.x.x/lib/index.js"),
// Replace 1.x.x with the version that you want
(e.async = !0),
(e.onload = () => {
window.WebChat.default(
{
customData: { language: "en" },
socketUrl: "http://13.212.231.80:5009",
// add other props here
},
null
);
}),
t.insertBefore(e, t.firstChild);
})();
</script>
<!-- <div id="rasa-chat-widget" data-websocket-url="https://your-rasa-url-here/"></div>
<script src="https://unpkg.com/@rasahq/rasa-chat" type="application/javascript"></script> -->
</body>
</html>
#### This file contains tests to evaluate that your bot behaves as expected.
#### If you want to learn more, please see the docs: https://rasa.com/docs/rasa/testing-your-assistant
stories:
- story: happy path 1
steps:
- user: |
hello there!
intent: greet
- action: utter_greet
- user: |
amazing
intent: mood_great
- action: utter_happy
- story: happy path 2
steps:
- user: |
hello there!
intent: greet
- action: utter_greet
- user: |
amazing
intent: mood_great
- action: utter_happy
- user: |
bye-bye!
intent: goodbye
- action: utter_goodbye
- story: sad path 1
steps:
- user: |
hello
intent: greet
- action: utter_greet
- user: |
not good
intent: mood_unhappy
- action: utter_cheer_up
- action: utter_did_that_help
- user: |
yes
intent: affirm
- action: utter_happy
- story: sad path 2
steps:
- user: |
hello
intent: greet
- action: utter_greet
- user: |
not good
intent: mood_unhappy
- action: utter_cheer_up
- action: utter_did_that_help
- user: |
not really
intent: deny
- action: utter_goodbye
- story: sad path 3
steps:
- user: |
hi
intent: greet
- action: utter_greet
- user: |
very terrible
intent: mood_unhappy
- action: utter_cheer_up
- action: utter_did_that_help
- user: |
no
intent: deny
- action: utter_goodbye
- story: say goodbye
steps:
- user: |
bye-bye!
intent: goodbye
- action: utter_goodbye
- story: bot challenge
steps:
- user: |
are you a bot?
intent: bot_challenge
- action: utter_iamabot
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment