Chuyển đến nội dung chính

第 10 課:DALL-E 3 API — 整合 OpenAI 影像生成

DALL-E 3 架構概觀。 OpenAI 圖像 API:生成、編輯、變化。提示 DALL-E 的最佳實踐。速率限制和定價優化。與 Web 應用程式的整合模式。

🧠 人工智慧與機器學習 — 第 9 課 第 10 課:DALL-E 3 API — 整合 OpenAI 影像生成

生成式 AI:使用 AI 創建圖像和視頻

第 4 部分:DALL-E、中途和商業 API

亞洲開發網

簡介

DALL-E 3 是 OpenAI 最強大的圖像生成模型 - 與 ChatGPT 深度集成,很好地理解自然提示,並具有可用於生產的 API。本文指導如何將 DALL-E 3 API 整合到實際應用程式中。


1.OpenAI 圖片 API

from openai import OpenAI

client = OpenAI()

# Text-to-Image
response = client.images.generate(
    model="dall-e-3",
    prompt="A white siamese cat sitting on a windowsill, watercolor painting",
    size="1024x1024",      # 1024x1024, 1024x1792, 1792x1024
    quality="standard",     # standard, hd
    style="vivid",          # vivid, natural
    n=1,
)

image_url = response.data[0].url
revised_prompt = response.data[0].revised_prompt  # DALL-E rewrites prompt

尺寸和定價

尺寸品質價格
1024×1024標準~$0.04
1024×1024高清~$0.08
1024×1792標準~$0.08
1792×1024標準~$0.08

2. 影像編輯 (DALL-E 2)

# Edit specific region of image
response = client.images.edit(
    model="dall-e-2",
    image=open("original.png", "rb"),
    mask=open("mask.png", "rb"),  # transparent area = edit zone
    prompt="a golden retriever sitting naturally",
    size="1024x1024",
    n=1,
)

影像變化

# Generate variations of existing image
response = client.images.create_variation(
    model="dall-e-2",
    image=open("cat.png", "rb"),
    size="1024x1024",
    n=4,  # 4 variations
)

3.下載並儲存映像

import httpx
from pathlib import Path

async def generate_and_save(prompt, output_path, client):
    """Generate image and save to disk"""
    response = client.images.generate(
        model="dall-e-3",
        prompt=prompt,
        size="1024x1024",
        quality="hd",
        response_format="b64_json",  # get base64 instead of URL
    )

    # From base64
    import base64
    image_data = base64.b64decode(response.data[0].b64_json)
    Path(output_path).write_bytes(image_data)

    return response.data[0].revised_prompt


# Or download from URL
def download_image(url, output_path):
    response = httpx.get(url)
    Path(output_path).write_bytes(response.content)

4. 提示 DALL-E 3 的最佳實踐

# DALL-E 3 hiểu ngôn ngữ tự nhiên tốt hơn SD
# Viết mô tả chi tiết như đang nói chuyện

# ❌ SD-style prompt (keyword spam)
bad_prompt = "cat, digital art, 4k, trending on artstation, highly detailed"

# ✅ DALL-E 3 prompt (natural description)
good_prompt = """
A fluffy white cat sitting on a velvet cushion in a cozy library.
The cat is wearing tiny round glasses and looking at an open book.
Warm golden afternoon light streams through tall windows.
Style: detailed watercolor illustration with soft colors.
"""

# DALL-E 3 tự động rewrite prompt → check revised_prompt
print(f"Original: {prompt}")
print(f"Revised: {response.data[0].revised_prompt}")

5. 整合模式-FastAPI 伺服器

from fastapi import FastAPI, HTTPException
from pydantic import BaseModel
from openai import OpenAI
import base64

app = FastAPI()
client = OpenAI()

class ImageRequest(BaseModel):
    prompt: str
    size: str = "1024x1024"
    quality: str = "standard"
    style: str = "vivid"

class ImageResponse(BaseModel):
    image_url: str
    revised_prompt: str

@app.post("/generate", response_model=ImageResponse)
async def generate_image(req: ImageRequest):
    try:
        response = client.images.generate(
            model="dall-e-3",
            prompt=req.prompt,
            size=req.size,
            quality=req.quality,
            style=req.style,
        )
        return ImageResponse(
            image_url=response.data[0].url,
            revised_prompt=response.data[0].revised_prompt,
        )
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

6. 速率限制與成本最佳化

import asyncio
from collections import deque
import time

class RateLimiter:
    """Simple rate limiter for API calls"""
    def __init__(self, max_calls=5, period=60):
        self.max_calls = max_calls
        self.period = period
        self.calls = deque()

    async def acquire(self):
        now = time.time()
        while self.calls and self.calls[0] < now - self.period:
            self.calls.popleft()
        if len(self.calls) >= self.max_calls:
            sleep_time = self.calls[0] + self.period - now
            await asyncio.sleep(sleep_time)
        self.calls.append(time.time())

# Usage
limiter = RateLimiter(max_calls=5, period=60)

async def safe_generate(prompt):
    await limiter.acquire()
    return client.images.generate(model="dall-e-3", prompt=prompt)

7. 安全與內容政策

# DALL-E 3 có built-in safety filters
# Tự động reject: violence, explicit content, real people

# Handle content policy errors
from openai import BadRequestError

try:
    response = client.images.generate(
        model="dall-e-3",
        prompt=user_prompt,
    )
except BadRequestError as e:
    if "content_policy" in str(e):
        print("Prompt vi phạm content policy")
    elif "billing" in str(e):
        print("Hết quota hoặc billing issue")
    raise

總結

特點達爾-E 3達爾-E 2
品質優好
隨時了解自然語言關鍵字
編輯沒有是(面罩)
變化沒有是的
最大解析度1792×10241024×1024
及時重寫汽車沒有

📌 下一篇: Midjourney、Flux 和新興模型 — 比較平台。