一次调用产出多语言结构化结果
场景:同一款商品,要批量产出多语言标题、卖点、主图描述——手工写慢,逐条调用贵。
核心思路:一次请求返回多语言结构化结果(JSON),而不是分三次调用。省两次往返,token 更省、速度更快。
import json, urllib.request
KEY = "你的Key"
BASE = "https://starseaapi.com/v1"
MODEL = "glm-5.3-flash" # 实测:¥0.4 / ¥1.4 每百万字,原生多模态,适合文案
def multi_lang_copy(product: str, langs=("zh", "en", "ja")):
"""一次调用生成多语言标题"""
prompt = (f'为产品「{product}」生成电商标题,语言代码 {"、".join(langs)},'
f'各不超过 20 字/词。只返回 JSON,键为语言代码,不要 markdown 代码块。')
body = {
"model": MODEL,
"messages": [{"role": "user", "content": prompt}],
"max_tokens": 600,
"thinking": {"type": "disabled"}, # 🔴 关键:见下方「必读」
}
req = urllib.request.Request(
BASE + "/chat/completions",
data=json.dumps(body).encode(),
headers={"Authorization": "Bearer " + KEY, "Content-Type": "application/json"})
r = json.loads(urllib.request.urlopen(req, timeout=90).read())
return json.loads(r["choices"][0]["message"]["content"])
print(multi_lang_copy("便携榨汁杯"))
# {'zh': '便携榨汁杯,随行鲜榨,畅享活力', 'en': 'Portable Juicer Cup, Fresh Juice On the Go', 'ja': '...'}
实测输出(2026-10-03,真实网关返回):
{"zh":"便携榨汁杯,随行鲜榨,畅享活力","en":"Portable Juicer Cup, Fresh Juicing Anywhere","ja":"..."}
实测对照(同一问题「回答一个字:好」,模型 glm-4.7):
| max_tokens | 思考消耗 | 正文长度 | finish | 结果 |
|---|---|---|---|---|
| 20 | 18 | 0 | length | ❌ 空回复 |
| 100 | 73 | 1 | stop | ✅ 正常 |
| 800 | 198 | 1 | stop | ✅ 正常(但浪费) |
原因:GLM、DeepSeek 等推理型模型先输出思考链,思考也计入 max_tokens。预算不够 → 正文被截断为 0。
解决(二选一):
"thinking": {"type": "disabled"} # 推荐:关闭思考,正文完整,还更快更省
或给足预算(max_tokens ≥ 500)。
实测收益:关闭思考后,glm-4.7 单次响应从 5.2s → 1.2s,且 reasoning_tokens 归零(这部分同样计费)。
注意:
thinking: {"type":"disabled"}是实测唯一有效的写法。enable_thinking=false、reasoning_effort=low在部分模型上无效。
多语言 JSON 的字符数比单语多 2–3 倍。max_tokens 建议 ≥ 600,否则 JSON 会被截断成非法格式。
真实业务里 SKU 有几百上千个,需要控制成本与失败重试:
import json, time, urllib.request, urllib.error
from concurrent.futures import ThreadPoolExecutor
KEY, BASE = "你的Key", "https://starseaapi.com/v1"
MODEL = "glm-5.3-flash"
CNY_IN, CNY_OUT = 0.4, 1.4 # 每百万字,用于成本统计
def translate_batch(items, workers=4):
"""items: [{'sku':'A001','title':'便携榨汁杯'}, ...]"""
def one(it):
body = {"model": MODEL,
"messages": [{"role": "user", "content":
f'把商品名翻译成英语、日语、德语,只返回 JSON:{{"en":"","ja":"","de":""}}。商品名:{it["title"]}'}],
"max_tokens": 400, "thinking": {"type": "disabled"}}
req = urllib.request.Request(BASE + "/chat/completions",
data=json.dumps(body).encode(),
headers={"Authorization": "Bearer " + KEY, "Content-Type": "application/json"})
for attempt in range(3): # 重试 3 次
try:
r = json.loads(urllib.request.urlopen(req, timeout=60).read())
u = r["usage"]
cost = (u["prompt_tokens"]*CNY_IN + u["completion_tokens"]*CNY_OUT) / 1e6
return {"sku": it["sku"], **json.loads(r["choices"][0]["message"]["content"]),
"_cost_cny": round(cost, 6)}
except Exception as e:
if attempt == 2:
return {"sku": it["sku"], "error": str(e)[:80]}
time.sleep(2 ** attempt)
with ThreadPoolExecutor(max_workers=workers) as ex:
return list(ex.map(one, items))
if __name__ == "__main__":
items = [{"sku": f"A{i:03d}", "title": t} for i, t in enumerate(
["便携榨汁杯", "不锈钢保温杯", "折叠收纳箱", "无线充电板"])]
res = translate_batch(items)
total = sum(r.get("_cost_cny", 0) for r in res)
for r in res:
print(r)
print(f"\n合计成本:¥{total:.4f}({len(items)} 个 SKU)")
实测成本(真实网关 usage): - 单条翻译任务:输入 ≈87 token / 输出 ≈66 token → 约 ¥0.0001/条 - 4 条并发:约 ¥0.0004,耗时约 3 秒
换算到真实规模:1000 个 SKU 的多语翻译,约 ¥0.1–0.5(视文本长度)。这比逐条人工处理的成本低数个量级。
电商主图可用站内文生图模型(gpt-image-2.5-flare,约 ¥0.456/次)。先用文本模型生成画面描述:
def image_prompt(product, style="电商主图,简洁背景"):
body = {"model": "qwen3.8-flash",
"messages": [{"role": "user", "content":
f'为「{product}」写一条画面描述(40字内),用于文生图。风格:{style}。只输出描述本身。'}],
"max_tokens": 200, "thinking": {"type": "disabled"}}
req = urllib.request.Request(BASE + "/chat/completions",
data=json.dumps(body).encode(),
headers={"Authorization": "Bearer " + KEY, "Content-Type": "application/json"})
return json.loads(urllib.request.urlopen(req, timeout=60).read())["choices"][0]["message"]["content"]
print(image_prompt("不锈钢保温杯"))
实测输出:不锈钢保温杯居中,金属质感,水珠与冰块环绕,浅色背景,电商主图
| 用途 | 推荐模型 | 价格(每百万字) | 理由 |
|---|---|---|---|
| 主文案生成 | glm-5.3-flash |
¥0.4 / ¥1.4 | 成本极低,原生多模态 |
| 高要求创意 | claude-sonnet-5 |
¥13 / ¥65 | 语言质量更高 |
| 极低成本批量 | doubao-seed-2.0-mini |
¥0.2 / ¥2 | 价格处于低位档 |
| 主图生成 | gpt-image-2.5-flare |
¥0.456/次 | 延迟降低 50% |
| 结构调整 | doubao-seed-2.1-lite |
¥0.8 / ¥2.7 | 1M 上下文,多模态 |
| 项 | 结果 |
|---|---|
| 多语言 JSON 输出 | ✅ 三语完整,JSON 合法 |
| 关闭思考后响应 | ✅ 2.4s(未关闭时超时风险高) |
| 翻译任务 token 消耗 | ✅ 输入 87 / 输出 66 |
| 主图描述生成 | ✅ 40 字内,符合文生图格式 |
| 并发 4 线程 | ✅ 无报错,总耗时约 3s |
选型提示:文案类任务务必关闭思考——这类任务不需要推理链,关闭后速度提升约 4 倍、成本降低且不会截断。
1. 在 Key 服务台创建一把 Key
2. 把代码里的 你的Key 替换为实际 Key
3. 直接运行即可
每篇配方均经真实调用实测 · 返回配方库 · 前往 Key 服务台