首页 > 其他分享 >OpenAI Images Generations API 申请及使用

OpenAI Images Generations API 申请及使用

时间:2024-08-28 13:52:14浏览次数:9  
标签:url json API OpenAI sea Images 图片 its otter

OpenAI Images Generations API 申请及使用

DALL-E 3 是 OpenAI 开发的两个版本的图像生成模型,它们能够根据文本描述生成高质量的图像。

本文档主要介绍 OpenAI Images Generations API 操作的使用流程,利用它我们可以轻松使用官方 OpenAI DALL-E 的图像生成功能。

申请流程

要使用 OpenAI Images Generations API,首先可以到 OpenAI Images Generations API 页面点击「Acquire」按钮,获取请求所需要的凭证:

外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

如果你尚未登录或注册,会自动跳转到登录页面邀请您来注册和登录,登录注册之后会自动返回当前页面。

在首次申请时会有免费额度赠送,可以免费使用该 API。

基本使用

接下来就可以在界面上填写对应的内容,如图所示:

在第一次使用该接口时,我们至少需要填写三个内容,一个是 authorization,直接在下拉列表里面选择即可。另一个参数是 modelmodel 就是我们选择使用 OpenAI DALL-E 官网模型类别,这里我们主要有 1 种模型,详情可以看我们提供的模型。最后一个参数是promptprompt 是我们输入要生成图像的提示词。

同时您可以注意到右侧有对应的调用代码生成,您可以复制代码直接运行,也可以直接点击「Try」按钮进行测试。

Python 样例调用代码:

import requests

url = "https://api.acedata.cloud/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

调用之后,我们发现返回结果如下:

{
  "created": 1721626477,
  "data": [
    {
      "revised_prompt": "A delightful image showcasing a young sea otter, who is born brown, with wide charming eyes. It is delightfully lying on its back, paddling in the calm sea waters. Its dense, velvety fur appears wet and shimmering, capturing the essence of its habitat. The small creature curiously plays with a sea shell with its small paws, looking absolutely innocent and charming in its natural environment.",
      "url": "https://dalleprodsec.blob.core.windows.net/private/images/5d98aa7c-80c6-4523-b571-fc606ad455b9/generated_00.png?se=2024-07-23T05%3A34%3A48Z&sig=GAz%2Bi3%2BkHOQwAMhxcv22tBM%2FaexrxPgT9V0DbNrL4ik%3D&ske=2024-07-23T08%3A41%3A10Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-07-16T08%3A41%3A10Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02"
    }
  ]
}

返回结果一共有多个字段,介绍如下:

  • created ,生成此次图像生成的 ID,用于唯一标识此次任务。
  • data,包含图像生成的结果信息。

其中 data 是包含了模型生成图片的具体信息,它里面的 url 是生成图片的详情链接,可以发现如图所示。

图片质量参数 quality

接下来将介绍如何设置图像生成结果的一些详细参数,其中图片质量参数 quality 包含俩种,第一个 standard 表示生成标准的图片,另一个 hd 表示创建的图像具有更精细的细节和更大的一致性。

下面设置图片质量参数为 standard ,具体设置如下图:

同时您可以注意到右侧有对应的调用代码生成,您可以复制代码直接运行,也可以直接点击「Try」按钮进行测试。

Python 样例调用代码:

import requests

url = "https://api.acedata.cloud/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter",
    "quality": "standard"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

调用之后,我们发现返回结果如下:

{
  "created": 1721636023,
  "data": [
    {
      "revised_prompt": "A cute baby sea otter is lying playfully on its back in the water, with its fur looking glossy and soft. One of its tiny paws is reaching out curiously, and it has an expression of pure joy and warmth on its face as it looks up to the sky. Its body is surrounded by bubbles from its playful twirling in the water. A gentle breeze is playing with its fur making it look more charming. The scene portrays the tranquility and charm of marine life.",
      "url": "https://dalleprodsec.blob.core.windows.net/private/images/a93ee5e7-3abd-4923-8d79-dc9ef126da46/generated_00.png?se=2024-07-23T08%3A13%3A55Z&sig=wTXGYvUOwUIkaB2CxjK9ww%2FHjS8OwYUWcYInXYKwcAM%3D&ske=2024-07-23T11%3A32%3A05Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-07-16T11%3A32%3A05Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02"
    }
  ]
}

