|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556 |
- from bot.bot_factory import create_bot
- from bridge.context import Context
- from bridge.reply import Reply
- from common import const
- from common.log import logger
- from common.singleton import singleton
- from config import conf
- from translate.factory import create_translator
- from voice.factory import create_voice
-
-
- @singleton
- class Bridge(object):
- def __init__(self):
- self.btype = {
- "chat": const.CHATGPT,
- "voice_to_text": conf().get("voice_to_text", "openai"),
- "text_to_voice": conf().get("text_to_voice", "google"),
- "translate": conf().get("translate", "baidu"),
- }
- model_type = conf().get("model")
- if model_type in ["text-davinci-003"]:
- self.btype["chat"] = const.OPEN_AI
- if conf().get("use_azure_chatgpt", False):
- self.btype["chat"] = const.CHATGPTONAZURE
- if conf().get("linkai_api_key") and conf().get("linkai_app_code"):
- self.btype["chat"] = const.LINKAI
- self.bots = {}
-
- def get_bot(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] = create_voice(self.btype[typename])
- elif typename == "voice_to_text":
- self.bots[typename] = create_voice(self.btype[typename])
- elif typename == "chat":
- self.bots[typename] = create_bot(self.btype[typename])
- elif typename == "translate":
- self.bots[typename] = create_translator(self.btype[typename])
- return self.bots[typename]
-
- def get_bot_type(self, typename):
- return self.btype[typename]
-
- def fetch_reply_content(self, query, context: Context) -> Reply:
- return self.get_bot("chat").reply(query, context)
-
- def fetch_voice_to_text(self, voiceFile) -> Reply:
- return self.get_bot("voice_to_text").voiceToText(voiceFile)
-
- def fetch_text_to_voice(self, text) -> Reply:
- return self.get_bot("text_to_voice").textToVoice(text)
-
- def fetch_translate(self, text, from_lang="", to_lang="en") -> Reply:
- return self.get_bot("translate").translate(text, from_lang, to_lang)
|