本文目录
隐藏
OpenAI DALL·E接口的功能是给定文字生成对应图像,或者是给定旧图像生成修改后的新图像(编辑/变化),有dall-e-3和dall-e-2两个模型,本文分享下便携AI聚合API DALL·E图像生成接口接入教程。
一、前言
本文参考OpenAI官方API文档:
- API reference:https://platform.openai.com/docs/api-reference
- Docs:https://platform.openai.com/docs/overview
文中使用的API Key均为便携AI聚合API后台生成的令牌,以sk-开头的一串随机字符,获取方法:《便携AI聚合API新建令牌(API key)教程》。
便携AI聚合API有三个接入地址(即URL),一般推荐选择第一个或者第二个:
- 中转API调用地址①(中国香港服务器,直连线路,带宽大):
https://api.bianxie.ai
- 中转API调用地址②(国内上海服务器,带宽稍小):
https://api.bianxieai.com
- 中转API调用地址③(国外服务器,也可以直连,备用):
https://api.a8.hk
(三个网站都可以登录账号,数据同步)
模型支持各种语言接入,包括python、PHP、C#、C、Ruby、Java、Go、JavaScript等,本文主要分享官方的CURL调用方法,以及基于python的调用方法,如果你是用其他语言调用API的,则直接问ChatGPT或者其他语言模型怎么改写就行了,如下图:
二、DALL·E模型接入教程
DALL·E模型可以根据文本描述生成图像,也可以修改旧的图像(提供prompt),文档可以参考官方文档:https://platform.openai.com/docs/guides/images
目前DALL·E有连个模型:dall-e-3和dall-e-2,其中:
- 1792×1024 ( 仅Dalle3 )
- 1024×1792 ( 仅Dalle3 )
- 1024×1024 ( Dalle2 ,Dalle3 )
- 512×512 ( 仅Dalle2 )
- 256×256 ( 仅Dalle2 )
1、图像生成(Generations)
OpenAI官方示例:
curl https://api.bianxie.ai/v1/images/generations \ -H "Content-Type: application/json" \ -H "Authorization: Bearer $OPENAI_API_KEY" \ -d '{ "model": "dall-e-3", "prompt": "a white siamese cat", "n": 1, "size": "1024x1024" }'
python代码:
import requests api_key = 'sk-Xy3WuCpTTvY19g' url = 'https://api.bianxie.ai/v1/images/generations' data = { "model": "dall-e-3", "prompt": "a white siamese cat", "n": 1, "size": "1024x1024" } headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" } response = requests.post(url, headers=headers, json=data) if response.status_code == 200: print("Image generation response:", response.json()) else: print(f"Request failed: {response.status_code} - {response.text}")
回复示例:
Image generation response: { 'created': 1717564644, 'data': [{ 'url': 'https://dalleprodsec.blob.core.windows.net/private/images/21919d3e-12c6-4ebd-9563-121f567b9224/generated_00.png?se=2024-06-06T05%3A17%3A35Z&sig=Wv9eWw0xzxeSz6ozYYwJbtXxYh1z25bgr0cSXxHffV0%3D&ske=2024-06-08T04%3A49%3A45Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-06-01T04%3A49%3A45Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02', 'revised_prompt': 'A white Siamese cat elegantly poised showing its prominent sapphire blue eyes, a sharp contrast against its fair fur. Its ears have dark tips matching the silky fur on its tail and paws giving it the distinctive features of a purebred Siamese cat. Its sleek body reflects its agility and athleticism. The cat is situated on a plush rug, its soft texture caressing its paws, looking curiously at a gentle butterfly fluttering nearby.' }] }
这是生成的图像:
2、图像编辑(Edit)
中转暂时不支持
3、图像变化(Variations)
中转暂时不支持