返回的结果与基本使用的内容一致,可以看到图片质量参数为 standard 的生成图片如下图所示:

与上述相同操作,仅需将图片质量参数设置为 hd ,可以得到如下图所示的图片:

可以看到 hdstandard 生成的图片具有更精细的细节和更大的一致性。

图片大小尺寸参数 size

我们还可以设置生成图片的尺寸大小,我们可以进行下面的设置。

下面设置图片的尺寸大小为 1024 * 1024 ,具体设置如下图:

同时您可以注意到右侧有对应的调用代码生成,您可以复制代码直接运行,也可以直接点击「Try」按钮进行测试。

Python 样例调用代码:

import requests

url = "https://api.acedata.cloud/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter"
    "size": "1024x1024"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

调用之后,我们发现返回结果如下:

{
  "created": 1721636652,
  "data": [
    {
      "revised_prompt": "A delightful depiction of a baby sea otter. The small mammal is captured in its natural habitat in the ocean, floating on its back. It has thick brown fur that is sleek and wet from the sea water. Its eyes are closed as if it is enjoying a moment of deep relaxation. The water around it is calm, reflecting the peacefulness of the scene. The background should hint at a diverse marine ecosystem, with visible strands of kelp floating on the surface, suggesting the baby otter's preferred environment.",
      "url": "https://dalleprodsec.blob.core.windows.net/private/images/9d625ac6-fd2b-42a9-84a6-8c99eb357ccf/generated_00.png?se=2024-07-23T08%3A24%3A24Z&sig=AXtYXowEakGxfRp8LhC2DwqL%2F07LhEDW40oCP%2BdTO8s%3D&ske=2024-07-23T18%3A00%3A45Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-07-16T18%3A00%3A45Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02"
    }
  ]
}

返回的结果与基本使用的内容一致,可以看到图片的尺寸大小为 1024 * 1024 的生成图片如下图所示:

与上述相同操作,仅需将图片的尺寸大小为 1792 * 1024 ,可以得到如下图所示的图片:

可以看到图片的尺寸大小很明显不一样,另外还可以设置更多尺寸大小,详情信息参考我们官网文档。

图片风格参数 style

图片风格参数 style 包含俩个参数,第一种 vivid 表示生成的图片是更加生动的,另一种 natural 表示生成的图片更加的自然一点。

下面设置图片风格参数为 vivid ,具体设置如下图:

同时您可以注意到右侧有对应的调用代码生成,您可以复制代码直接运行,也可以直接点击「Try」按钮进行测试。

Python 样例调用代码:

import requests

url = "https://api.acedata.cloud/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter",
    "style": "vivid"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

调用之后,我们发现返回结果如下:

{
  "created": 1721637086,
  "data": [
    {
      "revised_prompt": "A baby sea otter with soft, shiny fur and sparkling eyes floating playfully on calm ocean waters. This adorable creature is trippingly frolicking amidst small, gentle waves under a bright, clear, sunny sky. The tranquility of the sea contrasts subtly with the delightful energy of this young otter. The critter gamely clings to a tiny piece of driftwood, its small paws adorably enveloping the floating object.",
      "url": "https://dalleprodsec.blob.core.windows.net/private/images/6e48f701-7fd3-4356-839e-a2f6f0fe82d9/generated_00.png?se=2024-07-23T08%3A31%3A37Z&sig=4percxqTbUR1j3BQmkhvj%2FAhHzInKI%2FqiTo1MP69coI%3D&ske=2024-07-27T10%3A39%3A55Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-07-20T10%3A39%3A55Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02"
    }
  ]
}

返回的结果与基本使用的内容一致,可以看到图片风格参数为 vivid 的生成图片如下图所示:

与上述相同操作,仅需将图片风格参数为 natural ,可以得到如下图所示的图片:

可以看到 vividnatural 生成的图片具有更加生动逼真。

图片链接的格式参数 response_format

