forked from RockChinQ/WebwlkrPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
65 lines (49 loc) · 2.11 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from pkg.plugin.models import *
from pkg.plugin.host import EventContext, PluginHost
import logging
import re
import os
import shutil
import yaml
from . import mux, webpilot
backend_mapping = {
"webpilot": webpilot.process,
"native": mux.process,
}
process: callable = None
# 注册插件
@register(name="Webwlkr", description="基于GPT的函数调用能力,为QChatGPT提供联网功能", version="0.1.1", author="RockChinQ")
class WebwlkrPlugin(Plugin):
cfg: dict = None
# 插件加载时触发
# plugin_host (pkg.plugin.host.PluginHost) 提供了与主程序交互的一些方法,详细请查看其源码
def __init__(self, plugin_host: PluginHost):
global process
# 检查webwlkr.yaml是否存在
if not os.path.exists("webwlkr.yaml"):
shutil.copyfile("plugins/WebwlkrPlugin/config-template.yaml", "webwlkr.yaml")
# 读取配置文件
with open("webwlkr.yaml", "r", encoding="utf-8") as f:
self.cfg = yaml.load(f, Loader=yaml.FullLoader)
process = backend_mapping[self.cfg["backend"]]
@func("access_the_web")
def _(url: str, brief_len: int):
"""Call this function to search about the question before you answer any questions.
- Do not search through google.com at any time.
- If you need to search somthing, visit https://www.sogou.com/web?query=<something>.
- If user ask you to open a url (start with http:// or https://), visit it directly.
- Summary the plain content result by yourself, DO NOT directly output anything in the result you got.
Args:
url(str): url to visit
brief_len(int): max length of the plain text content, recommend 1024-4096, prefer 4096
Returns:
str: plain text content of the web page or error message(starts with 'error:')
"""
try:
return process(url, brief_len)
except Exception as e:
logging.error("[Webwlkr] error visit web: {}".format(e))
return "error visit web:{}".format(e)
# 插件卸载时触发
def __del__(self):
pass