把模型接进后端服务
概览
用 Python FastAPI 包一层,前端只调自己的接口,不暴露 API Key。
| 项 | 说明 |
|---|---|
| 语言 | Python / Node 均可 |
| 要点 | Key 只在服务端 |
| 耗时 | 30 分钟 |
操作步骤
- 装 openai SDK
- 把 base_url 指向我们
- 写一个 /chat 接口
- 加超时与重试
- 前端调自己的 /chat
示例
示例
from fastapi import FastAPI
from openai import OpenAI
app = FastAPI()
client = OpenAI(api_key="sk-shsl-...", base_url="https://yun.nmgshenghang.cn/v1")
@app.post("/chat")
def chat(q: str):
r = client.chat.completions.create(
model="glm-5.3",
messages=[{"role": "user", "content": q}],
timeout=60)
return {"text": r.choices[0].message.content}使用限制
- 不要在浏览器里直接用 Key
注意事项
- 服务端要设超时 60 秒,流式要透传 SSE