DeepSeek Harness 教程
按顺序阅读,循序渐进掌握相关 AI 工具与实战方法。
掌握有效的提示词技巧是充分利用 Codex CLI 的关键。本节详细介绍如何编写高效的提示词来获得最佳结果。
你通过发送提示词(prompt)与 Codex 交互,描述你想要它完成的任务。
# 解释代码功能 "Explain how the transform module works and how other modules use it." # 添加新功能 "Add a new command-line option `--json` that outputs JSON." # 修复 bug "Fix the error in the login function"
当你提交提示词时,Codex 按以下循环工作:
理解这个循环有助于你更好地设计提示词,因为 Codex 会在每个步骤中基于上下文做出决策。
Codex 在能够验证工作时会产生更高质量的输出。包含验证步骤可以帮助 Codex 确保正确性:
# 不推荐 "Write a function to sort a list" # 推荐 - 包含验证 "Write a function to sort a list. Include test cases to verify it handles empty lists, already-sorted lists, reverse-sorted lists, and lists with duplicates. Run the tests to confirm they pass."
Codex 处理较小的、专注的任务时表现更好。小任务更容易测试和审查:
# 不推荐 - 任务太大 "Create a complete user authentication system with login, registration, password reset, and OAuth" # 推荐 - 拆分为小步骤 "Step 1: Create a user model with email and password fields Step 2: Add a registration endpoint Step 3: Add a login endpoint with JWT Step 4: Add password reset functionality Let me know when Step 1 is done before moving to Step 2"
提供 Codex 可以使用的上下文,如相关文件和图像的引用:
# 不推荐 "Fix this bug" # 推荐 - 包含详细信息 "Fix the bug in src/auth.py where users can't log in with special characters in their password. I've attached a screenshot of the error message and the relevant code section."
告诉 Codex 你期望的输出格式:
# 指定代码风格 "Write a function to calculate Fibonacci numbers. Use TypeScript, include JSDoc comments, and follow functional programming principles." # 指定输出结构 "Explain the codebase structure. Organize your answer with: 1) Overview 2) Key directories 3) Main entry points"
越具体的提示词通常会产生越好的结果。Codex 擅长理解意图,但你提供的信息越多,它的表现越好。
线程是单个会话:你的提示词加上后续的模型输出和工具调用。一个线程可以包含多个提示词。
| 类型 | 说明 | 优势 |
|---|---|---|
| 本地线程 | 在你的机器上运行 | 可以读写文件、使用现有工具 |
| 云端线程 | 在隔离环境中运行 | 并行运行、从其他设备委派任务 |
# 打开会话选择器 codex resume # 直接跳转到最近会话 codex resume --last # 恢复指定会话 codex resume <SESSION_ID>
运行中的线程可以并行,但避免两个线程同时修改同一文件。你也可以稍后通过继续另一个提示词来恢复线程。
当你提交提示词时,包含 Codex 可以使用的上下文,例如对相关文件和图像的引用。IDE 扩展会自动将打开的文件列表和选中的文本范围作为上下文。
线程中的所有信息必须适合模型的上下文窗口。Codex 会监控并报告剩余空间。
对于较长的任务,Codex 可能会自动压缩上下文,通过总结相关信息并丢弃不太相关的细节。Codex 可以在多个步骤中继续处理复杂任务。
你可以通过以下方式帮助 Codex 管理上下文:
/new)当 Codex 报告上下文使用量较高时,考虑开始一个新的会话或减少历史消息。
对于复杂任务,使用计划模式让 Codex 先规划再执行:
# 使用 /plan 命令 /plan 实现用户认证系统 # 或者在提示词中包含 "plan" "Plan and implement a REST API with CRUD operations for a blog"
请求 Codex 审查代码而非直接修改:
# 使用 /review 命令 /review src/auth.py # 或者描述审查需求 "Review this code for potential security vulnerabilities and suggest improvements"
明确告诉 Codex 可以修改什么:
# 只读模式 "Explain what this function does, don't make any changes" # 限定文件范围 "Add logging only to the auth module, don't touch other files" # 逐步确认 "Let me know the proposed changes before you make them"
以下是一些常用的提示词模板:
Write a [语言] function that [功能描述]. Requirements: - [需求1] - [需求2] Include: - 详细的文档注释 - 错误处理 - 测试用例
Review the [文件/模块] for: - Potential bugs - Security issues - Performance problems - Code style violations Provide: - Issue list with severity - Suggested fixes - Overall quality score (1-10)
Fix the bug in [文件:行号/函数名]. Error: [错误信息] Expected behavior: [期望行为] Steps to reproduce: [复现步骤]
Refactor [文件/函数] to [目标]. Current issues: - [问题1] - [问题2] Constraints: - 保持功能不变 - 不要改变 API 接口 - 添加单元测试
熟能生巧。多练习编写提示词,你会逐渐掌握如何获得最佳结果。
尝试:1) 更具体地描述 2) 提供更多上下文 3) 分解为更小的步骤 4) 使用示例说明期望的输出
Codex 支持多语言,但英文通常效果最好。如果你用中文描述,确保描述足够详细和明确。
明确告诉它 "只读" 或 "don't make any changes",它就会只提供分析而不修改。
开始一个新的会话(使用 /new),或者让 Codex 压缩历史记录(它会自动处理)。