-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess_message.py
51 lines (44 loc) · 1.98 KB
/
process_message.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
from rasa_nlu.model import Interpreter
from rasa_core.agent import Agent
from rasa_core.interpreter import NaturalLanguageInterpreter
from rasa_core.utils import EndpointConfig
# from rasa.utils.endpoints import EndpointConfig
import os
# list_agent={}
# list_nlu={}
#load all bot
def load_bots():
list_agent = {}
list_nlu={}
for bot_name in os.listdir("list_bot"):
interpreter = NaturalLanguageInterpreter.create('list_bot/{}/models/nlu/default/{}'.format(bot_name,bot_name))
endpoint = EndpointConfig('http://localhost:5055/webhook')
list_agent["agent_" + bot_name]=Agent.load('list_bot/{}/models/dialogue'.format(bot_name), interpreter=interpreter, action_endpoint = endpoint)
list_nlu["nlu_" + bot_name]=Interpreter.load('list_bot/{}/models/nlu/default/{}'.format(bot_name,bot_name))
print(list_agent)
print(list_nlu)
return list_agent,list_nlu
def load_bot_select(botName):
for botSelect in os.listdir("list_bot"):
if botSelect == botName:
interpreter = NaturalLanguageInterpreter.create(
'list_bot/{}/models/nlu/default/{}'.format(botName, botName))
endpoint = EndpointConfig('http://localhost:5055/webhook')
agent = Agent.load('list_bot/{}/models/dialogue'.format(botName),
interpreter=interpreter, action_endpoint=endpoint)
nlu = Interpreter.load(
'list_bot/{}/models/nlu/default/{}'.format(botName, botName))
return agent,nlu
def answer_question_user(list_agent,bot_name,text):
responses = list_agent["agent_{}".format(bot_name)].handle_text(text)
message=None
for response in responses:
message=response["text"]
return message
def extract_info(list_nlu,bot_name,text):
summary={}
info=list_nlu['nlu_{}'.format(bot_name)].parse(text)
for entities in info['entities']:
if entities['value'] is not None:
summary[entities['entity']] = entities['value']
return summary