def __init__(self, cache_path='cache.json'): self.bot = Bot() self.cache = {} self.cache_path = cache_path if os.path.exists(cache_path): self.cache = load_json(cache_path) logger.info("use cache, cache file: %s" % cache_path)
class BotServer: def __init__(self, cache_path='cache.json'): self.bot = Bot() self.cache = {} self.cache_path = cache_path if os.path.exists(cache_path): self.cache = load_json(cache_path) logger.info("use cache, cache file: %s" % cache_path) def answer(self, query): if query in self.cache: response = self.cache[query] else: response = self.bot.answer(query) self.cache[query] = response if self.cache_path: save_json(self.cache, self.cache_path) logger.info("save cache.") return response
# -*- coding: utf-8 -*- """ @author:XuMing([email protected]) @description: """ import sys sys.path.append('..') from dialogbot import Bot bot = Bot() msgs = [ '北京天气?', '上海的呢?', '明天晚上能发出来吗?', '有5元的东西吗? 哪种口味好吃', '这个金额是否达到包邮条件', '好的谢谢哦。', '好的谢了', '姚明多高?', '长城哪一年开始修的?', ] for msg in msgs: response = bot.answer(msg, use_task=True) print("query:", msg) print("response:", response)
# -*- coding: utf-8 -*- """ @author:XuMing([email protected]) @description: """ import sys sys.path.append('..') from dialogbot import Bot bot = Bot() if __name__ == "__main__": print(bot.answer('这车怎么卖?'))
# -*- coding: utf-8 -*- """ @author:XuMing([email protected]) @description: """ import sys sys.path.append('..') from dialogbot import Bot bot = Bot() def start_dialog(): print("\nChatbot: %s\n" % "您好,我是可爱的对话机器人小智,有问题都可以向我提问哦~") print("input1: ", end="") while True: msg = input().strip() if msg.lower() == "finish": print("Chatbot: %s\n\n" % "change session", end="") print("input1: ", end="") bot.set_context([]) elif msg.lower() == "exit": print("Chatbot: %s\n\n" % "感谢您的支持,我们下次再见呢~, 拜拜亲爱哒") exit() else: bot.context.append(msg) response = bot.answer(msg, use_task=True) print("output%d: %s\n\n" % (len(bot.context) / 2, response), end="")
# -*- coding: utf-8 -*- """ @author:XuMing([email protected]) @description: """ import sys import torch sys.path.append('..') from dialogbot import Bot if __name__ == '__main__': bot = Bot() response = bot.answer('姚明多高?') print(response)
# -*- coding: utf-8 -*- """ @author:XuMing([email protected]) @description: test seq2seq model """ import sys sys.path.append('..') from dialogbot import GPTBot from dialogbot import Bot if __name__ == '__main__': bot = Bot() response = bot.answer('亲 你吃了吗?', use_gen=True, use_search=False, use_task=False) print(response) # 具体的指定GPT2生成对话模型 bot = GPTBot() msgs = [ '你吃了吗?', '明天晚上能发出来吗?', '有5元 的 东西 吗? 哪种口味好吃', '这个金额是否达到包邮条件?', '好的谢谢哦。', '好的谢了', '你好苹果怎么卖?' ] for msg in msgs: r = bot.answer(msg) print('gpt2', msg, r)
# -*- coding: utf-8 -*- """ @author:XuMing([email protected]) @description: """ import sys sys.path.append('..') from dialogbot import Bot if __name__ == '__main__': bot = Bot() msgs = [ '亲 吃了吗', '雅阁车二手卖多少钱?', '北京天气?', '上海的天气呢', '明天晚上能发出来吗?', '有5元的东西吗? 哪种口味好吃', '这个金额是否达到包邮条件', '好的谢谢哦。', '姚明多高?', '长城哪一年开始修的?', ] for msg in msgs: print("query:", msg) response = bot.answer(msg) print("response:", response)