Prompt Engineering 基础:如何通过文本指令引导模型

📂 所属阶段:第五阶段 — 迈向大模型 (LLM) 的阶梯
🔗 相关章节:GPT 系列演进 · 指令微调 (Instruction Tuning)


1. 什么是 Prompt Engineering?

Prompt = 给大模型的"指令"

类比:向一位知识渊博的助手提问

差的问题:
"翻译" → 助手不知道译什么、从哪译到哪

好的问题:
"将以下英文翻译成中文:
Hello, how are you?
→ "

Prompt Engineering = 学会如何精确地向大模型提问

2. Zero-shot Prompting

2.1 无示例,直接给任务

from openai import OpenAI
client = OpenAI(api_key="your-api-key")

def zero_shot(prompt):
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content

# 文本分类
result = zero_shot("""
判断以下评论的情感(正面/负面):
"这个产品太棒了,质量很好!"
""")
print(result)  # 正面

# 翻译
result = zero_shot("将以下中文翻译成英文:今天天气很好")
print(result)  # The weather is nice today.

# 摘要
result = zero_shot("请用一句话总结:人工智能是计算机科学的一个分支...")
print(result)

2.2 Zero-shot 的局限

Zero-shot 的局限:
1. 复杂推理任务效果差
2. 特定格式输出不稳定
3. 对 Prompt 措辞敏感

→ 解决方案:Few-shot Prompting!

3. Few-shot Prompting

3.1 通过示例学习

def few_shot(prompt):
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content

# 示例:在 Prompt 中给出几个例子
prompt = """
将成语翻译成英文:

塞翁失马,焉知非福
→ A loss may turn out to be a gain

纸上谈兵
→ To talk idly about theoretical matters

亡羊补牢
→ Repair the pen after the sheep are lost (It's never too late to fix)

破镜重圆
→ """

result = few_shot(prompt)
print(result)
# To reunite after a separation or rift

3.2 示例的质量比数量重要

# 好的示例:多样化、有代表性
examples = """
情感分类:

文本:"今天心情很好"
情感:正面

文本:"这个东西太差了"
情感:负面

文本:"一般般,没什么特别的"
情感:中性
"""

# 差的示例:重复、歧义
bad_examples = """
情感:
好 → 正
好 → 正
差 → 负
"""

4. Chain-of-Thought(思维链)

4.1 什么是 CoT?

"""
标准 Prompt:
问:小明有5个苹果,他买了3个,吃了2个,还剩几个?
答:6个

错误!小模型会直接跳步计算

CoT Prompt:
问:小明有5个苹果,他买了3个,吃了2个,还剩几个?
答:
- 起始:5个
- 买了3个:5 + 3 = 8个
- 吃了2个:8 - 2 = 6个
- 最终:6个

大模型被引导着一步步思考,正确率大大提升!
"""

4.2 CoT 实践

def cot_prompt(question):
    prompt = f"""
请逐步推理,回答以下问题:

问题:{question}

推理步骤:
1. """
    return cot_prompt

# 示例
question = "一个商店有50个苹果,上午卖了20个,下午又进货30个,现在有多少个?"
result = few_shot(cot_prompt(question))
print(result)
# 1. 起始有50个苹果
# 2. 卖了20个:50 - 20 = 30个
# 3. 进货30个:30 + 30 = 60个
# 4. 最终:60个

4.3 Zero-shot CoT

"""
2022 年发现:只需在 Prompt 末尾加一句"让我们一步一步思考"
模型就会自动进行思维链推理!
"""
prompt = """
小明有10个球,丢了3个,又买了5个,现在有多少个球?
让我们一步一步思考。
"""
# 模型自动分步推理,不需要 Few-shot 示例!

5. 高级 Prompt 技巧

5.1 结构化 Prompt

def structured_prompt(task_description, context, question):
    return f"""
【任务】
{task_description}

【背景信息】
{context}

【问题】
{question}

【输出格式】
请用以下 JSON 格式输出:
{{
    "answer": "答案",
    "confidence": 0.0-1.0,
    "reasoning": "推理过程"
}}
"""

5.2 System Prompt

"""
System Prompt = 给模型设定身份和行为约束
"""

messages = [
    {
        "role": "system",
        "content": "你是一位资深Python程序员,擅长写出简洁、高效、可读性强的代码。"
    },
    {
        "role": "user",
        "content": "写一个快速排序算法"
    }
]

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages
)

6. 小结

Prompt Engineering 三剑客:

1. Zero-shot:直接给任务
2. Few-shot:给几个示例
3. CoT:引导模型分步推理

技巧:
- 明确任务描述
- 指定输出格式
- 用 System Prompt 设定角色
- CoT 末尾加"让我们一步一步思考"

💡 记住:Prompt Engineering 没有万能公式,需要不断迭代优化。好 Prompt = 清晰的任务 + 充分的上下文 + 正确的格式约束。


🔗 扩展阅读