Skip to content

Commit

Permalink
Merge pull request #2 from CCYellowStar/增加搜索扩展和读取链接内容扩展
Browse files Browse the repository at this point in the history
增加搜索扩展和读取链接内容扩展,删除baidu
  • Loading branch information
CCYellowStar authored Mar 14, 2023
2 parents edd6caf + a50cf15 commit 9554f0e
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 10 deletions.
88 changes: 88 additions & 0 deletions extensions/ext_readLink.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from .Extension import Extension
import requests

# 拓展的配置信息,用于ai理解拓展的功能 *必填*
ext_config:dict = {
"name": "readLink", # 拓展名称,用于标识拓展
"arguments": {
"url": "str", # 关键字
},
# 拓展的描述信息,用于提示ai理解拓展的功能 *必填* 尽量简短 使用英文更节省token
# 如果bot无法理解拓展的功能,可适当添加使用示例 格式: /#拓展名&参数1&...&参数n#/
"description": "Open the link and read the text and wait for the results. (usage in response: /#readLink&https://www.bilibili.com/#/))",
# 参考词,用于上下文参考使用,为空则每次都会被参考(消耗token)
"refer_word": [],
# 每次消息回复中最大调用次数,不填则默认为99
"max_call_times_per_msg": 5,
# 作者信息
"author": "CCYellowStar",
# 版本
"version": "0.0.1",
# 拓展简介
"intro": "让机器人openai能阅读链接内容",
}

class CustomExtension(Extension):
async def call(self, arg_dict: dict, ctx_data: dict) -> dict:
""" 当拓展被调用时执行的函数 *由拓展自行实现*
参数:
arg_dict: dict, 由ai解析的参数字典 {参数名: 参数值}
"""
custom_config:dict = self.get_custom_config() # 获取yaml中的配置信息

# 从arg_dict中获取参数
U = arg_dict.get('url', None)

if U is None:
return {}
else:
quote = requests.utils.quote(U)

headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63'
}

url = f"https://ddg-webapp-aagd.vercel.app/url_to_text?url={quote}"

res = requests.get(url, headers=headers)
print(res.json())

try:
data=res.json()
text = data[0]['body'].replace("\n"," ")
title = data[0]['title']
from_ = "auto"
to_ = "en"
d = {"data": [text, from_, to_]}
r = requests.post("https://hf.space/embed/mikeee/gradio-gtr/+/api/predict", json=d) #翻译成英文
result = r.json()
text = result["data"][0]
except:
return {
'text': f"[ext_readLink] 读取链接错误",
'image': None, # 图片url
'voice': None, # 语音url
}
# 返回的信息将会被发送到会话中
if text != "Put something there, man.":
return {
'text': f'[ext_readLink] 读取: {U} ...',
'notify': {
'sender': '[readLink]',
'msg': f"[ext_readLink] 读取 {U} 结果:(To save tokens, the content of the web page has been translated into English)\n{text}\n{title}"
},
'wake_up': True, # 是否再次响应
}
else:
return {
'text': f'[ext_readLink] 读取: {U} ...',
'notify': {
'sender': '[readLink]',
'msg': f"[ext_readLink] 读取 {U} 结果: null\n{title}"
},
'wake_up': True, # 是否再次响应
}

def __init__(self, custom_config: dict):
super().__init__(ext_config.copy(), custom_config)
23 changes: 13 additions & 10 deletions extensions/ext_baidu.py → extensions/ext_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@
# 每次消息回复中最大调用次数,不填则默认为99
"max_call_times_per_msg": 5,
# 作者信息
"author": "KroMiose",
"author": "CCYellowStar",
# 版本
"version": "0.0.1"
"version": "0.0.1",
# 拓展简介
"intro": "让机器人openai能搜索信息",
}

class CustomExtension(Extension):
Expand All @@ -41,26 +43,27 @@ async def call(self, arg_dict: dict, ctx_data: dict) -> dict:
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36 Edg/110.0.1587.63'
}

url = f"http://baike.baidu.com/api/openapi/BaikeLemmaCardApi?scope=103&format=json&appid=379020&bk_key={quote}_length=600"
url = f"https://ddg-webapp-aagd.vercel.app/search?q={keyword}&max_results=3&region=cn-zh"

res = requests.get(url, headers=headers)
print(res.json()) # todo 此处拿不到数据,原因暂时不明
print(res.json())

try:
abstract = res.json()['abstract']
refer_url = res.json()['url']
data=res.json()
text = data[0]['body']+"\n"+data[0]['href']+"\n"+data[1]['body']+"\n"+data[1]['href']+"\n"+data[2]['body']+"\n"+data[2]['href']
#refer_url = data[0]['href']+"\n"+data[1]['href']+"\n"+data[2]['href']
except:
return {
'text': f"[ext_baidu] 未找到关于{keyword}的百科信息",
'text': f"[ext_search] 未找到关于{keyword}的信息",
'image': None, # 图片url
'voice': None, # 语音url
}
# 返回的信息将会被发送到会话中
return {
'text': f'[ext_baidu] 调用百度百科搜索: {keyword} ...',
'text': f'[ext_search] 搜索: {keyword} ...',
'notify': {
'sender': '[baidu]',
'msg': f"[ext_baidu] 百度百科搜索 {keyword} 结果:\n{abstract}\n{refer_url}"
'sender': '[search]',
'msg': f"[ext_search] 搜索 {keyword} 结果:\n{text}\n"
},
'wake_up': True, # 是否再次响应
}
Expand Down

0 comments on commit 9554f0e

Please sign in to comment.