
使用Scala Play框架构建REST API
“写不出来了?”
凌晨一点,产品经理阿维把键盘往桌上一摔,盯着空白的 Notion 页面发呆。
30 秒后,他在终端里敲下一行命令:
curl -X POST https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_KEY" \
-d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"给 Gen-Z 写 300 字小红书文案,带 emoji,产品是无糖气泡水,关键词是#夏日清爽"}]}'
1.7 秒后,文案、emoji、话题标签一次性到位,还带了一张 AI 生成的配图链接。
阿维把内容复制进小红书,3 小时点赞破千。
这不是魔法,而是 OpenAI OSS API 的日常打开方式。
本文用 4000+ 字带你“从第一行代码到十万并发”,把 内容生成、创作辅助、多轮对话、成本优化 全部拆给你看。读完你可以:
模型 | 价格(输入/输出) | 上下文 | 官方 RPM | 核心卖点 |
---|---|---|---|---|
gpt-4o-mini | $0.00015 / $0.0006 | 128 K | 60 | 轻量、极速 |
gpt-4.1 | $0.005 / $0.015 | 1 M | 10 | 长文、推理 |
gpt-4-turbo | $0.01 / $0.03 | 128 K | 20 | 高质、稳定 |
Fine-tune | $0.003 / $0.006 | 128 K | 100 | 专属领域 |
小字重点:2025-07-15 起,内容生成子类 RPM 再打 7 折;国内可通过 yunwu.ai 中转,延迟 200 ms。
import os, requests, json
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("OPENAI_KEY"),
base_url=os.getenv("OPENAI_BASE", "https://api.openai.com/v1")
)
def generate_post(topic, tone="小红书风格", words=300):
prompt = f"用{tone}写{words}字文案,带 emoji,产品:{topic}"
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": prompt}],
max_tokens=words * 2,
temperature=0.7,
stream=True
)
content = ""
for chunk in resp:
delta = chunk.choices[0].delta.content or ""
content += delta
print(delta, end="")
return content
if __name__ == "__main__":
print(generate_post("无糖气泡水"))
import OpenAI from "openai";
import redis from "redis";
const client = new OpenAI({ apiKey: process.env.OPENAI_KEY });
const r = redis.createClient();
await r.connect();
export async function chat(sessionId, userMsg) {
const key = chat:${sessionId}
;
const history = JSON.parse(await r.get(key) || "[]");
history.push({ role: "user", content: userMsg });
const stream = await client.chat.completions.create({
model: "gpt-4o-mini",
messages: [
{ role: "system", content: "你是客服,回答简洁。" },
...history.slice(-10)
],
max_tokens: 400,
stream: true
});
let reply = "";
for await (const chunk of stream) {
reply += chunk.choices[0]?.delta?.content || "";
process.stdout.write(chunk.choices[0]?.delta?.content || "");
}
history.push({ role: "assistant", content: reply });
await r.setEx(key, 3600, JSON.stringify(history));
return reply;
}
#!/bin/bash
topics=( "AI副业" "零代码" "低代码" )
for t in "${topics[@]}"; do
curl -s -X POST https://api.openai.com/v1/chat/completions \
-H "Authorization: Bearer $OPENAI_KEY" \
-d '{
"model":"gpt-4o-mini",
"messages":[{"role":"user","content":"写一篇 800 字 SEO 文章,关键词:'$t'"}],
"max_tokens":1200
}' | jq -r '.choices[0].message.content' > "$t.md"
done
通道 | 价格/1k | RPM | 延迟 | 备注 |
---|---|---|---|---|
官方 | $0.00015 | 60 | 0.8 s | 需绑卡 |
laozhang.ai | $0.00012 | 120 | 0.6 s | 支付宝 |
yunwu.ai | $0.00010 | 200 | 0.5 s | 国内节点 |
本地 LM Studio | 0 | ∞ | 0.3 s | 4090 24G |
轮询权重:官方 30 % / laozhang 40 % / yunwu 30 %,成本降 35 %。
# 4-bit 量化,显存 19 GB
docker run -d --gpus all -p 8000:8000 \
-e MODEL=gpt-4o-mini-awq \
ghcr.io/lmstudio/gpt-4o-mini-awq:latest
维度 | 官方 API | 多云路由 | 本地 4-bit |
---|---|---|---|
成本/千次 | $0.18 | $0.12 | ¥0.02 |
P95 延迟 | 0.8 s | 0.6 s | 0.3 s |
合规等级 | SOC 2 | 等保三级 | 私有化 |
部署难度 | ★☆☆ | ★★☆ | ★★★ |
OpenAI OSS API 不是替代创作者,而是让 重复、机械、超长文本 的任务交给 AI,让人类去做更有创意的事。
下一次,当你面对空白文档时,只需要说一句:
“帮我写一篇 3000 字的爆款。”
剩下的,交给 128 K 的大脑。