跳到主要内容

Prompt Engineering

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

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

选择模型

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

优化提示词是为了让模型回答符合预期。

OpenAI: Prompt engineering

https://platform.openai.com/docs/guides/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. 尽可能清晰地表达你的需求

Github: Waht is Prompt engineering?

https://github.com/resources/articles/what-is-prompt-engineering

提示工程是一种编写清晰、有目的性输入的技术,旨在引导 AI 模型输出准确且符合 上下文的结果。它已成为 AI 辅助工作流程中的核心技能,帮助团队在编码、数据分 析、内容生成、自动化及对话系统设计等任务中塑造大语言模型(LLMs)的响应方式。 当提示经过精心设计后,与 AI 的交互会变得更可预测、高效且实用。

对于一些基础提示可以采用直接指令,高级提示则需要通过添加结构、上下文、约束条件 和示例来引导模型推导。

通过调整措辞、格式或者基于角色的提示来提升表达的清晰度和一致性

  • 测试反馈
  • 优化语言
  • 调整结构

how do prompts work?

prompt 会影响到模型的输出。

temperature 参数也会影响。 通过 temperature 参数来控制输出的创造性和确定性。 数值越大,输出结果越飞。

Token Limits 定义了模型一次能处理的信息量。

Model memory(也叫 context window) 决定了生成新响应时模型能保留多少对话或先前输入的内容。

Different types of prompts

  • instruction prompts 指令型提示
  • example-based prompts 示例性提示
  • conversational prompts 对话式提示
  • system prompts 系统提示

structed prompt-engineering techniques 结构化提示工程技术

  • Zero-shot prompting
  • Few-show prompting
  • Chain-of-thought prompting (CoT) 思维链提示,引导模型逐步解释其推理过程。

Prompt engineering best practices

  1. 明确目的,说清楚你希望模型生成什么内容,或者你的指令是什么,你要干什么,可以解释下为什么会有这个指令。避免歧义。
  2. 提供上下文,如编程语言,允许环境,目标受众等。
  3. 格式和结构, 使用 markdown 格式,固定的结构
  4. 必要时添加示例。
  5. 迭代并优化,提示词不会一次就能调好,要持续迭代。
  6. 在上生产之前要测试。

how to avoid common prompting pitfals

  1. 避免使用模糊或开放式的提示词
  2. 保持措辞中立客观。(你可以清晰的表达出你的立场,AI 是谄媚的,如果你想听到真话,那么就在提问的时候明确表明你的立场。
  3. 验证输出结果。(不要完全相信 AI 的输出

AI prompt engineering use cases

  • 代码生成与调试
    • 生成样板代码
    • 语法修正
    • 重构
  • 文档学习
    • 注释
    • 技术摘要
    • 知识库
  • 测试与自动化
    • 生成测试用例
    • 验证输出结果
    • 生成测试文档
  • chatbots
  • 数据分析与内容创作
  • workflow optimization 工作流优化
    • 生成 PR 摘要
    • 分析代码质量
    • 自动执行常规文档更新

learing and evolving with prompt engineering

  1. 尝试不同的提示风格和模型
    1. 措辞
    2. 语气
    3. 结构
  2. 学习别人的提示词是如何写的
  3. 持续优化测试

Google:提示词工程

什么是提示工程?

提示工程是一门设计和优化提示的艺术和科学,可指导 AI 模型(尤其是 LLM) 生成所需回答。通过精心设计提示,您可以为模型提供上下文、说明和示例,帮 助模型理解您的意图并做出有意义的回答。

编写优质提示的策略

  1. 设定明确的目标
    1. 期望的操作
    2. 输出的格式和长度
    3. 目标受众
  2. 提示上下文和背景
    1. 相关事实和数据
    2. 引用特定的文档和来源
    3. 定义关键术语和概念
  3. 使用少样本提示
    1. 提供一些输入输出的案例
    2. 所需的风格和基调
    3. 展示所需的详细程度
  4. 具体说明
    1. 使用准确的语言,避免含糊不清
    2. 尽量优化你的请求
    3. 将复杂的任务分解成较小的步骤
  5. 迭代和实验
    1. 尝试不同的措辞和关键字
    2. 调整详细程度和具体程度
    3. 测试不同的提示长度
  6. 使用思维链提示(CoT)
    1. 鼓励逐步推理
    2. 让模型解释其推导过程
    3. 引导模型完成符合逻辑的思维顺序

最佳实践

  1. 了解训练限制或偏见,以便更有效地设计提示,并理解特定模型可能支持哪些类型的提示。
  2. 尽可能具体明确
  3. 善用情境化提示(AI 所扮演的角色)
  4. 为 AI 模型提供示例
  5. 尝试不同的提示和角色设定
  6. 尝试思维链式提示法

Claude: Prompt engineering overview

https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/overview

案例

案例 1

你的需求是:制作 50 个素食蓝莓松饼

第一次提问:蓝莓松饼的食谱是什么?

虽然模型能够给出具体的食谱,但是这与你的需求并不完全匹配。因为模型并不知道你要做 50 个,因此,它不太可能列出所需的大量食材,也不会提供有助于更高效烘焙大批量松饼的建议。模型只能依据所给上下文进行回应。

第二次提问:我要招待 50 位客人,请生成一份制作 50 个蓝莓松饼的食谱。

对这个提问继续优化

第三次提问:你是一位专业素食主厨,我要招待 50 位客人,请生成一份制作 50 个蓝莓松饼的食谱。

资料