发布于
提示词工程技巧:面向业务应用的实用指南
LLM 运行在业务流程中时真正重要的提示词模式:清晰的结构、少样本示例、结构化输出、版本管理,以及像测试代码一样对提示词进行评估、测试和持续迭代。
- 作者
- Ali ÖzdurmuşVeni AI 创始人
- 更新于
- 阅读时间
- 阅读约 4 分钟

提示词工程是指设计模型所接收的指令和上下文,使其可靠地产出你所需要的输出。在业务应用中,提示词是产品的一部分:它像代码一样被版本管理、测试和评审。本指南介绍影响最大的那些技巧。
提示词工程基础
提示词的构成
一个有效的提示词由以下部分组成:
- 系统指令:模型的角色和行为规则
- 上下文:与任务相关的背景信息
- 示例:期望输出格式的示例
- 任务:具体的请求或问题
- 输出格式:期望的响应结构
基本提示词结构
[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, conclusion2. 明确输出格式
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 answers4. 使用分隔符
让各部分界限清晰:
###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提示词模板系统
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
结论
好的提示词具体、有结构且经过测试。定义角色,提供上下文和示例,固定输出格式,并把每一次改动都视为需要对照评估集运行的一次发布。这种纪律比任何单个技巧都更重要。


