UniApp企业办公:把审批、客户和项目装进手机,能做些什么?
2026/10/28 10:19:51
生成式AI的浪潮从文本卷到图像,再到音视频,而ChatGPT-4o(omni)是OpenAI第一次把“原生图像输出”塞进对话模型里。与DALL·E 3相比,4o把图像token当成普通token一起参与自回归生成,不再走“先文本后图像”的级联路线。好处是:
对开发者而言,这意味着可以把“生图”当成普通补全请求,无需额外接入DALL·E接口,维护一套密钥即可。
images.generate端点,结果返回404size、quality、style、n到底怎么组合?官方文档只给“推荐值”,没有量化指标下面用一段可运行代码把这些问题一次讲透。
python -m venv venv source venv/bin/activate pip install -U openai python-dotenv pillow tenacity.env文件里只放一行:
OPENAI_API_KEY="sk-xxxxxxxx"import os import base64 import time from io import BytesIO from pathlib import Path import openai from dotenv import load_dotenv from PIL import Image from tenacity import retry, stop_after_attempt, wait_exponential load_dotenv() client = openai.OpenAI( api_key=os.getenv("OPENAI_API_KEY"), max_retries=0, # 我们自己做重试,更透明 ) @retry(stop=stop_after_attempt(3), wait=wait_exponential(multiplier=1, min=2, max=10)) def generate_image(prompt: str, size: str = "1024x1024", quality: str = "hd") -> Image.Image: """ 调用ChatGPT-4o生图并返回PIL.Image对象 参数: prompt: 提示词,长度<=1000字符 size: 仅支持 1024x1024、1792x1024、1024x1792 quality: 标准"standard"或高质量"hd",hd贵2×token """ try: response = client.chat.completions.create( model="gpt-4o", # 关键:用gpt-4o而非dall-e-3 messages=[ { "role": "user", "content": [ {"type": "text", "text": prompt}, # 如果想给参考图,可再塞一个{"type": "image_url", "image_url": {"url": ...}} ], } ], size=size, quality=quality, n=1, response_format="b64_json", # 强制返回base64 ) except openai.BadRequestError as e: # 把官方错误信息直接抛出来,方便定位 print(f"[BadRequest] {e}") raise # 4o把图像当message.content返回 msg_content = response.choices[0].message.content # content是list,取第一个image_token image_b64 = None for item in msg_content: if item.get("type") == "image": image_b64 = item["image"]["b64"] break if not image_b64: raise RuntimeError("No image token found in response") image_data = base64.b64decode(image_b64) return Image.open(BytesIO(image_data)) if __name__ == "__main__": prompt = "A futuristic coffee shop on Mars, warm lighting, digital menu board, 4K style" t0 = time.time() img = generate_image(prompt, size="1792x1024", quality="hd") print(f"Done in {time.time() - t0:.2f}s") img.save("mars_cafe.png") print("Image saved to mars_cafe.png")| 参数 | 可选值 | 对token/价格影响 |
|---|---|---|
| size | 1024×1024、1792×1024、1024×1792 | 越大越贵 |
| quality | standard、hd | hd≈2×token |
| n | 1–4 | 线性倍增 |
| response_format | url、base64 | 不影响价格,建议base64省一次GET |
如果生成分镜故事板,一次返回4张图,可循环message.content数组,把type=="image"的全部落盘,并本地生成filename_001.png序号。
分辨率取舍
1024×1024单张约消耗260 token;1792×1024约460 token。产品初期可用小尺寸做缩略图,用户点击“下载高清”时再调一次hd接口。
并发与限速
官方默认60 RPM/10k TPM。图像token也算在TPM里,一次hd大图≈460 T。超出后返回429,可用tenacity做指数退避。
缓存+提示词模板
把高频场景(头像、海报、商品图)提示词模板化,再加一层Redis缓存(key=hash(prompt+size+quality)),可节省30%以上调用量。
sexual/minors或hate置信度>0.8直接拒绝images/edits做二次审核,防止漏网daily_quota=50,超出返回402 Payment Required,避免Key被刷image_token字段偶尔带\n,需先strip()再b64decode。response_format="url",地址有效期仅1小时,下载后尽快转存S3。1024*1024(乘号)会报invalid size,一定用小写x。400,先用LLM把prompt压缩成关键词再调用。image_url字段,保持上下文一致?如果你希望亲手搭一个能实时语音对话的AI角色,而不仅停留在静态生图,可以试试这个动手实验:从0打造个人豆包实时通话AI。实验把ASR、LLM、TTS串成一条低延迟链路,让你用30行代码就能在浏览器里跟AI“打电话”。我这种语音小白也能跑通,顺便把火山引擎的免费额度薅了个够,值得一试。