最后一个图片链接的格式参数 response_format 也有俩种,第一种 b64_json 是对图片链接进行 Base64 编码,另一种 url 就是普通的图片链接,可以直接查看图片。

下面设置图片链接的格式参数为 url ,具体设置如下图:

同时您可以注意到右侧有对应的调用代码生成,您可以复制代码直接运行,也可以直接点击「Try」按钮进行测试。

Python 样例调用代码:

import requests

url = "https://api.acedata.cloud/openai/images/generations"

headers = {
    "accept": "application/json",
    "authorization": "Bearer {token}",
    "content-type": "application/json"
}

payload = {
    "model": "dall-e-3",
    "prompt": "A cute baby sea otter",
    "response_format": "url"
}

response = requests.post(url, json=payload, headers=headers)
print(response.text)

调用之后,我们发现返回结果如下:

{
  "created": 1721637575,
  "data": [
    {
      "revised_prompt": "A charming depiction of a baby sea otter. The otter is seen resting serenely on its back amidst the gentle, blue ocean waves. The baby otter's fur is an endearing mix of soft greyish brown shades, glinting subtly in the muted sunlight. Its small paws are touching, lifted slightly towards the sky as if playing with an unseen object. Its round, expressive eyes are wide in curiosity, sparking with life and innocence. Use a realistic style to evoke the otter's natural habitat and its adorably fluffy exterior.",
      "url": "https://dalleprodsec.blob.core.windows.net/private/images/87792c5f-8b6d-412e-81dd-f1a1baa19bd2/generated_00.png?se=2024-07-23T08%3A39%3A47Z&sig=zzRAn30TqIKHdLVqZPUUuSJdjCYpoJdaGU6BeoA76Jo%3D&ske=2024-07-23T13%3A32%3A13Z&skoid=e52d5ed7-0657-4f62-bc12-7e5dbb260a96&sks=b&skt=2024-07-16T13%3A32%3A13Z&sktid=33e01921-4d64-4f8c-a055-5bdaffd5e33d&skv=2020-10-02&sp=r&spr=https&sr=b&sv=2020-10-02"
    }
  ]
}

返回的结果与基本使用的内容一致,可以看到图片链接的格式参数为 url 的生成图片的链接为 图片 URL 这是可以直接访问的,图片内容如下图所示:

与上述相同操作,仅需将图片链接的格式参数为 b64_json ,可以得到结果 Base64 编码后的图片链接,具体结果如下图所示:

{
  "created": 1721638071,
  "data": [
    {
      "b64_json": "iVBORw0..............v//AQEAAP4AAAD+AAADAQAAAwEEA/4D//8Q/Pbw64mKbVTFoQAAAABJRU5ErkJggg==",
      "revised_prompt": "A charming image of a young baby sea otter. The otter is gently floating on a calm blue sea, basking in the warm, golden rays of sunlight streaming down from a clear sky above. The otter's fur is a rich chocolate brown, and it looks incredibly soft and fluffy. The otter's eyes are bright and expressive, filled with childlike curiosity and joy. It has small, pricked ears and a button-like nose which adds to its overall cuteness. In the sea around it, twinkling droplets of water can be seen, pepped up by the sunlight, the sight is certainly a delightful one."
    }
  ]
}

错误处理

在调用 API 时,如果遇到错误,API 会返回相应的错误代码和信息。例如:

  • 400 token_mismatched:Bad request, possibly due to missing or invalid parameters.
  • 400 api_not_implemented:Bad request, possibly due to missing or invalid parameters.
  • 401 invalid_token:Unauthorized, invalid or missing authorization token.
  • 429 too_many_requests:Too many requests, you have exceeded the rate limit.
  • 500 api_error:Internal server error, something went wrong on the server.

错误响应示例

{
  "success": false,
  "error": {
    "code": "api_error",
    "message": "fetch failed"
  },
  "trace_id": "2cf86e86-22a4-46e1-ac2f-032c0f2a4e89"
}

结论

通过本文档,您已经了解了如何使用 OpenAI Images Generations API 轻松使用官方 OpenAI DALL-E 的图像生成功能。希望本文档能帮助您更好地对接和使用该 API。如有任何问题,请随时联系我们的技术支持团队。

