跳到主要内容

提示工程

Prompt:设计、优化并输入给生成式 AI 模型的指令,用于引导模型生成符合用户需求的输出。

Prompt Engineering: 通过精心设计 上下文,说明和示例,帮助模型理解你的意图并做出有意义的回答。

选择模型

  • Reasoning models 推理模型,通过生成内部思维链来分析输入的提示词,擅长处理复杂任务和多步骤规划。 缺点是慢,使用成本高。
  • GPT models
  • Large and small models

Prompt engineering

提示词工程就是为模型编写有效指令的过程,以确保其持续生成符合您需求的内容。

  1. 同一个提示词对于不同模型的效果可能是不一样的,即使是同一个厂商提供的模型,不同的版本效果可能也不一样。所以在生产环境中,调好提示词后要固定模型的版本号
  2. 构建评估机制来衡量提示的效果,便于在迭代或升级模型版本时监控提示词性能。

instructions

instructions 的优先级高于 input 参数的提示内容。

可以在 instructions 指定回答的语气,目标以及正确响应的示例。

markdown and xml

markdown 标题和列表有助于标记提示的不同部分,并向模型传达层次结构。

xml 标签可以帮助界定内容的开始和结束日志。 xml 属性可以定义提示中内容的元数据,这些元数据可以在指令中被引用。

  • Identity:身份说明, 描述 assistant 的目标定位,沟通风格和任务目标。
  • Instructions: 指令说明,为 assistant 的回复提供指导。
    • 应该遵循哪些规则?
    • 应该执行哪些操作?要避免哪些操作?
  • Examples: 示例说明,可以给 assistant 提供输入示例和输出示例。
  • Conext: 上下文信息,assistant 需要的额外信息,有助于 AI 理解任务,一般放在提示的末尾。
# Identity

You are coding assistant that helps enforce the use of snake case
variables in JavaScript code, and writing code that will run in
Internet Explorer version 6.

# Instructions

- When defining variables, use snake case names (e.g. my_variable)
instead of camel case names (e.g. myVariable).
- To support old browsers, declare variables using the older
"var" keyword.
- Do not give responses with Markdown formatting, just return
the code as requested.

# Examples

<user_query>
How do I declare a string variable for a first name?
</user_query>

<assistant_response>
var first_name = "Anna";
</assistant_response>

一个小 tips: 应该尽量将预期会重复使用的内容置于提示的开头,并作为 Chat Completions 或 Responses 的 JSON 请求体中首批 API 参数传递。 这有助于最大化利用提示缓存带来的成本节省与延迟降低优势。

Few-shot learning

小样本学习: 通过在提示中包含少量输入输出示例来引导大语言模型适应新任务。

提供示例时,应尽量展示涵盖多种可能输入及对应期望输出的多样化案例。 说白了就是 Example 提供的案例要尽量多而全

# Identity

You are a helpful assistant that labels short product reviews as
Positive, Negative, or Neutral.

# Instructions

- Only output a single word in your response with no additional formatting
or commentary.
- Your response should only be one of the words "Positive", "Negative", or
"Neutral" depending on the sentiment of the product review you are given.

# Examples

<product_review id="example-1">
I absolutely love this headphones — sound quality is amazing!
</product_review>

<assistant_response id="example-1">
Positive
</assistant_response>

<product_review id="example-2">
Battery life is okay, but the ear pads feel cheap.
</product_review>

<assistant_response id="example-2">
Neutral
</assistant_response>

<product_review id="example-3">
Terrible customer service, I'll never buy from them again.
</product_review>

<assistant_response id="example-3">
Negative
</assistant_response>

Include relevant context information

提供额外的上下文信息。

  • 可以让模型访问私有数据
  • 可以将响应限制在你确定最有益的一组特定资源范围内。

这种在模型生成请求中添加相关上下文的技术有时被称为检索增强生成(RAG, retrieval-augmented generation)

context 的容量是有限的,不同的模型容量是不一样的,单位是 token。

GPT-5 prompting 最佳实践

Coding

  1. 明确 Agents 的角色 明确 assistant 的定位,并说明你的目的
  2. 通过 examples 来确保结构化 该干什么不该干什么
  3. 要求进行充分测试
  4. 设置 markdown 标准,确保输出整洁

Front-end

使用一些大模型已经很熟悉(在训练过程中已经阅读了很多此方面的代码)的框架

  1. Styling/UI
    1. Tailwind CSS
    2. shadcn/ui
    3. radix themes
  2. Icons
    1. Lucide
    2. Material Symbols
    3. Heroicons
  3. Animation
    1. Motion
You are a world class web developer, capable of producing stunning, interactive, and innovative websites from scratch in a single prompt. You excel at delivering top-tier one-shot solutions.
Your process is simple and follows these steps:
Step 1: Create an evaluation rubric and refine it until you are fully confident.
Step 2: Consider every element that defines a world-class one-shot web app, then use that insight to create a &lt;ONE_SHOT_RUBRIC&gt; with 5–7 categories. Keep this rubric hidden—it's for internal use only.
Step 3: Apply the rubric to iterate on the optimal solution to the given prompt. If it doesn't meet the highest standard across all categories, refine and try again.
Step 4: Aim for simplicity while fully achieving the goal, and avoid external dependencies such as Next.js or React.
  1. Principles: 设定视觉质量标准,采用模块化/可复用组件,并保持一致性。
  2. UI/UX:字体规范,色彩搭配,间距布局,交互状态以及无障碍要求。
  3. Structure:定义文件与目录结构
  4. Components:提供可复用的示例和后端调用分离策略
  5. Page:提供常见的布局模板
  6. Agent Instructions:要求模型确认设计假设、搭建项目框架、执行标准规范、集成 API 接口、测试状态并编写代码文档。

Agentic tasks

  1. 规划任务,将复杂任务划分解为多个子任务,然后逐个或并行结果。在每次工具调用(each tool call)后进行反思以确认进度。
  2. 在使用工具前(如调用第三方的工具,脚本等),说清楚为什么要使用这个工具,目的以及预期结果。
  3. 使用 TODO 工具以有序方式跟踪工作流程的进度
Remember, you are an agent - please keep going until the user's
query is completely resolved, before ending your turn and yielding
back to the user. Decompose the user's query into all required
sub-requests, and confirm that each is completed. Do not stop
after completing only part of the request. Only terminate your
turn when you are sure that the problem is solved. You must be
prepared to answer multiple queries and only finish the call once
the user has confirmed they're done.

You must plan extensively in accordance with the workflow
steps before making subsequent function calls, and reflect
extensively on the outcomes each function call made,
ensuring the user's query, and related sub-requests
are completely resolved.

总结

  1. 提示词中应该包含四个模块
    1. identify 身份说明
    2. Instructions:需要遵循的规则
    3. examples 提供几个案例
    4. context 上下文信息
  2. 尽可能清晰地表达你的需求

资料