跳到主要内容

Prompt Engineering

提示词工程是设计有效输入(prompt)以引导大语言模型(LLM)产生预期输出的艺术与科学。这门学科融合了对 LLM 行为的理解、语言精确性和迭代优化。

概览

什么是提示词工程?

提示词工程是设计和优化提示词以从语言模型中引出特定、高质量响应的实践。它包括:

  • 清晰的指令:明确表达你希望模型做什么
  • 提供上下文:给出相关背景信息
  • 格式规范:定义预期的输出结构
  • 提供示例:向模型展示好的输出是什么样的
  • 设置约束:确立边界和限制

为什么它很重要

方面影响
准确性精心设计的提示词显著减少幻觉和错误
一致性可靠的提示词在不同会话中产生可重复的结果
效率好的提示词减少多次迭代的需要
能力适当的提示词能解锁模型的高级能力

核心原则

1. 具体而明确

模糊的提示词导致模糊的输出。精确描述你想要什么。

差的:

Write something about AI.

好的:

Write a 3-paragraph technical introduction to Large Language Models,
covering their architecture, training process, and common use cases.
Target audience: software engineers.

2. 提供上下文

给模型相关的背景信息来指导其响应。

Context: You are a senior Java architect reviewing a Spring Boot application
that processes payment transactions. The application uses Spring AI for
fraud detection and needs to handle 10,000 transactions per second.

Task: Review the following controller code for potential performance bottlenecks...

3. 使用示例(少样本学习)

向模型展示你期望的输入-输出模式示例。

Convert the following technical terms from formal to casual:

Input: Asynchronous Programming
Output: async code

Input: Microservices Architecture
Output: microservices

Input: Event-Driven Architecture
Output: event-based systems

Input: Server-Side Rendering
Output: SSR

4. 指定输出格式

精确告诉模型你希望响应如何组织。

Analyze the following code and provide your response in this format:

## Security Issues
- [List any security vulnerabilities]

## Performance Issues
- [List performance concerns]

## Recommendations
1. [First recommendation]
2. [Second recommendation]
3. [Third recommendation]

5. 设置约束

为响应确立清晰的边界。

Write a Python function to validate email addresses.

Constraints:
- Maximum 50 lines of code
- Use only standard library (no external packages)
- Include docstring and type hints
- Must handle edge cases (null input, empty string, invalid formats)
- Provide 3 test cases

高级技巧

思维链提示(Chain-of-Thought)

引导模型进行逐步推理。

To solve this problem, let's think through it step by step:

1. First, identify what the question is asking
2. Then, break down the information given
3. Consider different approaches
4. Choose the best approach
5. Verify your answer

Question: [Your question here]

角色提示

为模型分配特定角色以获得一致的视角。

You are a principal software architect with 15 years of experience
building distributed systems at scale. You specialize in Spring Boot,
event-driven architectures, and cloud-native applications. You favor
pragmatic solutions over theoretical purity.

Review the following system design proposal...

自洽性

让模型用多种方式解决同一问题并进行比较。

Solve the following problem in three different ways, then identify
the best approach and explain your reasoning.

Problem: [Your problem here]

生成知识提示

让模型在回答前先生成相关上下文。

Step 1: Generate 5-7 key concepts about [topic] that are relevant to [question]

Step 2: Using these concepts, answer: [your question]

提示词模式

CO-STAR 框架

  1. Context(上下文)- 背景信息
  2. Objective(目标)- 你想要达成什么
  3. Style(风格)- 期望的语气/格式
  4. Tone(调性)- 声音/态度
  5. Audience(受众)- 谁会阅读这些内容
  6. Response(响应)- 输出格式
Context: I'm preparing a technical presentation for CTO-level executives
about adopting Spring AI in our payment processing platform.

Objective: Explain the business value and technical approach in 5 minutes
of speaking time.

Style: Executive summary with technical depth available on request

Tone: Confident but realistic about challenges

Audience: Technical decision-makers who understand software architecture

Response: A structured outline with key points, supporting arguments, and
risk mitigation strategies.

RTF 框架

  1. Role(角色)- 模型应该扮演谁
  2. Task(任务)- 需要做什么
  3. Format(格式)- 如何呈现输出
Role: Senior DevOps engineer specializing in Kubernetes and AWS

Task: Design a deployment strategy for a Spring Boot application using
Spring AI, including CI/CD pipeline, monitoring, and disaster recovery

Format: Architecture diagram with annotations, plus implementation checklist

常见陷阱

陷阱 1:过长的提示词

过长的提示词可能超出模型的上下文窗口并导致性能下降。

解决方案:保持简洁。删除不必要的信息。尽可能使用引用而非内联数据。

陷阱 2:相互冲突的指令

当提示词包含矛盾要求时,模型可能难以确定优先级。

解决方案:审查提示词的内部一致性。使用清晰的层级关系:"主要目标:X。次要目标:Y。如有冲突,X 优先于 Y。"

陷阱 3:模糊的成功标准

没有明确的"完成"标准,模型可能产出不完整的输出。

解决方案:定义明确的完成条件:"当你提供了 X、Y 和 Z 后,响应才算完成。"

陷阱 4:缺少反面示例

只展示应该做什么,而不展示不该做什么,可能导致常见错误。

解决方案:包含反模式:"这是一个糟糕输出的示例:[示例]。请避免这些问题。"

最佳实践

开发阶段

  1. 版本化你的提示词:像对待代码一样对待提示词。跟踪变更和结果
  2. 系统化测试:在多样化输入上评估提示词
  3. 衡量性能:跟踪准确性、延迟和成本指标
  4. 记录模式:构建可复用的提示词模板库

生产阶段

  1. 验证输入:部署前检查提示词模板
  2. 监控输出:跟踪生产环境中的质量指标
  3. 处理边界情况:为异常输入准备备用提示词
  4. A/B 测试提示词:持续优化以获得更好的结果

工具与框架

  • Promptfoo:开源提示词测试框架
  • PromptLayer:提示词版本管理和分析平台
  • LangChain:提示词模板和管理
  • Guidance:微软的结构化提示词框架

延伸阅读