You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

bridge.py 4.2KB

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