|
import openai |
|
|
|
from .Extension import Extension |
|
|
|
|
|
ext_config:dict = { |
|
"name": "paint", |
|
"arguments": { |
|
'style': 'str', |
|
'content': 'str', |
|
}, |
|
"description": "paint a picture.", |
|
|
|
"refer_word": ['paint', '画', '图'], |
|
|
|
"max_call_times_per_msg": 1, |
|
|
|
"author": "OREOREO", |
|
|
|
"version": "0.0.1", |
|
|
|
"intro": "绘图", |
|
} |
|
|
|
class CustomExtension(Extension): |
|
async def call(self, arg_dict: dict, ctx_data: dict) -> dict: |
|
""" 当拓展被调用时执行的函数 *由拓展自行实现* |
|
|
|
参数: |
|
arg_dict: dict, 由ai解析的参数字典 {参数名: 参数值(类型为str)} |
|
""" |
|
custom_config:dict = self.get_custom_config() |
|
|
|
|
|
content = arg_dict.get('content', None) |
|
style = arg_dict.get('style', None) |
|
|
|
if style is None: |
|
style = ',anime style, colored-pencil' |
|
|
|
response = openai.Image.create( |
|
prompt= content + ',' + style + ', high detail.', |
|
n=1, |
|
size="1024x1024" |
|
) |
|
image_url = response['data'][0]['url'] |
|
res = response |
|
|
|
if image_url is None: |
|
return { |
|
'text': "[ext_paint] 图片生成错误...", |
|
'image': None, |
|
'voice': None, |
|
} |
|
elif "rejected" in res: |
|
|
|
return { |
|
'text': "[ext_paint] 抱歉,这个图违反了ai生成规定,可能是太色了吧", |
|
'image': None, |
|
'voice': None, |
|
} |
|
else: |
|
|
|
return { |
|
'text': None, |
|
'image': image_url, |
|
'voice': None, |
|
} |
|
|
|
def __init__(self, custom_config: dict): |
|
super().__init__(ext_config.copy(), custom_config) |