-
Notifications
You must be signed in to change notification settings - Fork 45
/
bot.py
174 lines (134 loc) · 5.47 KB
/
bot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import argparse
import logging
import warnings
from policy.mobile_policy import MobilePolicy
from policy.attention_policy import AttentionPolicy
from rasa_core import utils
from rasa_core.agent import Agent
from rasa_core.policies.memoization import MemoizationPolicy
from rasa_core.policies.fallback import FallbackPolicy
from rasa_core.policies.form_policy import FormPolicy
from rasa_core.policies.embedding_policy import EmbeddingPolicy
from rasa_core import config
logger = logging.getLogger(__name__)
def train_dialogue_keras(domain_file="mobile_domain.yml",
model_path="models/dialogue_keras",
training_data_file="data/mobile_edit_story.md"):
fallback = FallbackPolicy(
fallback_action_name="action_unknown_intent",
nlu_threshold=0.7,
core_threshold=0.3
)
agent = Agent(domain_file,
policies=[MemoizationPolicy(max_history=8),
MobilePolicy(epochs=100, batch_size=16, max_history=8),
FormPolicy(),
fallback])
training_data = agent.load_data(training_data_file)
agent.train(
training_data,
validation_split=0.2
)
agent.persist(model_path)
return agent
def train_dialogue_embed(domain_file="mobile_domain.yml",
model_path="models/dialogue_embed",
training_data_file="data/mobile_edit_story.md"):
fallback = FallbackPolicy(
fallback_action_name="action_default_fallback",
nlu_threshold=0.7,
core_threshold=0.3
)
agent = Agent(domain_file,
policies=[MemoizationPolicy(max_history=5),
EmbeddingPolicy(epochs=100), fallback])
training_data = agent.load_data(training_data_file)
agent.train(
training_data,
validation_split=0.2
)
agent.persist(model_path)
return agent
def train_dialogue_transformer(domain_file="mobile_domain.yml",
model_path="models/dialogue_transformer",
training_data_file="data/mobile_edit_story.md"):
# 通过加载yml配置文件方式配置policy
policies = config.load('./policy/attention_policy.yml')
agent = Agent(domain_file,
policies=policies)
training_data = agent.load_data(training_data_file)
agent.train(
training_data,
validation_split=0.2
)
agent.persist(model_path)
return agent
def train_nlu():
from rasa_nlu.training_data import load_data
from rasa_nlu import config
from rasa_nlu.model import Trainer
training_data = load_data('data/rasa_dataset_training.json')
trainer = Trainer(config.load("configs/nlu_embedding_config.yml"))
trainer.train(training_data)
model_directory = trainer.persist('models/nlu/',
fixed_model_name="current")
return model_directory
def train_nlu_gao():
from rasa_nlu_gao.training_data import load_data
from rasa_nlu_gao import config
from rasa_nlu_gao.model import Trainer
training_data = load_data('data/rasa_dataset_training.json')
trainer = Trainer(config.load("configs/config_embedding_bilstm.yml"))
trainer.train(training_data)
model_directory = trainer.persist('models/nlu_gao/',
fixed_model_name="current")
return model_directory
def train_nlu_elmo():
from rasa_nlu_gao.training_data import load_data
from rasa_nlu_gao import config
from rasa_nlu_gao.model import Trainer
training_data = load_data('data/rasa_dataset_training.json')
trainer = Trainer(config.load("configs/elmo_model_config.yml"))
trainer.train(training_data)
model_directory = trainer.persist('models/elmo/',
fixed_model_name="current")
return model_directory
def train_nlu_wordvector():
from rasa_nlu_gao.training_data import load_data
from rasa_nlu_gao import config
from rasa_nlu_gao.model import Trainer
training_data = load_data('data/rasa_dataset_training.json')
trainer = Trainer(config.load("configs/wordvector_config.yml"))
trainer.train(training_data)
model_directory = trainer.persist('models/wordvector/',
fixed_model_name="current")
return model_directory
if __name__ == '__main__':
utils.configure_colored_logging(loglevel="INFO")
parser = argparse.ArgumentParser(
description='starts the bot')
parser.add_argument(
'task',
choices=["train-nlu", "train-dialogue-keras", "train-dialogue-embed", "train-nlu-gao", "train-nlu-elmo",
"train-dialogue-transformer", "train-nlu-wordvector"],
help="what the bot should do ?")
task = parser.parse_args().task
# decide what to do based on first parameter of the script
if task == "train-nlu":
train_nlu()
elif task == "train-nlu-gao":
train_nlu_gao()
elif task == "train-nlu-elmo":
train_nlu_elmo()
elif task == "train-nlu-wordvector":
train_nlu_wordvector()
elif task == "train-dialogue-keras":
train_dialogue_keras()
elif task == "train-dialogue-embed":
train_dialogue_embed()
elif task == "train-dialogue-transformer":
train_dialogue_transformer()