发布于

提示词工程技巧:面向业务应用的实用指南

LLM 运行在业务流程中时真正重要的提示词模式:清晰的结构、少样本示例、结构化输出、版本管理,以及像测试代码一样对提示词进行评估、测试和持续迭代。

作者
Ali ÖzdurmuşVeni AI 创始人
更新于
阅读时间
阅读约 4 分钟

提示词工程是指设计模型所接收的指令和上下文,使其可靠地产出你所需要的输出。在业务应用中,提示词是产品的一部分:它像代码一样被版本管理、测试和评审。本指南介绍影响最大的那些技巧。

提示词工程基础

提示词的构成

一个有效的提示词由以下部分组成:

  1. 系统指令:模型的角色和行为规则
  2. 上下文:与任务相关的背景信息
  3. 示例:期望输出格式的示例
  4. 任务:具体的请求或问题
  5. 输出格式:期望的响应结构

基本提示词结构

[Role Definition]
You are a {field of expertise} expert.

[Context]
{Relevant information}

[Task]
{Specific request}

[Format]
{Expected output format}

基本提示词技巧

1. 零样本提示

不提供示例,直接定义任务:

Summarize the following text:
{text}

适用场景:

  • 简单任务
  • 常识性问题
  • 分类

2. 少样本提示

带示例的任务定义:

Text: "This product is great, I am very satisfied"
Sentiment: Positive

Text: "It was a terrible experience, I do not recommend it"
Sentiment: Negative

Text: "It's okay for the price"
Sentiment: ?

最佳实践:

  • 通常3-5个示例就足够
  • 加入各种边缘案例
  • 随机排列示例顺序

3. 思维链(CoT)

逐步思考:

Question: A store has 15 apples. 8 apples were sold, 
then 6 more apples arrived. How many apples are there?

Let's think step by step:
1. Initially there are 15 apples
2. 8 apples sold: 15 - 8 = 7 apples left
3. 6 apples arrived: 7 + 6 = 13 apples

Answer: 13 apples

许多当前的模型内置了推理模式。使用这些模型时,在提示词中要求逐步推理的重要性,不如清晰说明目标、约束和期望的输出。

4. 自洽性

对同一问题采用不同的推理路径:

Solve this problem in 3 different ways and choose the most consistent answer:

[Problem]

5. 思维树(ToT)

分支式的思维树:

Problem: {complex problem}

Thought 1: {approach A}
  → Sub-thought 1.1: {detail}
  → Sub-thought 1.2: {detail}
  
Thought 2: {approach B}
  → Sub-thought 2.1: {detail}
  
Evaluate and select the most suitable path.

进阶技巧

ReAct(推理 + 行动)

思考与行动的循环:

Question: How many times larger is Istanbul's population than Paris?

Thought: I need to find the population of both cities
Action: [search] Istanbul population
Observation: Istanbul population ~16 million

Thought: Now I need to find Paris population
Action: [search] Paris population  
Observation: Paris population ~2.2 million

Thought: I can calculate the ratio
Action: [calculate] 16 / 2.2
Observation: 7.27

Answer: Istanbul's population is approximately 7.3 times larger than Paris.

宪法式AI提示

定义伦理与安全规则:

System: You are a helpful assistant.

Rules:
1. Do not generate harmful content
2. Do not share personal information
3. Do not help with illegal activities
4. Always be honest

User question: {question}

角色提示

定义特定的专业领域:

You are a cybersecurity expert with 20 years of experience.
You have worked as a CISO in Fortune 500 companies.
You can explain technical details clearly and understandably.

User's question: {question}

提示词优化策略

1. 提高具体性

❌ 不好:

Write a blog post

✅ 好:

Target audience: Software developers
Topic: Docker container security
Length: 1500-2000 words
Tone: Technical but accessible
Format: Introduction, 5 main sections, conclusion

2. 明确输出格式

Provide your response in this JSON format:
{
  "summary": "string",
  "key_points": ["string"],
  "next_steps": ["string"],
  "confidence_score": number
}

3. 负向提示

指明不希望出现的行为:

Do NOT do the following:
- Give speculative information
- Make claims without citing sources
- Lead the user
- Give excessively long answers

4. 使用分隔符

让各部分界限清晰:

###CONTEXT###
{context information}

###TASK###
{work to be done}

###FORMAT###
{output format}

针对特定模型的优化

针对OpenAI GPT模型

- Use System message effectively
- Use structured outputs with a JSON schema where available
- Temperature: 0.7-1.0 for creative tasks, 0.1-0.3 for analytical

针对Claude

- Use XML tags: <context>, <task>, <format>
- Put important information at the end in long context
- Use extended thinking for complex, multi-step tasks

针对Gemini

- Optimize for multimodal prompts
- Up-to-date information with Grounding
- Adjust Safety settings

提示词测试与迭代

A/B测试框架

Prompt A: {version 1}
Prompt B: {version 2}

Metrics:
- Accuracy: %
- Consistency: 1-5
- Latency: ms
- Token usage: #

提示词版本管理

prompt_v1.0: First version
prompt_v1.1: Typo corrections
prompt_v2.0: CoT added
prompt_v2.1: Output format changed

企业级提示词管理

创建提示词库

/prompts
  /classification
    - sentiment_analysis.json
    - intent_detection.json
  /generation
    - blog_writer.json
    - code_reviewer.json
  /extraction
    - entity_extraction.json
    - data_parsing.json

提示词模板系统

python
class PromptTemplate:
    def __init__(self, template, variables):
        self.template = template
        self.variables = variables
    
    def render(self, **kwargs):
        return self.template.format(**kwargs)

# Usage
sentiment_prompt = PromptTemplate(
    template="Analyze sentiment: {text}",
    variables=["text"]
)

常见错误与解决办法

错误1:提示词过于含糊

问题: 模型不明白你想要什么 解决: 加入具体的指令和示例

错误2:提示词过长

问题: 超出Token上限,成本上升 解决: 删除不必要的信息,使用摘要

错误3:指令相互冲突

问题: 模型行为前后不一致 解决: 为规则排定优先级并澄清

错误4:幻觉

问题: 模型给出编造的信息 解决: 依据事实来源(Grounding)、要求引用出处、降低temperature

结论

好的提示词具体、有结构且经过测试。定义角色,提供上下文和示例,固定输出格式,并把每一次改动都视为需要对照评估集运行的一次发布。这种纪律比任何单个技巧都更重要。

Veni AI为我们给企业构建的每个AI应用设计、测试并管理版本化的提示词。查看我们的服务,或联系我们的团队。

继续阅读

  • 大模型应用与 RAG

    RAG架构:让LLM扎根企业知识的实用指南

    检索增强生成的端到端原理:分块、嵌入、混合检索、重排序、引用标注,以及证明回答可信的评估指标,帮助企业让 LLM 扎根于自有知识库与内部文档资料。

    阅读约 2 分钟

  • 大模型应用与 RAG

    面向企业的多模态AI:处理文档、图像与音频

    视觉语言模型与语音模型如何读取扫描文档、照片和通话录音:架构原理、流水线示例代码,以及帮助企业降低图像与音频处理 Token 成本的实用方法和技巧。

    阅读约 4 分钟

有流程适合交给 AI 来跑吗?

描述一个目前的流程,我们会告诉您 AI 应用或智能体可以承担其中哪些重复性步骤。