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.

126 lines
5.1KB

  1. import json
  2. import os
  3. from chatgpt_tool_hub.apps import load_app
  4. from chatgpt_tool_hub.apps.app import App
  5. import plugins
  6. from bridge.bridge import Bridge
  7. from bridge.context import ContextType
  8. from bridge.reply import Reply, ReplyType
  9. from common import const
  10. from common.log import logger
  11. from config import conf
  12. from plugins import *
  13. @plugins.register(name="tool", desc="Arming your ChatGPT bot with various tools", version="0.3", author="goldfishh", desire_priority=0)
  14. class Tool(Plugin):
  15. def __init__(self):
  16. super().__init__()
  17. self.handlers[Event.ON_HANDLE_CONTEXT] = self.on_handle_context
  18. os.environ["OPENAI_API_KEY"] = conf().get("open_ai_api_key", "")
  19. os.environ["PROXY"] = conf().get("proxy", "")
  20. self.app = self._reset_app()
  21. logger.info("[tool] inited")
  22. def get_help_text(self, verbose=False, **kwargs):
  23. help_text = "这是一个能让chatgpt联网,搜索,数字运算的插件,将赋予强大且丰富的扩展能力。"
  24. if not verbose:
  25. return help_text
  26. trigger_prefix = conf().get('plugin_trigger_prefix', "$")
  27. help_text += "使用说明:\n"
  28. help_text += f"{trigger_prefix}tool "+"{命令}: 根据给出的命令使用一些可用工具尽力为你得到结果。\n"
  29. help_text += f"{trigger_prefix}tool reset: 重置工具。\n"
  30. return help_text
  31. def on_handle_context(self, e_context: EventContext):
  32. if e_context['context'].type != ContextType.TEXT:
  33. return
  34. # 暂时不支持未来扩展的bot
  35. if Bridge().get_bot_type("chat") not in (const.CHATGPT, const.OPEN_AI, const.CHATGPTONAZURE):
  36. return
  37. content = e_context['context'].content
  38. content_list = e_context['context'].content.split(maxsplit=1)
  39. if not content or len(content_list) < 1:
  40. e_context.action = EventAction.CONTINUE
  41. return
  42. logger.debug("[tool] on_handle_context. content: %s" % content)
  43. reply = Reply()
  44. reply.type = ReplyType.TEXT
  45. trigger_prefix = conf().get('plugin_trigger_prefix', "$")
  46. # todo: 有些工具必须要api-key,需要修改config文件,所以这里没有实现query增删tool的功能
  47. if content.startswith(f"{trigger_prefix}tool"):
  48. if len(content_list) == 1:
  49. logger.debug("[tool]: get help")
  50. reply.content = self.get_help_text()
  51. e_context['reply'] = reply
  52. e_context.action = EventAction.BREAK_PASS
  53. return
  54. elif len(content_list) > 1:
  55. if content_list[1].strip() == "reset":
  56. logger.debug("[tool]: reset config")
  57. self.app = self._reset_app()
  58. reply.content = "重置工具成功"
  59. e_context['reply'] = reply
  60. e_context.action = EventAction.BREAK_PASS
  61. return
  62. elif content_list[1].startswith("reset"):
  63. logger.debug("[tool]: remind")
  64. e_context['context'].content = "请你随机用一种聊天风格,提醒用户:如果想重置tool插件,reset之后不要加任何字符"
  65. e_context.action = EventAction.BREAK
  66. return
  67. query = content_list[1].strip()
  68. # Don't modify bot name
  69. all_sessions = Bridge().get_bot("chat").sessions
  70. user_session = all_sessions.session_query(query, e_context['context']['session_id']).messages
  71. # chatgpt-tool-hub will reply you with many tools
  72. logger.debug("[tool]: just-go")
  73. try:
  74. _reply = self.app.ask(query, user_session)
  75. e_context.action = EventAction.BREAK_PASS
  76. all_sessions.session_reply(_reply, e_context['context']['session_id'])
  77. except Exception as e:
  78. logger.exception(e)
  79. logger.error(str(e))
  80. e_context['context'].content = "请你随机用一种聊天风格,提醒用户:这个问题tool插件暂时无法处理"
  81. reply.type = ReplyType.ERROR
  82. e_context.action = EventAction.BREAK
  83. return
  84. reply.content = _reply
  85. e_context['reply'] = reply
  86. return
  87. def _read_json(self) -> dict:
  88. curdir = os.path.dirname(__file__)
  89. config_path = os.path.join(curdir, "config.json")
  90. tool_config = {
  91. "tools": [],
  92. "kwargs": {}
  93. }
  94. if not os.path.exists(config_path):
  95. return tool_config
  96. else:
  97. with open(config_path, "r") as f:
  98. tool_config = json.load(f)
  99. return tool_config
  100. def _reset_app(self) -> App:
  101. tool_config = self._read_json()
  102. kwargs = tool_config.get("kwargs", {})
  103. if kwargs.get("model_name", "") == "":
  104. kwargs["model_name"] = conf().get("model", "gpt-3.5-turbo")
  105. return load_app(tools_list=tool_config.get("tools"), **tool_config.get("kwargs"))