From 8847b5b6742509591f4085eea68595b871f63283 Mon Sep 17 00:00:00 2001 From: lanvent Date: Sun, 12 Mar 2023 13:25:23 +0800 Subject: [PATCH] create bot when first need --- bridge/bridge.py | 26 ++++++++++++++++++-------- plugins/plugin_manager.py | 2 +- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/bridge/bridge.py b/bridge/bridge.py index 81e5d73..e16d721 100644 --- a/bridge/bridge.py +++ b/bridge/bridge.py @@ -1,3 +1,4 @@ +from common.log import logger from bot import bot_factory from common.singleton import singleton from voice import voice_factory @@ -6,16 +7,24 @@ from voice import voice_factory @singleton class Bridge(object): def __init__(self): - self.bots = { - "chat": bot_factory.create_bot("chatGPT"), - "voice_to_text": voice_factory.create_voice("openai"), - # "text_to_voice": voice_factory.create_voice("baidu") + self.btype={ + "chat": "chatGPT", + "voice_to_text": "openai", + "text_to_voice": "baidu" } - try: - self.bots["text_to_voice"] = voice_factory.create_voice("baidu") - except ModuleNotFoundError as e: - print(e) + self.bots={} + def getbot(self,typename): + if self.bots.get(typename) is None: + logger.info("create bot {} for {}".format(self.btype[typename],typename)) + if typename == "text_to_voice": + self.bots[typename] = voice_factory.create_voice(self.btype[typename]) + elif typename == "voice_to_text": + self.bots[typename] = voice_factory.create_voice(self.btype[typename]) + elif typename == "chat": + self.bots[typename] = bot_factory.create_bot(self.btype[typename]) + return self.bots[typename] + # 以下所有函数需要得到一个reply字典,格式如下: # reply["type"] = "ERROR" / "TEXT" / "VOICE" / ... # reply["content"] = reply的内容 @@ -28,3 +37,4 @@ class Bridge(object): def fetch_text_to_voice(self, text): return self.bots["text_to_voice"].textToVoice(text) + diff --git a/plugins/plugin_manager.py b/plugins/plugin_manager.py index b7d88e7..bf202f8 100644 --- a/plugins/plugin_manager.py +++ b/plugins/plugin_manager.py @@ -63,7 +63,7 @@ class PluginManager: def load_plugins(self): pconf = self.load_config() - logger.debug("plugins.json config={}" % pconf) + logger.debug("plugins.json config={}".format(pconf)) for plugin in pconf["plugins"]: name = plugin["name"] enabled = plugin["enabled"]