Python OpenAI
The official SDK, streaming, structured output, and cost control basics.
Key PointsKeep the API key in the environment, set timeouts and max_tokens, and treat the model output as untrusted input.
Install and Configure
The official SDK reads the key from OPENAI_API_KEY. Never hard-code it. Compatible gateways only need a different base_url.
Bash
pip install openai
export OPENAI_API_KEY="sk-..." # Windows: set OPENAI_API_KEY=sk-...First Request
Python
from openai import OpenAI
client = OpenAI(timeout=30)
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a concise backend tutor."},
{"role": "user", "content": "用一句话解释 GIL。"},
],
temperature=0.3,
max_tokens=200,
)
print(resp.choices[0].message.content)
print(resp.usage) # prompt_tokens / completion_tokensOutput
GIL 是 CPython 的全局解释器锁,同一时刻只允许一个线程执行 Python 字节码,因此多线程无法并行跑纯 Python 计算。
CompletionUsage(prompt_tokens=28, completion_tokens=54, total_tokens=82)Streaming
Streaming cuts time-to-first-token to a few hundred milliseconds. Guard against delta.content being None.
Python
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "写一个 Python 快速排序"}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)Structured Output
For JSON output, use JSON mode or tool calling with an explicit schema — and still validate the result.
Python
import json
resp = client.chat.completions.create(
model="gpt-4o-mini",
response_format={"type": "json_object"},
messages=[{"role": "user",
"content": '返回 JSON:{"title": str, "tags": [str]},主题是 Redis 缓存'}],
)
data = json.loads(resp.choices[0].message.content)
print(data["title"], data["tags"])Output
Redis 缓存入门 ['Redis', '缓存', '后端']| Concern | Practice |
|---|---|
| Cost | Cap max_tokens, cache repeated prompts |
| Latency | Stream, or use a smaller model |
| Reliability | Retry on 429/5xx with backoff |
| Safety | Never send secrets or PII in prompts |
Model output is untrusted input: never
eval() it, never interpolate it into SQL, never use it for authorization.安装与配置
官方 SDK 通过环境变量 OPENAI_API_KEY 读取密钥,不要把 key 写进代码或提交到仓库。国内可用兼容 OpenAI 协议的第三方网关,只需改 base_url。
Bash
pip install openai
export OPENAI_API_KEY="sk-..." # Windows: set OPENAI_API_KEY=sk-...第一次请求
Python
from openai import OpenAI
client = OpenAI(timeout=30)
resp = client.chat.completions.create(
model="gpt-4o-mini",
messages=[
{"role": "system", "content": "You are a concise backend tutor."},
{"role": "user", "content": "用一句话解释 GIL。"},
],
temperature=0.3,
max_tokens=200,
)
print(resp.choices[0].message.content)
print(resp.usage) # prompt_tokens / completion_tokensOutput
GIL 是 CPython 的全局解释器锁,同一时刻只允许一个线程执行 Python 字节码,因此多线程无法并行跑纯 Python 计算。
CompletionUsage(prompt_tokens=28, completion_tokens=54, total_tokens=82)流式输出
流式返回让首字延迟从数秒降到几百毫秒,适合聊天界面。注意 delta.content 可能为 None(首帧和尾帧)。
Python
stream = client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "写一个 Python 快速排序"}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content
if delta:
print(delta, end="", flush=True)结构化输出
让模型返回 JSON 时,最稳的做法是用 JSON 模式或函数调用,并在提示词里给出字段定义;仍然要做 schema 校验,模型偶尔会漏字段。
Python
import json
resp = client.chat.completions.create(
model="gpt-4o-mini",
response_format={"type": "json_object"},
messages=[{"role": "user",
"content": '返回 JSON:{"title": str, "tags": [str]},主题是 Redis 缓存'}],
)
data = json.loads(resp.choices[0].message.content)
print(data["title"], data["tags"])Output
Redis 缓存入门 ['Redis', '缓存', '后端']| 关注点 | 做法 |
|---|---|
| 成本 | 限制 max_tokens,缓存重复提示 |
| 延迟 | 流式输出,或换小模型 |
| 可靠性 | 429/5xx 退避重试 |
| 安全 | 提示词里不要放密钥与隐私数据 |
模型输出永远是不可信输入:不要
eval() 它,不要直接拼进 SQL,也不要拿它当权限判断依据。