1. 引言
2026 年,OpenAI 的 ChatGPT 与 Codex 已经深度融合,成为开发者日常工作中不可或缺的 AI 搭档。无论是订阅 ChatGPT Plus 还是 Pro 的用户,都能在对话界面中直接调用 Codex 完成代码生成、调试、重构乃至完整的项目搭建。
本文基于 2026 年 9 月 5 日的最新版本,系统梳理 ChatGPT Plus / Pro 与 Codex 的核心能力、模型差异、实际用法与工程实践。全文约 3000 字,包含可直接运行的代码示例,帮助你从「会用」进阶到「用好」。
2. ChatGPT Plus 与 Pro 的核心差异
2.1 订阅档位与模型权限
截至 2026 年 9 月,OpenAI 提供以下主要订阅档位:
| 订阅档位 | 月费(美元) | 可用模型 | Codex 额度 | 适用人群 |
|---|---|---|---|---|
| Free | 0 | GPT-4o mini | 有限 | 尝鲜用户 |
| Plus | 20 | GPT-4o、GPT-4.1、o3 | 中等 | 日常开发与写作 |
| Pro | 200 | 全部模型 + o3 pro | 高额度 | 专业开发者与重度用户 |
2.2 模型能力对比
Plus 与 Pro 用户都能访问 GPT-4o 与 o3 系列,但 Pro 额外解锁了 o3 pro 模式,在数学推理、代码生成与长上下文理解上表现更优。
python
# 示例:通过 OpenAI Python SDK 检查当前可用的模型列表
from openai import OpenAI
client = OpenAI()
models = client.models.list()
for model in models.data:
print(model.id)
运行上述代码,你会看到类似 gpt-4o、gpt-4.1、o3、o3-pro 等模型标识。Pro 用户会额外看到 o3-pro 的访问权限。
3. Codex 是什么:从对话到代码的桥梁
3.1 Codex 的定位
Codex 是 OpenAI 推出的编程智能体,它不仅能生成代码片段,还能在沙箱环境中执行代码、读取文件、运行测试并迭代修复。在 ChatGPT 界面中,Codex 以「智能体模式」运行,可以自主完成多步骤任务。
3.2 Codex 与 ChatGPT 的协作方式
在 ChatGPT Plus / Pro 的对话界面中,你可以通过以下方式启用 Codex:
- 在对话中直接输入
@Codex并描述任务; - 使用「代码解释器」功能上传文件并让 Codex 分析;
- 在 Projects 中创建独立项目,让 Codex 持续迭代。
bash
# 在终端中安装 Codex CLI(2026 年已正式发布)
npm install -g @openai/codex
# 初始化项目
codex init my-project
cd my-project
codex "创建一个 Python Flask 应用,包含用户登录功能"
4. 实战:用 Codex 搭建一个完整的 Web 应用
4.1 需求描述
我们让 Codex 从零搭建一个「待办事项管理 API」,要求:
- 使用 Python FastAPI;
- 支持增删改查;
- 数据存储使用 SQLite;
- 附带单元测试。
4.2 让 Codex 生成项目骨架
在 ChatGPT 中发送以下提示词:
@Codex 请帮我创建一个 FastAPI 待办事项项目,包含:
1. 完整的目录结构
2. 数据库模型(SQLite)
3. RESTful API 接口
4. 单元测试
5. requirements.txt
请逐步执行并展示结果。
Codex 会自动创建项目并输出如下结构:
todo-app/
├── app/
│ ├── __init__.py
│ ├── main.py
│ ├── models.py
│ ├── schemas.py
│ └── database.py
├── tests/
│ └── test_todos.py
├── requirements.txt
└── README.md
4.3 核心代码示例
Codex 生成的 main.py 大致如下:
python
from fastapi import FastAPI, HTTPException, Depends
from sqlalchemy.orm import Session
from . import models, schemas
from .database import SessionLocal, engine
models.Base.metadata.create_all(bind=engine)
app = FastAPI(title="Todo API")
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
@app.post("/todos/", response_model=schemas.Todo)
def create_todo(todo: schemas.TodoCreate, db: Session = Depends(get_db)):
db_todo = models.Todo(**todo.dict())
db.add(db_todo)
db.commit()
db.refresh(db_todo)
return db_todo
@app.get("/todos/{todo_id}", response_model=schemas.Todo)
def read_todo(todo_id: int, db: Session = Depends(get_db)):
todo = db.query(models.Todo).filter(models.Todo.id == todo_id).first()
if todo is None:
raise HTTPException(status_code=404, detail="Todo not found")
return todo
4.4 运行与测试
bash
cd todo-app
pip install -r requirements.txt
uvicorn app.main:app --reload
打开浏览器访问 http://localhost:8000/docs,即可看到 Swagger 交互式文档。Codex 还会自动生成测试文件:
python
from fastapi.testclient import TestClient
from app.main import app
client = TestClient(app)
def test_create_todo():
response = client.post("/todos/", json={"title": "学习 Codex", "completed": False})
assert response.status_code == 200
assert response.json()["title"] == "学习 Codex"
5. Codex 的高级用法
5.1 多文件重构
Codex 能理解整个项目的上下文,执行跨文件重构。例如:
@Codex 将项目中所有使用 requests 库的代码迁移到 httpx,并更新对应的测试。
5.2 代码审查
你可以让 Codex 扮演资深 Reviewer:
@Codex 请审查 src/ 目录下的所有 Python 文件,找出潜在 bug、性能问题和安全隐患,并给出修改建议。
5.3 自动修复 CI 错误
当 CI 构建失败时,把错误日志粘贴给 Codex:
@Codex 以下是我的 CI 报错日志,请分析原因并给出修复方案:
[粘贴日志]
Codex 会定位问题并生成修复补丁。
6. 提示词工程:让 Codex 输出更精准
6.1 结构化提示词模板
python
# 一个高效的 Codex 提示词模板
prompt = f"""
任务:{task_description}
技术栈:{tech_stack}
约束条件:
- 使用 Python 3.12+
- 遵循 PEP 8 规范
- 包含类型注解
- 附带单元测试
输出要求:
1. 先给出整体设计思路
2. 再分文件展示代码
3. 最后给出运行说明
"""
6.2 常见误区
- 提示词过于模糊:只说「写个爬虫」不如「抓取某网站的文章标题、发布时间与正文,保存为 Markdown 文件」;
- 不提供上下文:让 Codex 修改代码时,应粘贴相关文件内容或说明文件路径;
- 忽略迭代:一次生成往往不完美,应通过多轮对话逐步完善。
7. 实际项目中的工作流建议
7.1 推荐流程
#mermaid-svg-A0SSMu8rCpFKKEDY{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-A0SSMu8rCpFKKEDY .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-A0SSMu8rCpFKKEDY .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-A0SSMu8rCpFKKEDY .error-icon{fill:#552222;}#mermaid-svg-A0SSMu8rCpFKKEDY .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-A0SSMu8rCpFKKEDY .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-A0SSMu8rCpFKKEDY .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-A0SSMu8rCpFKKEDY .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-A0SSMu8rCpFKKEDY .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-A0SSMu8rCpFKKEDY .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-A0SSMu8rCpFKKEDY .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-A0SSMu8rCpFKKEDY .marker{fill:#333333;stroke:#333333;}#mermaid-svg-A0SSMu8rCpFKKEDY .marker.cross{stroke:#333333;}#mermaid-svg-A0SSMu8rCpFKKEDY svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-A0SSMu8rCpFKKEDY p{margin:0;}#mermaid-svg-A0SSMu8rCpFKKEDY .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-A0SSMu8rCpFKKEDY .cluster-label text{fill:#333;}#mermaid-svg-A0SSMu8rCpFKKEDY .cluster-label span{color:#333;}#mermaid-svg-A0SSMu8rCpFKKEDY .cluster-label span p{background-color:transparent;}#mermaid-svg-A0SSMu8rCpFKKEDY .label text,#mermaid-svg-A0SSMu8rCpFKKEDY span{fill:#333;color:#333;}#mermaid-svg-A0SSMu8rCpFKKEDY .node rect,#mermaid-svg-A0SSMu8rCpFKKEDY .node circle,#mermaid-svg-A0SSMu8rCpFKKEDY .node ellipse,#mermaid-svg-A0SSMu8rCpFKKEDY .node polygon,#mermaid-svg-A0SSMu8rCpFKKEDY .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-A0SSMu8rCpFKKEDY .rough-node .label text,#mermaid-svg-A0SSMu8rCpFKKEDY .node .label text,#mermaid-svg-A0SSMu8rCpFKKEDY .image-shape .label,#mermaid-svg-A0SSMu8rCpFKKEDY .icon-shape .label{text-anchor:middle;}#mermaid-svg-A0SSMu8rCpFKKEDY .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-A0SSMu8rCpFKKEDY .rough-node .label,#mermaid-svg-A0SSMu8rCpFKKEDY .node .label,#mermaid-svg-A0SSMu8rCpFKKEDY .image-shape .label,#mermaid-svg-A0SSMu8rCpFKKEDY .icon-shape .label{text-align:center;}#mermaid-svg-A0SSMu8rCpFKKEDY .node.clickable{cursor:pointer;}#mermaid-svg-A0SSMu8rCpFKKEDY .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-A0SSMu8rCpFKKEDY .arrowheadPath{fill:#333333;}#mermaid-svg-A0SSMu8rCpFKKEDY .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-A0SSMu8rCpFKKEDY .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-A0SSMu8rCpFKKEDY .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-A0SSMu8rCpFKKEDY .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-A0SSMu8rCpFKKEDY .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-A0SSMu8rCpFKKEDY .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-A0SSMu8rCpFKKEDY .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-A0SSMu8rCpFKKEDY .cluster text{fill:#333;}#mermaid-svg-A0SSMu8rCpFKKEDY .cluster span{color:#333;}#mermaid-svg-A0SSMu8rCpFKKEDY div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-A0SSMu8rCpFKKEDY .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-A0SSMu8rCpFKKEDY rect.text{fill:none;stroke-width:0;}#mermaid-svg-A0SSMu8rCpFKKEDY .icon-shape,#mermaid-svg-A0SSMu8rCpFKKEDY .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-A0SSMu8rCpFKKEDY .icon-shape p,#mermaid-svg-A0SSMu8rCpFKKEDY .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-A0SSMu8rCpFKKEDY .icon-shape .label rect,#mermaid-svg-A0SSMu8rCpFKKEDY .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-A0SSMu8rCpFKKEDY .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-A0SSMu8rCpFKKEDY .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-A0SSMu8rCpFKKEDY :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 否
是
需求分析
用 Codex 生成项目骨架
人工审查与调整
Codex 生成核心模块
运行测试
测试通过?
把报错反馈给 Codex
代码审查与优化
提交与部署
7.2 人机协作原则
- Codex 负责「快」,人类负责「准」;
- 关键业务逻辑必须人工 review;
- 安全敏感代码(认证、支付)不要完全依赖 AI 生成;
- 保持代码库整洁,定期让 Codex 做重构建议。
8. 常见问题与排查
8.1 Codex 生成的代码运行报错怎么办
将完整报错堆栈粘贴回对话,并附上相关代码文件,Codex 会定位问题并修复。
8.2 如何让 Codex 记住项目上下文
在 Projects 功能中创建项目,把相关文件上传或关联 GitHub 仓库,Codex 会自动读取项目结构。
8.3 Plus 与 Pro 的 Codex 额度差异
Pro 用户拥有更高的消息频率与更长的上下文窗口,适合长时间、多文件的复杂任务;Plus 用户在日常开发中通常也足够使用。
9. 总结
ChatGPT Plus / Pro 与 Codex 的组合,正在重新定义开发者的工作方式。通过本文的实战演练,你已经掌握了:
- Plus 与 Pro 的差异与选型建议;
- Codex 的基本用法与高级技巧;
- 从零搭建完整项目的完整流程;
- 提示词工程的核心要点;
- 人机协作的最佳实践。
技术工具会持续迭代,但「清晰描述需求 + 合理拆分任务 + 人工把关质量」的方法论不会过时。希望你在 2026 年的开发旅程中,让 Codex 成为你最得力的编程搭档。