選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

81 行
3.2KB

  1. from bot.bot_factory import create_bot
  2. from bridge.context import Context
  3. from bridge.reply import Reply
  4. from common import const
  5. from common.log import logger
  6. from common.singleton import singleton
  7. from config import conf
  8. from translate.factory import create_translator
  9. from voice.factory import create_voice
  10. @singleton
  11. class Bridge(object):
  12. def __init__(self):
  13. self.btype = {
  14. "chat": const.CHATGPT,
  15. "voice_to_text": conf().get("voice_to_text", "openai"),
  16. "text_to_voice": conf().get("text_to_voice", "google"),
  17. "translate": conf().get("translate", "baidu"),
  18. }
  19. model_type = conf().get("model") or const.GPT35
  20. if model_type in ["text-davinci-003"]:
  21. self.btype["chat"] = const.OPEN_AI
  22. if conf().get("use_azure_chatgpt", False):
  23. self.btype["chat"] = const.CHATGPTONAZURE
  24. if model_type in ["wenxin", "wenxin-4"]:
  25. self.btype["chat"] = const.BAIDU
  26. if model_type in ["xunfei"]:
  27. self.btype["chat"] = const.XUNFEI
  28. if model_type in [const.QWEN]:
  29. self.btype["chat"] = const.QWEN
  30. if conf().get("use_linkai") and conf().get("linkai_api_key"):
  31. self.btype["chat"] = const.LINKAI
  32. if not conf().get("voice_to_text") or conf().get("voice_to_text") in ["openai"]:
  33. self.btype["voice_to_text"] = const.LINKAI
  34. if not conf().get("text_to_voice") or conf().get("text_to_voice") in ["openai", const.TTS_1, const.TTS_1_HD]:
  35. self.btype["text_to_voice"] = const.LINKAI
  36. if model_type in ["claude"]:
  37. self.btype["chat"] = const.CLAUDEAI
  38. self.bots = {}
  39. self.chat_bots = {}
  40. def get_bot(self, typename):
  41. if self.bots.get(typename) is None:
  42. logger.info("create bot {} for {}".format(self.btype[typename], typename))
  43. if typename == "text_to_voice":
  44. self.bots[typename] = create_voice(self.btype[typename])
  45. elif typename == "voice_to_text":
  46. self.bots[typename] = create_voice(self.btype[typename])
  47. elif typename == "chat":
  48. self.bots[typename] = create_bot(self.btype[typename])
  49. elif typename == "translate":
  50. self.bots[typename] = create_translator(self.btype[typename])
  51. return self.bots[typename]
  52. def get_bot_type(self, typename):
  53. return self.btype[typename]
  54. def fetch_reply_content(self, query, context: Context) -> Reply:
  55. return self.get_bot("chat").reply(query, context)
  56. def fetch_voice_to_text(self, voiceFile) -> Reply:
  57. return self.get_bot("voice_to_text").voiceToText(voiceFile)
  58. def fetch_text_to_voice(self, text) -> Reply:
  59. return self.get_bot("text_to_voice").textToVoice(text)
  60. def fetch_translate(self, text, from_lang="", to_lang="en") -> Reply:
  61. return self.get_bot("translate").translate(text, from_lang, to_lang)
  62. def find_chat_bot(self, bot_type: str):
  63. if self.chat_bots.get(bot_type) is None:
  64. self.chat_bots[bot_type] = create_bot(bot_type)
  65. return self.chat_bots.get(bot_type)
  66. def reset_bot(self):
  67. """
  68. 重置bot路由
  69. """
  70. self.__init__()