主题
图片生成
POST /v1/chat/completions
使用 Gemini 3.1 Flash Image 根据文本生成图片。接口采用 OpenAI Chat Completions 兼容格式,图片以 JPEG Base64 Data URL 返回。
图片生成只支持
smt_live_*API Key,不支持 Sandbox Key。
模型与限制
| 项目 | 值 |
|---|---|
| 对外模型 | gemini-3.1-flash-image |
stream | 必须为 false |
n | 首版固定为 1 |
| 默认尺寸 | 1024x1024 |
| 最大尺寸 | 4K |
| 返回格式 | choices[0].message.content 中的 data:image/jpeg;base64,... |
尺寸采用 宽x高 格式。平台按最长边映射到 0.5K、1K、2K 或 4K 计费档位。
计费
SeerMarTech 的图片生成价格为 Google Gemini 3.1 Flash Image 官方价格的 40%,再按请求发生时的美元兑人民币汇率结算。
| 分辨率档位 | Google 官方图片价格 | SeerMarTech 图片价格(USD 等值) |
|---|---|---|
| 0.5K | $0.045 / 张 | $0.0180 / 张 |
| 1K | $0.067 / 张 | $0.0268 / 张 |
| 2K | $0.101 / 张 | $0.0404 / 张 |
| 4K | $0.151 / 张 | $0.0604 / 张 |
文本输入以及模型产生的文本/思考输出同样按对应 Google 官方 token 价格的 40% 计费。最终人民币扣费以响应头 X-SeerMarTech-Charge-CNY 为准。
请求示例
curl
bash
curl -X POST "https://api.seermartech.cn/v1/chat/completions" \
-H "Authorization: Bearer smt_live_你的Key" \
-H "Content-Type: application/json" \
-d '{
"model": "gemini-3.1-flash-image",
"messages": [
{
"role": "user",
"content": "生成一张简约的蓝色圆形品牌插画,白色背景,不要文字"
}
],
"stream": false,
"size": "1024x1024",
"n": 1
}'Python
python
import base64
import re
import requests
response = requests.post(
"https://api.seermartech.cn/v1/chat/completions",
headers={
"Authorization": "Bearer smt_live_你的Key",
"Content-Type": "application/json",
},
json={
"model": "gemini-3.1-flash-image",
"messages": [{"role": "user", "content": "生成一张简约的蓝色圆形品牌插画"}],
"stream": False,
"size": "1024x1024",
"n": 1,
},
timeout=180,
)
response.raise_for_status()
content = response.json()["choices"][0]["message"]["content"]
match = re.search(r"data:image/[^;]+;base64,([A-Za-z0-9+/=]+)", content)
if not match:
raise RuntimeError("响应中没有图片")
with open("generated.jpg", "wb") as image_file:
image_file.write(base64.b64decode(match.group(1)))TypeScript
typescript
import { writeFile } from "node:fs/promises";
const response = await fetch("https://api.seermartech.cn/v1/chat/completions", {
method: "POST",
headers: {
Authorization: "Bearer smt_live_你的Key",
"Content-Type": "application/json",
},
body: JSON.stringify({
model: "gemini-3.1-flash-image",
messages: [{ role: "user", content: "生成一张简约的蓝色圆形品牌插画" }],
stream: false,
size: "1024x1024",
n: 1,
}),
});
if (!response.ok) throw new Error(await response.text());
const data = await response.json();
const content = data.choices[0].message.content as string;
const match = content.match(/data:image\/[^;]+;base64,([A-Za-z0-9+/=]+)/);
if (!match) throw new Error("响应中没有图片");
await writeFile("generated.jpg", Buffer.from(match[1], "base64"));响应示例
json
{
"object": "chat.completion",
"model": "gemini-3.1-flash-image-preview",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": ""
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 23,
"completion_tokens": 1391,
"total_tokens": 1414
}
}响应中的 model 是供给端路由 ID,调用时始终使用稳定的对外模型名 gemini-3.1-flash-image。
超时与重试
- 图片生成通常比文本响应慢,客户端超时建议至少设置为 180 秒。
- 网络超时后不要立即无限重试;建议等待并使用新的业务请求标识重试一次。
- 图片结果可能较大,应避免在日志或数据库中保存完整 Base64。
常见错误
| HTTP | 场景 | 处理建议 |
|---|---|---|
| 400 | stream=true、n 不为 1、尺寸非法 | 按本文限制修改参数 |
| 402 | 余额不足以覆盖本次最大预估费用 | 充值后重试 |
| 403 | 使用 Sandbox Key | 改用 smt_live_* Key |
| 429 | 图片生成并发或速率超限 | 稍后重试 |
| 502 | 上游暂不可用或计费信息不完整 | 稍后重试并保留请求 ID |