Procházet zdrojové kódy

plugin(tool) ver0.4.1 (#891)

* plugin(tool) fix bugs

* plugin(tool) tool插件更新至0.4.1 版本
goldfish菌 před 3 roky
rodič
revize
0a7d6e4577
4 změnil soubory, kde provedl 74 přidání a 25 odebrání
  1. 1 0
      .gitignore
  2. 59 11
      plugins/tool/README.md
  3. 12 13
      plugins/tool/tool.py
  4. 2 1
      requirements-optional.txt

+ 1 - 0
.gitignore

@@ -13,6 +13,7 @@ plugins.json
 itchat.pkl
 *.log
 user_datas.pkl
+chatgpt_tool_hub/
 plugins/**/
 !plugins/bdunit
 !plugins/dungeon

+ 59 - 11
plugins/tool/README.md

@@ -9,9 +9,21 @@
 ### 1. python
 ###### python解释器,使用它来解释执行python指令,可以配合你想要chatgpt生成的代码输出结果或执行事务
 
-### 2. url-get
+### 2. 访问网页的工具汇总(默认url-get)
+
+#### 2.1 url-get
 ###### 往往用来获取某个网站具体内容,结果可能会被反爬策略影响
 
+#### 2.2 browser
+###### 浏览器,功能与2.1类似,但能更好模拟,不会被识别为爬虫影响获取网站内容
+
+> 注1:url-get默认配置、browser需额外配置,browser依赖google-chrome,你需要提前安装好
+
+> 注2:browser默认使用summary tool 分段总结长文本信息,tokens可能会大量消耗!
+
+这是debian端安装google-chrome教程,其他系统请执行查找
+> https://www.linuxjournal.com/content/how-can-you-install-google-browser-debian
+
 ### 3. terminal
 ###### 在你运行的电脑里执行shell命令,可以配合你想要chatgpt生成的代码使用,给予自然语言控制手段
 
@@ -38,47 +50,83 @@
 ### 5. wikipedia
 ###### 可以回答你想要知道确切的人事物
 
-### 6. news *
+### 6. 新闻类工具
+
+#### 6.1. news-api *
 ###### 从全球 80,000 多个信息源中获取当前和历史新闻文章
 
-### 7. morning-news *
+#### 6.2. morning-news *
 ###### 每日60秒早报,每天凌晨一点更新,本工具使用了[alapi-每日60秒早报](https://alapi.cn/api/view/93)
 
 > 该tool每天返回内容相同
 
-### 8. bing-search *
+#### 6.3. finance-news 
+###### 获取实时的金融财政新闻
+
+> 该工具需要解决browser tool 的google-chrome依赖安装
+
+### 7. bing-search *
 ###### bing搜索引擎,从此你不用再烦恼搜索要用哪些关键词
 
-### 9. wolfram-alpha *
+### 8. wolfram-alpha *
 ###### 知识搜索引擎、科学问答系统,常用于专业学科计算
 
-### 10. google-search *
+### 9. google-search *
 ###### google搜索引擎,申请流程较bing-search繁琐
 
-###### 注1:带*工具需要获取api-key才能使用,部分工具需要外网支持  
+
+### 10. arxiv(dev 开发中)
+###### 用于查找论文
+
+
+### 11. debug(dev 开发中,目前没有接入wechat)
+###### 当bot遇到无法确定的信息时,将会向你寻求帮助的工具
+
+
+### 12. summary
+###### 总结工具,该工具必须输入一个本地文件的绝对路径
+
+> 该工具目前是和其他工具配合使用,暂未测试单独使用效果
+
+
+### 13. image2text
+###### 将图片转换成文字,底层调用imageCaption模型,该工具必须输入一个本地文件的绝对路径
+
+
+### 14. searxng-search *
+###### 一个私有化的搜索引擎工具
+
+> 安装教程:https://docs.searxng.org/admin/installation.html
+
+---
+
+###### 注1:带*工具需要获取api-key才能使用(在config.json内的kwargs添加项),部分工具需要外网支持  
 #### [申请方法](https://github.com/goldfishh/chatgpt-tool-hub/blob/master/docs/apply_optional_tool.md)
 
 ## config.json 配置说明
 ###### 默认工具无需配置,其它工具需手动配置,一个例子:
 ```json
 {
-  "tools": ["wikipedia"],  // 填入你想用到的额外工具名
+  "tools": ["wikipedia", "你想要添加的其他工具"],  // 填入你想用到的额外工具名
   "kwargs": {
-      "request_timeout": 60,  // openai接口超时时间
+      "debug": true, // 当你遇到问题求助时,需要配置
+      "request_timeout": 120,  // openai接口超时时间
       "no_default": false,  // 是否不使用默认的4个工具
-      "OPTIONAL_API_NAME": "OPTIONAL_API_KEY"  // 带*工具需要申请api-key,在这里填入,api_name参考前述`申请方法`
+      // 带*工具需要申请api-key,在这里填入,api_name参考前述`申请方法`
   }
 }
 
 ```
 注:config.json文件非必须,未创建仍可使用本tool;带*工具需在kwargs填入对应api-key键值对  
-- `tools`:本插件初始化时加载的工具, 目前可选集:["wikipedia", "wolfram-alpha", "bing-search", "google-search", "news", "morning-news"] & 默认工具,除wikipedia工具之外均需要申请api-key
+- `tools`:本插件初始化时加载的工具, 目前可选集:["wikipedia", "wolfram-alpha", "bing-search", "google-search", "news"] & 默认工具,除wikipedia工具之外均需要申请api-key
 - `kwargs`:工具执行时的配置,一般在这里存放**api-key**,或环境配置
+  - `debug`: 输出chatgpt-tool-hub额外信息用于调试
   - `request_timeout`: 访问openai接口的超时时间,默认与wechat-on-chatgpt配置一致,可单独配置
   - `no_default`: 用于配置默认加载4个工具的行为,如果为true则仅使用tools列表工具,不加载默认工具
   - `top_k_results`: 控制所有有关搜索的工具返回条目数,数字越高则参考信息越多,但无用信息可能干扰判断,该值一般为2
   - `model_name`: 用于控制tool插件底层使用的llm模型,目前暂未测试3.5以外的模型,一般保持默认
 
+---
 
 ## 备注
 - 强烈建议申请搜索工具搭配使用,推荐bing-search

+ 12 - 13
plugins/tool/tool.py

@@ -1,7 +1,7 @@
 import json
 import os
 
-from chatgpt_tool_hub.apps import load_app
+from chatgpt_tool_hub.apps import AppFactory
 from chatgpt_tool_hub.apps.app import App
 from chatgpt_tool_hub.tools.all_tool_list import get_all_tool_names
 
@@ -18,7 +18,7 @@ from plugins import *
 @plugins.register(
     name="tool",
     desc="Arming your ChatGPT bot with various tools",
-    version="0.3",
+    version="0.4",
     author="goldfishh",
     desire_priority=0,
 )
@@ -131,17 +131,17 @@ class Tool(Plugin):
 
     def _build_tool_kwargs(self, kwargs: dict):
         tool_model_name = kwargs.get("model_name")
+        request_timeout = kwargs.get("request_timeout")
 
         return {
+            "debug": kwargs.get("debug", False),
             "openai_api_key": conf().get("open_ai_api_key", ""),
             "proxy": conf().get("proxy", ""),
-            "request_timeout": str(conf().get("request_timeout", 60)),
+            "request_timeout": request_timeout if request_timeout else conf().get("request_timeout", 120),
             # note: 目前tool暂未对其他模型测试,但这里仍对配置来源做了优先级区分,一般插件配置可覆盖全局配置
-            "model_name": tool_model_name
-            if tool_model_name
-            else conf().get("model", "gpt-3.5-turbo"),
+            "model_name": tool_model_name if tool_model_name else conf().get("model", "gpt-3.5-turbo"),
             "no_default": kwargs.get("no_default", False),
-            "top_k_results": kwargs.get("top_k_results", 2),
+            "top_k_results": kwargs.get("top_k_results", 3),
             # for news tool
             "news_api_key": kwargs.get("news_api_key", ""),
             # for bing-search tool
@@ -157,8 +157,6 @@ class Tool(Plugin):
             "zaobao_api_key": kwargs.get("zaobao_api_key", ""),
             # for visual_dl tool
             "cuda_device": kwargs.get("cuda_device", "cpu"),
-            # for browser tool
-            "phantomjs_exec_path": kwargs.get("phantomjs_exec_path", ""),
         }
 
     def _filter_tool_list(self, tool_list: list):
@@ -172,11 +170,12 @@ class Tool(Plugin):
 
     def _reset_app(self) -> App:
         tool_config = self._read_json()
+        app_kwargs = self._build_tool_kwargs(tool_config.get("kwargs", {}))
+
+        app = AppFactory()
+        app.init_env(**app_kwargs)
 
         # filter not support tool
         tool_list = self._filter_tool_list(tool_config.get("tools", []))
 
-        return load_app(
-            tools_list=tool_list,
-            **self._build_tool_kwargs(tool_config.get("kwargs", {})),
-        )
+        return app.create_app(tools_list=tool_list, **app_kwargs)

+ 2 - 1
requirements-optional.txt

@@ -20,5 +20,6 @@ pysilk_mod>=1.6.0 # needed by send voice
 web.py
 
 # chatgpt-tool-hub plugin
+
 --extra-index-url https://pypi.python.org/simple
-chatgpt_tool_hub>=0.3.9
+chatgpt_tool_hub>=0.4.1