lintonxue00
commited on
Commit
•
88e64ce
1
Parent(s):
48fba35
Upload ext_paint.py
Browse files- 不知道/回收站/1/ext_paint.py +71 -0
不知道/回收站/1/ext_paint.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import openai
|
2 |
+
|
3 |
+
from .Extension import Extension
|
4 |
+
|
5 |
+
# 拓展的配置信息,用于ai理解拓展的功能 *必填*
|
6 |
+
ext_config:dict = {
|
7 |
+
"name": "paint", # 拓展名称,用于标识拓展
|
8 |
+
"arguments": {
|
9 |
+
'style': 'str', # 绘画风格
|
10 |
+
'content': 'str', # 绘画内容描述
|
11 |
+
},
|
12 |
+
"description": "paint a picture.",
|
13 |
+
# 参考词,用于上下文参考使用,为空则每次都会被参考(消耗token)
|
14 |
+
"refer_word": ['paint', '画', '图'],
|
15 |
+
# 每次消息回复中最大调用次数,不填则默认为99
|
16 |
+
"max_call_times_per_msg": 1,
|
17 |
+
# 作者信息
|
18 |
+
"author": "OREOREO",
|
19 |
+
# 版本
|
20 |
+
"version": "0.0.1",
|
21 |
+
# 拓展简介
|
22 |
+
"intro": "绘图",
|
23 |
+
}
|
24 |
+
|
25 |
+
class CustomExtension(Extension):
|
26 |
+
async def call(self, arg_dict: dict, ctx_data: dict) -> dict:
|
27 |
+
""" 当拓展被调用时执行的函数 *由拓展自行实现*
|
28 |
+
|
29 |
+
参数:
|
30 |
+
arg_dict: dict, 由ai解析的参数字典 {参数名: 参数值(类型为str)}
|
31 |
+
"""
|
32 |
+
custom_config:dict = self.get_custom_config() # 获取yaml中的配置信息
|
33 |
+
|
34 |
+
# 从arg_dict中获取参数
|
35 |
+
content = arg_dict.get('content', None)
|
36 |
+
style = arg_dict.get('style', None)
|
37 |
+
|
38 |
+
if style is None:
|
39 |
+
style = ',anime style, colored-pencil'
|
40 |
+
|
41 |
+
response = openai.Image.create(
|
42 |
+
prompt= content + ',' + style + ', high detail.',
|
43 |
+
n=1,
|
44 |
+
size="1024x1024"
|
45 |
+
)
|
46 |
+
image_url = response['data'][0]['url']
|
47 |
+
res = response
|
48 |
+
|
49 |
+
if image_url is None:
|
50 |
+
return {
|
51 |
+
'text': "[ext_paint] 图片生成错误...",
|
52 |
+
'image': None, # 图片url
|
53 |
+
'voice': None, # 语音url
|
54 |
+
}
|
55 |
+
elif "rejected" in res:
|
56 |
+
# 返回的信息将会被发送到会话中
|
57 |
+
return {
|
58 |
+
'text': "[ext_paint] 抱歉,这个图违反了ai生成规定,可能是太色了吧", # 文本信息
|
59 |
+
'image': None, # 图片url
|
60 |
+
'voice': None, # 语音url
|
61 |
+
}
|
62 |
+
else:
|
63 |
+
# 返回的信息将会被发送到会话中
|
64 |
+
return {
|
65 |
+
'text': None, # 文本信息
|
66 |
+
'image': image_url, # 图片url
|
67 |
+
'voice': None, # 语音url
|
68 |
+
}
|
69 |
+
|
70 |
+
def __init__(self, custom_config: dict):
|
71 |
+
super().__init__(ext_config.copy(), custom_config)
|