class ChatServicer(chatbot_pb2_grpc.ChatBotServiceServicer): def __init__(self): # 提前加载各种模型 self.classify = Classify() self.recall = Recall() self.sort = DnnSort() self.chatbot = Chatbot() self.message_manager = MessageManager() def Chatbot(self, request, context): user_id = request.user_id message = request.user_message create_time = request.create_time attention, prob = self.classify.predict(message) if attention == "QA": # 实现对对话数据的保存 self.message_manager.user_message_pipeline( user_id, message, create_time, attention, entity=message["entity"]) recall_list, entity = self.recall.predict(message) user_response = self.sort.predict(message, recall_list) else: # 实现对对话数据的保存 self.message_manager.user_message_pipeline( user_id, message, create_time, attention, entity=message["entity"]) user_response = self.chatbot.predict(message) self.message_manager.bot_message_pipeline(user_id, user_response) create_time = int(time.time()) return chatbot_pb2.ResponsedMessage(user_response=user_response, create_time=create_time)
ws.build_vocab() print(len(ws)) pickle.dump(ws, open(config.chatbot_ws_by_word_input_path, "wb")) ws = Word_sequence() for line in open(config.chatbot_target_path, "r", encoding="utf-8").readlines(): ws.fit(line.strip().split()) ws.build_vocab() print(len(ws)) pickle.dump(ws, open(config.chatbot_ws_by_word_target_path, "wb")) def train_seq2seq(): for i in range(10): train(i) if __name__ == '__main__': # save_ws() # for idx,(input,target,input_length,target_length) in enumerate(dataloader): # print(input) # print(target) # print(input_length) # print(target_length) # break # train_seq2seq() chatbot = Chatbot() while True: print(chatbot.predict(input("请输入:")))