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