lintonxue00
commited on
Commit
•
5714f9e
1
Parent(s):
aae1a6e
Upload ext_acg_image.py
Browse files- 不知道/回收站/1/ext_acg_image.py +70 -0
不知道/回收站/1/ext_acg_image.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from .Extension import Extension
|
2 |
+
import requests, random
|
3 |
+
|
4 |
+
# 拓展的配置信息,用于ai理解拓展的功能 *必填*
|
5 |
+
ext_config = {
|
6 |
+
"name": "acg_image",
|
7 |
+
"arguments": {
|
8 |
+
"keyword": "str", # 关键字
|
9 |
+
},
|
10 |
+
"description": "Send an ACG image related to the keyword(in Chinese). Usage in response: /#acg_image&动漫#/",
|
11 |
+
# 参考词,用于上下文参考使用,为空则每次都会被参考(消耗token)
|
12 |
+
"refer_word": [],
|
13 |
+
# 每次消息回复中最大调用次数,不填则默认为99
|
14 |
+
"max_call_times_per_msg": 3,
|
15 |
+
# 作者信息
|
16 |
+
"author": "KroMiose",
|
17 |
+
# 版本
|
18 |
+
"version": "0.0.1",
|
19 |
+
# 拓展简介
|
20 |
+
"intro": "发送ACG图片",
|
21 |
+
}
|
22 |
+
|
23 |
+
|
24 |
+
class CustomExtension(Extension):
|
25 |
+
async def call(self, arg_dict: dict, ctx_data: dict) -> dict:
|
26 |
+
""" 当拓展被调用时执行的函数 *由拓展自行实现*
|
27 |
+
|
28 |
+
参数:
|
29 |
+
arg_dict: dict, 由ai解析的参数字典 {参数名: 参数值}
|
30 |
+
"""
|
31 |
+
custom_config: dict = self.get_custom_config() # 获取yaml中的配置信息
|
32 |
+
|
33 |
+
token = custom_config.get('token', None)
|
34 |
+
if token is None:
|
35 |
+
return {
|
36 |
+
'text': error_msg if self.get_custom_config().get('debug', False) else None
|
37 |
+
}
|
38 |
+
|
39 |
+
keyword = arg_dict.get('keyword', '')
|
40 |
+
req_args = {
|
41 |
+
'token': custom_config['token'],
|
42 |
+
'format': 'json',
|
43 |
+
'keyword': keyword
|
44 |
+
}
|
45 |
+
|
46 |
+
url = "https://v2.alapi.cn/api/acg"
|
47 |
+
payload = {'token': custom_config['token'], 'format': 'json'}
|
48 |
+
headers = {'Content-Type': 'application/json'}
|
49 |
+
|
50 |
+
try:
|
51 |
+
res = requests.post(url, json=req_args, headers=headers, timeout=10).json()
|
52 |
+
except Exception as e:
|
53 |
+
return {
|
54 |
+
'text': f"[ext_acg_image] 访问api时发生错误: {e}"
|
55 |
+
|
56 |
+
}
|
57 |
+
|
58 |
+
if 'data' in res and isinstance(res['data'], list) and len(res['data']) > 0:
|
59 |
+
return {
|
60 |
+
'image': random.choice(res['data'])
|
61 |
+
}
|
62 |
+
else:
|
63 |
+
error_msg = f"[ext_acg_image] 返回的数据格式不正确: {res}"
|
64 |
+
return {
|
65 |
+
'text': error_msg if self.get_custom_config().get('debug', False) else None
|
66 |
+
}
|
67 |
+
|
68 |
+
def __init__(self, custom_config: dict):
|
69 |
+
super().__init__(custom_config=custom_config)
|
70 |
+
|