No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

90 líneas
3.6KB

  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. # 这边取配置的模型
  20. model_type = conf().get("model") or const.GPT35
  21. if model_type in ["text-davinci-003"]:
  22. self.btype["chat"] = const.OPEN_AI
  23. if conf().get("use_azure_chatgpt", False):
  24. self.btype["chat"] = const.CHATGPTONAZURE
  25. if model_type in ["wenxin", "wenxin-4"]:
  26. self.btype["chat"] = const.BAIDU
  27. if model_type in ["xunfei"]:
  28. self.btype["chat"] = const.XUNFEI
  29. if model_type in [const.QWEN]:
  30. self.btype["chat"] = const.QWEN
  31. if model_type in [const.GEMINI]:
  32. self.btype["chat"] = const.GEMINI
  33. if model_type in [const.ZHIPU_AI]:
  34. self.btype["chat"] = const.ZHIPU_AI
  35. if model_type and model_type.startswith("claude-3"):
  36. self.btype["chat"] = const.CLAUDEAPI
  37. if conf().get("use_linkai") and conf().get("linkai_api_key"):
  38. self.btype["chat"] = const.LINKAI
  39. if not conf().get("voice_to_text") or conf().get("voice_to_text") in ["openai"]:
  40. self.btype["voice_to_text"] = const.LINKAI
  41. if not conf().get("text_to_voice") or conf().get("text_to_voice") in ["openai", const.TTS_1, const.TTS_1_HD]:
  42. self.btype["text_to_voice"] = const.LINKAI
  43. if model_type in ["claude"]:
  44. self.btype["chat"] = const.CLAUDEAI
  45. self.bots = {}
  46. self.chat_bots = {}
  47. # 模型对应的接口
  48. def get_bot(self, typename):
  49. if self.bots.get(typename) is None:
  50. logger.info("create bot {} for {}".format(self.btype[typename], typename))
  51. if typename == "text_to_voice":
  52. self.bots[typename] = create_voice(self.btype[typename])
  53. elif typename == "voice_to_text":
  54. self.bots[typename] = create_voice(self.btype[typename])
  55. elif typename == "chat":
  56. self.bots[typename] = create_bot(self.btype[typename])
  57. elif typename == "translate":
  58. self.bots[typename] = create_translator(self.btype[typename])
  59. return self.bots[typename]
  60. def get_bot_type(self, typename):
  61. return self.btype[typename]
  62. def fetch_reply_content(self, query, context: Context) -> Reply:
  63. return self.get_bot("chat").reply(query, context)
  64. def fetch_voice_to_text(self, voiceFile) -> Reply:
  65. return self.get_bot("voice_to_text").voiceToText(voiceFile)
  66. def fetch_text_to_voice(self, text) -> Reply:
  67. return self.get_bot("text_to_voice").textToVoice(text)
  68. def fetch_translate(self, text, from_lang="", to_lang="en") -> Reply:
  69. return self.get_bot("translate").translate(text, from_lang, to_lang)
  70. def find_chat_bot(self, bot_type: str):
  71. if self.chat_bots.get(bot_type) is None:
  72. self.chat_bots[bot_type] = create_bot(bot_type)
  73. return self.chat_bots.get(bot_type)
  74. def reset_bot(self):
  75. """
  76. 重置bot路由
  77. """
  78. self.__init__()