HEIHUZI_API_KEY 读取。SDK Base URL 为 https://code.heihuzi.ai/v1。
实测版本:Python openai 2.32.0;JavaScript openai 7.13.0。 在自己的 Python / Node.js 项目中安装对应版本后运行。文本与图片示例分别使用具备对应能力的 Key。
JavaScript 文本
保存为.mjs 文件运行,2026-09-13 实测返回 API_OK。示例使用 .asResponse() 取得原始响应,再显式解析 JSON。本次测试发现部分非流式 JSON 响应的 Content-Type 为 text/event-stream,JavaScript SDK 的自动解析会因此失败,故使用下面已通过的读取方式。
import OpenAI from "openai";
const client = new OpenAI({
apiKey: process.env.HEIHUZI_API_KEY,
baseURL: "https://code.heihuzi.ai/v1",
timeout: 600000,
maxRetries: 0
});
const raw = await client.responses.create({
model: "gpt-5.5",
input: [{ role: "user", content: "Reply with exactly API_OK." }]
}).asResponse();
const response = await raw.json();
if (response.error || response.status !== "completed") {
throw new Error(JSON.stringify(response.error ?? { status: response.status }));
}
const text = (response.output ?? [])
.filter(item => item.type === "message")
.flatMap(item => item.content ?? [])
.filter(item => item.type === "output_text")
.map(item => item.text)
.join("");
if (!text) throw new Error("No text returned");
console.log(text);
Python 生成并保存 PNG
import base64
import os
from pathlib import Path
from openai import OpenAI
client = OpenAI(
api_key=os.environ["HEIHUZI_API_KEY"],
base_url="https://code.heihuzi.ai/v1",
timeout=600.0,
max_retries=0,
)
result = client.images.generate(
model="gpt-image-2.5-flare",
prompt="A simple blue ceramic cup on a white background.",
n=1,
size="1024x1024",
quality="low",
output_format="png",
)
if not result.data:
raise RuntimeError("No image returned")
for index, item in enumerate(result.data, 1):
if not item.b64_json:
raise RuntimeError("Missing b64_json")
Path(f"generated-{index}.png").write_bytes(base64.b64decode(item.b64_json))
print("Saved", len(result.data), "image(s)")
Python 上传参考图编辑
将测试参考图保存为reference.png,放在运行目录。
import base64
import os
from pathlib import Path
from openai import OpenAI
client = OpenAI(
api_key=os.environ["HEIHUZI_API_KEY"],
base_url="https://code.heihuzi.ai/v1",
timeout=600.0,
max_retries=0,
)
with open("reference.png", "rb") as image:
result = client.images.edit(
model="gpt-image-2.5-sunburst",
image=image,
prompt="Change the blue cup to a bright red cup. Keep the cup shape and the white background.",
n=1,
size="1024x1024",
quality="low",
output_format="png",
)
if not result.data:
raise RuntimeError("No image returned")
for index, item in enumerate(result.data, 1):
if not item.b64_json:
raise RuntimeError("Missing b64_json")
Path(f"edited-{index}.png").write_bytes(base64.b64decode(item.b64_json))
print("Saved", len(result.data), "image(s)")
JavaScript 生成并保存 PNG
import OpenAI from "openai";
import { writeFile } from "node:fs/promises";
const client = new OpenAI({
apiKey: process.env.HEIHUZI_API_KEY,
baseURL: "https://code.heihuzi.ai/v1",
timeout: 600000,
maxRetries: 0
});
const result = await client.images.generate({
model: "gpt-image-2.5-flare",
prompt: "A simple blue ceramic cup on a white background.",
n: 1,
size: "1024x1024",
quality: "low",
output_format: "png"
});
if (!result.data?.length) throw new Error("No image returned");
for (const [index, item] of result.data.entries()) {
if (!item.b64_json) throw new Error("Missing b64_json");
await writeFile(`generated-js-${index + 1}.png`, Buffer.from(item.b64_json, "base64"));
}
console.log("Saved", result.data.length, "image(s)");