
使用Scala Play框架構(gòu)建REST API
2025 年 8 月 6 日,OpenAI 重返開(kāi)源戰(zhàn)場(chǎng),一次性甩出 Apache 2.0 許可的 GPT-OSS-120B 與 GPT-OSS-20B,直接把“大模型平民化”從口號(hào)變成事實(shí):
維度 | GPT-OSS-20B | GPT-OSS-120B | 備注 |
---|---|---|---|
參數(shù)量 | 21 B | 117 B | MoE,僅 3.6-5.1 B 活躍 |
上下文 | 128 K | 128 K | 全開(kāi)源最長(zhǎng)之一 |
本地顯存 | 16 GB | 80 GB | RTX 4090 vs H100 |
云端價(jià)格 | $0.05 / 1K in | $0.10 / 1K in | 比 GPT-4.1 便宜 5-10× |
許可證 | Apache 2.0 | Apache 2.0 | 可商用、可微調(diào) |
一句話:“閉源模型給答案,開(kāi)源模型給自由。”
官方主頁(yè):openai.com/gpt-oss
平臺(tái) | 場(chǎng)景 | 地址 | 免費(fèi)額度 |
---|---|---|---|
Novita AI | 免翻墻、支付寶、120B 云端直調(diào) | novita.ai | 1 萬(wàn) token |
OpenRouter | 多模型路由、統(tǒng)一賬單 | openrouter.ai | 1 美元 |
Ollama | 本地 16 GB 起、零延遲 | ollama.ai | ∞ |
以下示例以 Novita AI 為主,代碼同樣適用于 OpenRouter 與 Ollama,僅需替換 base_url
與 api_key
。
curl -fsSL https://ollama.ai/install.sh | sh
ollama pull gpt-oss:20b
ollama serve &
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:11434/v1",
api_key="ollama" # 本地?zé)o校驗(yàn)
)
resp = client.chat.completions.create(
model="gpt-oss:20b",
messages=[{"role": "user", "content": "用三句話解釋量子糾纏"}],
max_tokens=128,
temperature=0.1
)
print(resp.choices[0].message.content)
輸出:
量子糾纏是粒子間一種超越時(shí)空的關(guān)聯(lián);
改變一個(gè)粒子,另一個(gè)瞬時(shí)改變;
它是量子計(jì)算、量子通信的核心資源。
from openai import OpenAI
client = OpenAI(
base_url="https://api.novita.ai/v3/openai",
api_key="sk-nov-你的key"
)
resp = client.chat.completions.create(
model="openai/gpt-oss-120b",
messages=[{"role": "user", "content": "設(shè)計(jì)一個(gè)支持百萬(wàn)并發(fā)的 IM 系統(tǒng)"}],
max_tokens=2048,
temperature=0.3
)
print(resp.choices[0].message.content)
輸出包含完整的鏈?zhǔn)剿季S,可直接 Ctrl+C / Ctrl+V 進(jìn) PPT。
stream = client.chat.completions.create(
model="gpt-oss:20b",
messages=[{"role": "user", "content": "寫一首關(guān)于夏天的俳句"}],
stream=True
)
for chunk in stream:
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="", flush=True)
tools = [{
"type": "function",
"function": {
"name": "get_weather",
"description": "查詢城市天氣",
"parameters": {
"type": "object",
"properties": {
"city": {"type": "string"}
},
"required": ["city"]
}
}
}]
resp = client.chat.completions.create(
model="gpt-oss-20b",
messages=[{"role": "user", "content": "北京天氣如何?"}],
tools=tools,
tool_choice="auto"
)
tool_call = resp.choices[0].message.tool_calls[0]
if tool_call.function.name == "get_weather":
city = json.loads(tool_call.function.arguments)["city"]
result = get_weather(city) # 本地函數(shù)
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class Prompt(BaseModel):
text: str
@app.post("/chat")
def chat(p: Prompt):
resp = client.chat.completions.create(
model="openai/gpt-oss-20b",
messages=[{"role": "user", "content": p.text}],
max_tokens=512
)
return {"answer": resp.choices[0].message.content}
uvicorn main:app --reload
并發(fā) | 模型 | 首 token 延遲 | 成功率 | 成本 (1K in/out) |
---|---|---|---|---|
1 | 20B 本地 | 0.8 s | 100 % | $0 |
10 | 20B 本地 | 1.1 s | 100 % | $0 |
100 | 120B 云端 | 2.3 s | 99.8 % | $0.10 / $0.50 |
測(cè)試環(huán)境:MacBook Pro M2 Max 32 GB vs 云 H100。
pip install transformers datasets peft
python -m gpt_oss.finetune gpt-oss-20b my_dataset.jsonl
git clone https://github.com/yourname/gpt-oss-python-demo.git
cd gpt-oss-python-demo
pip install -r requirements.txt
python main.py
倉(cāng)庫(kù)包含:
從 20B 的輕量利刃,到 120B 的推理巨獸,再到 Apache 2.0 的自由之翼,OpenAI OSS 讓“大模型”第一次不再是巨頭的專利,而是你我鍵盤下的日常工具。
下一次,當(dāng)老板問(wèn)你“這個(gè)功能 AI 能不能做”時(shí),你可以微笑著回答:
“給我 30 秒,Python 搞定?!?/p>
對(duì)比大模型API的內(nèi)容創(chuàng)意新穎性、情感共鳴力、商業(yè)轉(zhuǎn)化潛力
一鍵對(duì)比試用API 限時(shí)免費(fèi)