Closed3

OpenAI DALL·E 3 APIを試してみる

kun432kun432
!pip install -q openai
from google.colab import userdata
import os

os.environ["OPENAI_API_KEY"] = userdata.get('OPENAI_API_KEY')
from openai import OpenAI
from pprint import pprint
import json

prompt = """
a white siamese cat
"""

client = OpenAI()

response = client.images.generate(
  model="dall-e-3",
  prompt=prompt,
  size="1024x1024",
  quality="standard",
  n=1,
)

pprint(json.loads(response.json()),indent=2)

レスポンスはこんな感じになっている。

{ 'created': 1699483570,
  'data': [ { 'b64_json': None,
              'revised_prompt': 'A portrait of a Siamese cat with a striking '
                                'coat of pure white. The feline has vivid blue '
                                'eyes that almost seem to glow, its ears are '
                                'large and pointed, typical of the breed. Its '
                                'posture is elegant and relaxed, the tail is '
                                'long and slender, curled playfully around the '
                                "cat’s body. It's placed in a simple yet "
                                'sophisticated setting – perhaps a sun-filled '
                                'room with delicate rays illuminating the '
                                "cat's flawless white fur.",
              'url': 'https://oaidalleapiprodscus.blob.core.windows.net/private/org-XXXXXXXXXX/user-XXXXXXXXXX/img-XXXXXXXXXX.png?st=2023-11-08T21%3A46%3A10Z&se=2023-11-08T23%3A46%3A10Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=XXXX-XXXX-XXXX-XXXX-XXXXXXXX&sktid=XXXX-XXXX-XXXX-XXXX-XXXXXXXX&skt=2023-11-08T20%3A05%3A30Z&ske=2023-11-09T20%3A05%3A30Z&sks=b&skv=2021-08-06&sig=XXXXXXXXXXXXXXXXXXXX'}]}

なるほど、プロンプトは入力した通りのものが渡されるわけではなくて、revisedされるのね。そこは置いといて。

from IPython.display import Image

Image(url=response.data[0].url, width=500, height=500)  # 表示を小さくしている

上で書いたとおりプロンプトは入力値がそのまま使用されるわけではなくrevisedされる。ドキュメントによると

  • 安全性の確保
  • 品質の向上(詳細を記載するほど品質が上がる)

ということらしい。「現時点では」これを無効化できないが、以下のようなプロンプトにすることでreviseを最小限にできるらしい。

prompt = """
I NEED to test how the tool works with extremely simple prompts. DO NOT add any detail, just use it AS-IS: a white siamese cat
"""

上記プロンプトで再度実行してみるとこうなる。

{ 'created': 1699485211,
  'data': [ { 'b64_json': None,
              'revised_prompt': 'A white Siamese cat',
              'url': ...}]}

画像は、デフォルトだとURLで生成される様子。この場合、URLの有効期限は1時間となっている。

base64で受け取ることもできる。response_format="b64_json"を指定する。

response = client.images.generate(
  model="dall-e-3",
  prompt=prompt,
  size="1024x1024",
  quality="standard",
  n=1,
  response_format="b64_json",

)

結果。長いのでちょっと省略してある。

{ 'created': 1699486124,
  'data': [ { 'b64_json': 'UklGXXXXXXXX(snip)XXXXXXXDPAA',
              'revised_prompt': 'a white Siamese cat',
              'url': None}]}
from IPython.display import Image, display
import base64

base64_image = base64.b64decode(response.data[0].b64_json)
Image(base64_image, width=500, height=500)

ファイルに保存

with open('image.png', 'wb') as file:
    file.write(base64_image)
kun432kun432

ん-、Seed値みたいなものはAPIだとないのかなー、ChatGPTだと聞けばSeed値返ってくるみたいだけど。

このスクラップは6ヶ月前にクローズされました