Skip to content

Commit

Permalink
Dall-e-3画图扩展 (#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yang-ZhenHao authored Nov 28, 2023
1 parent 86686f1 commit 7daae9c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ nb plugin install nonebot-plugin-naturel-gpt
<details>
<summary>点击展开</summary>

### [2023/11/28] Dall-e-3 绘图支持

- 新增 Dall-e-3 绘图扩展,支持使用Dall-e-3绘图

### [2023/11/26] Stable Diffusion 绘图支持

- 新增 Stable Diffusion 绘图扩展,支持使用任意 SD 后端由 AI 自主绘图
Expand Down
23 changes: 23 additions & 0 deletions docs/extension_list.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@ always_negative_prompt: too many fingers, long neck, bad anatomy, bad hands, tex
<hr />
## [Dall-e-3 绘画扩展](https://github.com/KroMiose/nonebot_plugin_naturel_gpt/blob/main/extensions/ext_dalle_draw.py)
### 简介 <!-- {docsify-ignore} -->
调用dall-e-3绘制图像并发送
### 配置 <!-- {docsify-ignore} -->
请根据下方示例配置中的注释来编辑你的扩展配置
```yml
# 调用 dalle3 使用的api key
key: sk-xxxxxxxxxxxxxxxx

# 使用代理地址
proxy: null

# 请求的url地址
url: https://api.openai.com/v1/images/generations
```
<hr />
## [发送随机二次元图片 (ixiaowai)](https://github.com/KroMiose/nonebot_plugin_naturel_gpt/blob/main/extensions/ext_random_pic.py)
!> 请勿与其它发图拓展一并启用
Expand Down
70 changes: 70 additions & 0 deletions extensions/ext_dalle_draw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from typing import Union
from .Extension import Extension
import requests

# 扩展的配置信息
ext_config = {
"name": "draw",
"arguments": {
"prompt": "str",
"size": "str",
},
"description": "绘制一副图像,可选尺寸1024x1024,1792x1024,1024x1792,prompt必须为英文,用法:/#draw&prompt&size#/。示例:/#draw&a cute cat&1024x1024#/",
"refer_word": ["画", "图"],
"max_call_times_per_msg": 3,
"author": "微量元素",
"version": "0.0.1",
"intro": "调用dall-e-3绘制图像",
}


class CustomExtension(Extension):

async def call(self, arg_dict: dict, custom_config: dict) -> Union[dict, str]:

# 获取参数
prompt = arg_dict.get("prompt")
size = arg_dict.get("size")

# 获取配置信息
custom_config: dict = self.get_custom_config()
api_key = custom_config.get("key")
proxy = custom_config.get("proxy", "")
api_url = custom_config.get("url", "https://api.openai.com/v1/images/generations")

# 构造请求
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json",
}
proxies = {
"https": proxy
}
payload = {
"model": "dall-e-3",
"prompt": prompt,
"n": 1,
"size": size,
}

# 发送请求
response = requests.post(api_url, headers=headers, json=payload, proxies=proxies)

# 检查响应状态码
if response.status_code == 200:

# 返回图片URL
image_url = response.json()["data"][0]["url"]
return {
"text": f"画好了喵~",
"image": image_url,
}

else:
return {
"text": f"呜呜呜画不出来喵"
f"Error: {response.status_code} {response.text}"
}

def __init__(self, custom_config: dict):
super().__init__(ext_config, custom_config)

0 comments on commit 7daae9c

Please sign in to comment.