JanetBackEnd/DM.py

85 lines
3.1 KiB
Python
Raw Permalink Normal View History

2023-03-30 15:17:54 +02:00
import time
class DM:
2023-04-17 09:45:22 +02:00
def __init__(self, max_history_length=2):
2023-04-04 05:34:47 +02:00
self.working_history_sep = ""
self.working_history_consec = ""
2023-04-16 02:44:09 +02:00
self.chitchat_history_consec = ""
2023-04-04 05:34:47 +02:00
self.max_history_length = max_history_length
self.chat_history = []
self.curr_state = None
def update_history(self):
2023-04-04 20:28:22 +02:00
to_consider = [x['modified_query'] for x in self.chat_history[-self.max_history_length*2:]]
2023-04-04 05:34:47 +02:00
self.working_history_consec = " . ".join(to_consider)
self.working_history_sep = " ||| ".join(to_consider)
2023-04-16 02:44:09 +02:00
chat = []
for utt in self.chat_history:
if utt['intent'] == 'CHITCHAT':
2023-04-17 09:45:22 +02:00
if len(chat) == 4:
2023-04-16 02:44:09 +02:00
chat = chat[1:]
chat.append(utt['modified_query'])
self.chitchat_history_consec = '. '.join(chat)
2023-04-04 05:34:47 +02:00
def get_consec_history(self):
return self.working_history_consec
2023-04-16 02:44:09 +02:00
def get_chitchat_history(self):
return self.chitchat_history_consec
2023-03-30 15:17:54 +02:00
2023-04-04 05:34:47 +02:00
def get_sep_history(self):
return self.working_history_sep
2023-03-30 15:17:54 +02:00
def get_recent_state(self):
2023-04-04 05:34:47 +02:00
return self.curr_state
2023-03-30 15:17:54 +02:00
2023-04-04 05:34:47 +02:00
def get_dialogue_history(self):
return self.chat_history
2023-03-30 15:17:54 +02:00
def update(self, new_state):
2023-04-04 05:34:47 +02:00
self.chat_history.append(new_state)
self.curr_state = new_state
self.update_history()
2023-03-30 15:17:54 +02:00
def next_action(self):
2023-04-04 05:34:47 +02:00
if self.curr_state['help']:
return "Help"
elif self.curr_state['inactive']:
return "Recommend"
elif self.curr_state['is_clear']:
if self.curr_state['is_offensive']:
return "OffenseReject"
2023-03-30 15:17:54 +02:00
else:
2023-04-04 05:34:47 +02:00
if self.curr_state['intent'] == 'QA':
2023-03-30 15:17:54 +02:00
return "RetGen"
2023-04-15 10:52:01 +02:00
if self.curr_state['intent'] == 'EXPLAINPOST':
return "findPost"
if self.curr_state['intent'] == 'HELP':
return "getHelp"
2023-04-04 05:34:47 +02:00
elif self.curr_state['intent'] == 'CHITCHAT':
2023-03-30 15:17:54 +02:00
return "ConvGen"
2023-04-04 05:34:47 +02:00
elif self.curr_state['intent'] == 'FINDPAPER':
2023-03-30 15:17:54 +02:00
return "findPaper"
2023-04-04 05:34:47 +02:00
elif self.curr_state['intent'] == 'FINDDATASET':
2023-03-30 15:17:54 +02:00
return "findDataset"
2023-04-04 05:34:47 +02:00
elif self.curr_state['intent'] == 'SUMMARIZEPAPER':
2023-03-30 15:17:54 +02:00
return "sumPaper"
2023-04-19 23:43:57 +02:00
elif self.curr_state['intent'] == 'LISTPAPERS':
return "listPapers"
elif self.curr_state['intent'] == 'LISTDATASETS':
return "listDatasets"
elif self.curr_state['intent'] == 'LISTCOMMANDS':
return "listCommands"
elif self.curr_state['intent'] == 'LISTTOPICS':
return "listTopics"
elif self.curr_state['intent'] == 'LISTRESOURCES':
return "listResources"
elif self.curr_state['intent'] == 'COMMAND':
return "command"
2023-04-10 01:34:58 +02:00
else:
2023-04-19 23:43:57 +02:00
return "RetGen"
2023-03-30 15:17:54 +02:00
else:
2023-04-09 22:43:13 +02:00
return "Clarify"