标签:url,json,API,OpenAI,sea,Images,图片,its,otter
From: https://blog.csdn.net/Acedata1/article/details/141565497

相关文章

  • OpenAI Chat Completion API 申请及使用
    OpenAIChatCompletionAPI申请及使用OpenAIChatGPT是一款非常强大的AI对话系统,只要输入提示词,就能在短短几秒内生成流畅自然的回复。ChatGPT以其出色的语言理解和生成能力在业界独树一帜,如今,ChatGPT早已在各个行业和领域广泛应用,其影响力愈发显著。无论是日常对话......
  • API接口的安全性分类及其对开发的影响
    API接口的分类:按照访问权限分公开APIs:对所有用户开放的API。私有APIs:只对特定用户或组织开放的API。内部API:仅供组织内部使用的API,用于连接组织内部的不同系统或服务。第三方APIs:由第三方开发并提供的API。API接口的分类:按照安全分非安全API:这些API可能不提供或仅提供......
  • 零成本、无编程,GLM-4-Flash免费API发布,算法工程师嗨翻了!!!
    作为一名资深NLP算法工程师,大模型在日常工作中扮演了非常重要的角色,辅助处理很多工作。但是,大模型的使用非常麻烦:主流大模型通过网页对话方式交互,手工输入Promt,通过对话的方式获取结果,长度有限且非常不方便。资源有限,市面上很少大模型API资源可供使用,并且都是收费的。今......
  • 电商API接口能解决什么问题?
    电商API接口能帮您解决多种与电商业务相关的问题,主要包括:商品信息获取:获取商品的详细信息,包括价格、库存、规格、描述、图片等。订单管理:创建、查询、修改订单,处理订单状态,包括发货、取消、退货等。库存管理:实时更新库存信息,确保库存数据的准确性。用户管理:获取用户信息,管理用......
  • RapidCMS 几个常见漏洞
    侵权声明本文章中的所有内容(包括但不限于文字、图像和其他媒体)仅供教育和参考目的。如果在本文章中使用了任何受版权保护的材料,我们满怀敬意地承认该内容的版权归原作者所有。如果您是版权持有人,并且认为您的作品被侵犯,请通过以下方式与我们联系:[[email protected]]。我们将在确......
  • js练习--用户管理API
    需要node.js运行环境,创建2个文件:user.js,server.jsuser.js:letusers={};module.exports=users;server.js:consthttp=require('http');//导入user模块letusers=require('./user');//创建HTTP服务器constserver=http.createServer((req,res)=......
  • 使用FastAPI来开发项目,项目的目录结构如何规划的一些参考和基类封装的一些处理
    使用FastAPI开发项目时,良好的目录结构可以帮助你更好地组织代码,提高可维护性和扩展性。同样,对基类的封装,也可以进一步减少开发代码,提供便利,并减少出错的几率。下面是一个推荐的目录结构示例:my_fastapi_project/├──app/│├──__init__.py│├──main.py......
  • 使用Python进行Mock测试详解(含Web API接口Mock)
    使用Python进行Mock测试详解(含WebAPI接口Mock)在软件开发过程中,单元测试是非常重要的一部分。为了确保代码的质量和可靠性,开发者需要编写测试用例来检查代码的行为是否符合预期。然而,在测试中有时会遇到一些难以直接测试的情况,例如依赖外部系统、数据库或网络服务等。在这......
  • Express+MySQL+Sequelize实作API
    本文章为观看哔站视频Express+MySQL+Sequelize实作API所作随笔,边敲代码便跟学......
  • GLM-4-Flash 大模型API免费了,手把手构建“儿童绘本”应用实战(附源码)
    老牛同学刚刷到了一条劲爆的消息,GLM-4-Flash大模型推理API免费了:https://bigmodel.cn/pricing老牛同学一直觉得上次阿里云百炼平台为期1个月免费额度的“羊毛”已经够大了(太卷了,阿里云免费1个月大模型算力额度,玩转Llama3.1/Qwen2等训练推理),但经过老牛同学在智谱AI官网......