绘画模型 调用示例

本文中,我们将介绍如何使用 Python 调用 OpenKEY 提供的ChatGPT API接口来调用 ChatGPT 两个绘画模型,即 dall-e-2 和 dall-e-3。
系统环境:
Python 3.8.10
Name: openai Version: 0.27.8
import requests
import json
# DALL-E 2的接口地址
url = 'https://openkey.cloud/v1/images/generations'
# 您的API密钥
api_key = 'sk-xxxx'
# 请求头部
headers = {
'Authorization': f'Bearer {api_key}',
'Content-Type': 'application/json',
}
# 请求的数据
payload = {
"model": "dall-e-3",
'prompt': '想象一个充满活力的科幻世界,这里是“牛爷爷的彩色星球”。中心是一个仁慈、智慧的牛爷爷形象,他的外观类似于传统的中国智者,身穿宇航服,周围环绕着五颜六色的星球和星云。这些星球光彩夺目,各具特色,有的覆盖着翠绿的森林,有的环绕着蓝色的海洋,还有的散发着神秘的紫色光芒。海报的背景是深邃的宇宙,点缀着繁星和迷人的星云。整个画面应该流露出一种未来科技与自然和谐共存的感觉,充满神秘和梦幻的氛围。牛爷爷的面容慈祥,目光深邃,仿佛在诉说着宇宙的奥秘和生命的多彩。',
'n': 1,
'quality': "hd",
'size': '1792x1024' # 修改这里
}
# 发送POST请求
response = requests.post(url, headers=headers, data=json.dumps(payload))
# 打印响应结果
print(response.json())
终端输出如下:
root@vmi1152840:/web/openai/models# python3 dall-e-3.py
{'created': 1701934687, 'data': [{'revised_prompt': "Imagine a vibrant sci-fi world named 'Grandpa Bull's Colorful Planet'. At the center is a benevolent and wise figure resembling a traditional Chinese sage, portrayed as Grandpa Bull in a spacesuit. Surrounding him are multicolored planets and nebulae. These Heavenly bodies are visually striking, each distinct from the next. Some are covered in lush green forests, others are surrounded by blue oceans, while others radiate a mystical purple light. The backdrop of the image is the depths of space, sprinkled with stars and enchanting nebulae. The entire scene should exude a feeling of future technology and nature coexisting harmoniously, filled with a sense of mystery and a dreamy atmosphere. Grandpa Bull's face is kind, his eyes deep, as if narrating the secrets of the universe and the vibrancy of life.", 'url': 'https://oaidalleapiprodscus.blob.core.windows.net/private/org-T97RC5fx0Oy4oBPrCS3W7Yz1/user-3TT738PAhNgKOU9XICWYPJum/img-kQHy5qs6yMVdswY7nVabUZt0.png?st=2023-12-07T06%3A38%3A07Z&se=2023-12-07T08%3A38%3A07Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-12-07T02%3A11%3A02Z&ske=2023-12-08T02%3A11%3A02Z&sks=b&skv=2021-08-06&sig=dJBaxWuHcr0suSAEBdVW2nqRnPk/U0TJie9M8CqICmU%3D'}]}
其中生成的图片效果如下:
OpenKey完全兼容OpenAI接口协议,具体调用方法参考官方文档:
Last modified 1mo ago