
使用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 的大腦。