Skip to content

OpenAI Python SDK 接入

bash
pip install --upgrade openai

对话

python
from openai import OpenAI
client = OpenAI(base_url="https://iliu.ai/v1", api_key="sk-xxxxxxxx")
resp = client.chat.completions.create(
    model="gpt-5.1",
    messages=[{"role": "user", "content": "你好"}],
)
print(resp.choices[0].message.content)

流式

python
stream = client.chat.completions.create(model="gpt-5.1", messages=[{"role":"user","content":"讲个笑话"}], stream=True)
for chunk in stream:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

图像

python
import base64
import httpx


def save_image(item, path):
    if item.b64_json:
        open(path, "wb").write(base64.b64decode(item.b64_json))
    elif item.url:
        open(path, "wb").write(httpx.get(item.url).content)


resp = client.images.generate(
    model="gpt-image-2",
    prompt="一张极简风格的玻璃香水瓶,透明背景",
    size="1024x1024",
    quality="high",
    background="transparent",
    output_format="png",
    n=1,
)
save_image(resp.data[0], "out.png")

gpt-image-2 同步返回结果,无需轮询;客户端超时建议至少 180 秒。每项结果可能提供 b64_json 或临时 url,应同时处理两种形式。

图像编辑

本地文件使用 multipart,重复传入 image 可进行多图参考,最多 16 张、单文件不超过 50 MB。gpt-image-2 / gpt-image-2.5-sunburst / gpt-image-2.5-flarePOST /v1/images/edits

python
with open("person.png", "rb") as person, open("clothing.png", "rb") as clothing:
    resp = client.images.edit(
        model="gpt-image-2",
        image=[person, clothing],
        prompt="让第一张图片中的人物穿上第二张图片中的服装,保留脸部、姿势和背景",
        size="1024x1024",
        input_fidelity="high",
    )
save_image(resp.data[0], "out.png")

无后缀 gpt-image-2.5 的图生图/编辑不要走 /v1/images/edits,也不要传 images[].image_url.url。改用 POST /v1/images/generations,字段是 image(URL 字符串数组):

python
import httpx

resp = httpx.post(
    "https://iliu.ai/v1/images/generations",
    headers={"Authorization": "Bearer sk-xxxxxxxx"},
    json={
        "model": "gpt-image-2.5",
        "prompt": "帮我把这个图片里面人物的衣服换成红色",
        "image": [
            "https://img.shetu66.com/2022/08/31/1661927098745582.jpg"
        ],
        "size": "1024x1024",
        "quality": "auto",
        "background": "transparent",
    },
    timeout=180,
)
resp.raise_for_status()
item = resp.json()["data"][0]
if item.get("b64_json"):
    open("out.png", "wb").write(base64.b64decode(item["b64_json"]))
elif item.get("url"):
    open("out.png", "wb").write(httpx.get(item["url"]).content)

gpt-image-2 等型号的公网 URL 参考图仍走 POST /v1/images/edits,JSON 字段是 images[].image_url.url,不要把 URL 写进 image

视频(httpx)

python
import httpx, time
BASE="https://iliu.ai"; KEY="sk-xxxxxxxx"
r = httpx.post(f"{BASE}/v1/videos", headers={"Authorization":f"Bearer {KEY}"}, json={"model":"veo_3_1-fast","prompt":"雪山日出","seconds":"5","size":"1280x720"})
tid = r.json()["task_id"]
while True:
    time.sleep(8)
    s = httpx.get(f"{BASE}/v1/videos/{tid}", headers={"Authorization":f"Bearer {KEY}"}).json()
    if s["status"] in ("completed","failed"): break
url = s.get("url") or s.get("video_url")
data = httpx.get(url).content if url else httpx.get(f"{BASE}/v1/videos/{tid}/content", headers={"Authorization":f"Bearer {KEY}"}).content
open("out.mp4","wb").write(data)

配置

所有兼容 OpenAI 的 SDK 和工具,只需改两处:

  • Base URLhttps://iliu.ai/v1
  • API Keysk-xxxxxxxx

相关链接

  • Node 版:见「OpenAI Node.js SDK 接入」
  • 错误码与重试:见「帮助中心 → 错误码大全」