AI应用提示词工程进阶技巧与高级模式深度实战:从思维链到自洽性与ReAct的全解析
AI应用提示词工程进阶技巧与高级模式深度实战:从思维链到自洽性与ReAct的全解析
引言
基础Prompt工程(结构化/Few-Shot/约束)覆盖了80%场景,剩余20%的复杂推理任务需要高级提示技巧。本文从高级Prompt技巧讲起,覆盖思维链(CoT)的变体(Zero-Shot CoT/Self-Consistency/CoT-SC)、ReAct推理-行动模式、Tree-of-Thought(ToT)树状推理、Least-to-Most从简到繁分解、Plan-and-Solve规划求解、Self-Ask自问自答、Prompt链式组合、元Prompt(让LLM优化Prompt),构建高级Prompt工程体系。
一、CoT变体
# advanced_prompt/cot_variants.py
class CoTVariants:
"""思维链变体"""
# Zero-Shot CoT:不加示例,只加"逐步思考"
ZERO_SHOT_COT = """问题:{question}
让我们逐步思考:"""
# Self-Consistency:多次采样取多数投票
async def self_consistency(self, question: str, llm_client,
n_samples: int = 5) -> dict:
"""自洽性:多次采样取多数投票"""
import asyncio
prompt = f"""问题:{question}
请逐步推理后给出最终答案。
最终答案:[答案]"""
tasks = [
llm_client.complete(prompt, temperature=0.3 + i * 0.1)
for i in range(n_samples)
]
results = await asyncio.gather(*tasks)
# 提取答案
import re
answers = []
for r in results:
match = re.search(r'最终答案[::]\s*(.+?)(?:\n|$)', r)
if match:
answers.append(match.group(1).strip())
# 多数投票
from collections import Counter
vote = Counter(answers)
best_answer, best_count = vote.most_common(1)[0]
return {
"answer": best_answer,
"confidence": best_count / len(answers),
"all_answers": answers,
"samples": n_samples,
}
# CoT-SC:CoT + Self-Consistency组合
async def cot_sc(self, question: str, llm_client,
examples: list[dict] = None,
n_samples: int = 5) -> dict:
"""CoT + 自洽性组合"""
few_shot = ""
if examples:
for ex in examples[:3]:
few_shot += f"问题:{ex['question']}\n推理:{ex['reasoning']}\n答案:{ex['answer']}\n\n"
prompt = f"""{few_shot}问题:{question}
推理:"""
# 多次采样
import asyncio
tasks = [
llm_client.complete(prompt, temperature=0.3 + i * 0.15)
for i in range(n_samples)
]
results = await asyncio.gather(*tasks)
# 提取最终答案
answers = []
for r in results:
lines = r.strip().split("\n")
if lines:
answers.append(lines[-1].replace("答案:", "").strip())
from collections import Counter
vote = Counter(answers)
best, count = vote.most_common(1)[0]
return {
"answer": best,
"confidence": count / len(answers),
}
二、ReAct模式
# advanced_prompt/react.py
class ReActPrompt:
"""ReAct:推理+行动交替"""
TEMPLATE = """你是一个能使用工具的AI助手。
可用工具:
{tools_description}
请用以下格式回答:
Question: 用户的问题
Thought: 你对问题的思考
Action: 工具名称
Action Input: JSON参数
Observation: 工具返回的结果
... (Thought/Action/Observation可重复多次)
Thought: 我现在知道答案了
Final Answer: 最终答案
Question: {question}"""
def build(self, question: str, tools: list[dict]) -> str:
tools_desc = "\n".join(
f"- {t['name']}: {t['description']}" for t in tools
)
return self.TEMPLATE.format(
tools_description=tools_desc, question=question,
)
三、Plan-and-Solve
# advanced_prompt/plan_solve.py
class PlanAndSolve:
"""Plan-and-Solve:先规划再求解"""
PLAN_PROMPT = """将以下问题分解为解决步骤。
问题:{question}
请制定解决计划:
1. 第一步:...
2. 第二步:...
3. ...
只输出计划,不要解决。"""
SOLVE_PROMPT = """按照以下计划逐步解决问题。
问题:{question}
计划:
{plan}
请按计划逐步执行,每步给出推理与结果。"""
async def solve(self, question: str, llm_client) -> str:
# 1. 规划
plan = await llm_client.complete(
self.PLAN_PROMPT.format(question=question),
temperature=0.1, max_tokens=500,
)
# 2. 按计划求解
result = await llm_client.complete(
self.SOLVE_PROMPT.format(question=question, plan=plan),
temperature=0.2, max_tokens=2000,
)
return result
四、元Prompt
# advanced_prompt/meta.py
class MetaPromptOptimizer:
"""元Prompt:让LLM优化自己的Prompt"""
META_PROMPT = """你是一个Prompt工程专家。
以下是一个Prompt及其在测试用例上的表现:
当前Prompt:
{current_prompt}
测试结果:
{test_results}
问题:
1. 当前Prompt在哪些测试用例上表现不好?
2. 可能的原因是什么?
3. 如何改进Prompt?
请输出改进后的Prompt(仅输出Prompt,不要解释)。"""
async def optimize(self, current_prompt: str,
test_results: list[dict],
llm_client) -> str:
"""优化Prompt"""
results_text = "\n".join(
f"输入:{r['input'][:100]}\n期望:{r.get('expected', '')[:100]}\n"
f"实际:{r.get('actual', '')[:100]}\n{'✅' if r.get('pass') else '❌'}"
for r in test_results[:10]
)
prompt = self.META_PROMPT.format(
current_prompt=current_prompt,
test_results=results_text,
)
improved = await llm_client.complete(prompt, temperature=0.3, max_tokens=2000)
return improved
async def iterative_optimize(self, initial_prompt: str,
eval_cases: list[dict],
llm_client,
max_iterations: int = 3) -> dict:
"""迭代优化Prompt"""
current = initial_prompt
for i in range(max_iterations):
# 评估当前Prompt
results = []
for case in eval_cases[:5]:
output = await llm_client.complete(
current.format(**case["input"]), temperature=0,
)
passed = case.get("expected_keyword", "") in output
results.append({
"input": str(case["input"]),
"expected": case.get("expected_keyword", ""),
"actual": output[:100],
"pass": passed,
})
pass_rate = sum(1 for r in results if r["pass"]) / len(results)
print(f"迭代{i+1}: pass_rate={pass_rate:.0%}")
if pass_rate >= 0.95:
break
# 优化
current = await self.optimize(current, results, llm_client)
return {"optimized_prompt": current, "iterations": i + 1,
"final_pass_rate": pass_rate}
总结
高级Prompt技巧以"CoT变体-ReAct-Plan-Solve-元Prompt"五模式展开:Self-Consistency通过多次采样取多数投票将CoT的准确率从70%提升至90%+(代价是5x token消耗),ReAct以Thought-Action-Observation交替模式让LLM在推理中调用工具实现"边想边做",Plan-and-Solve先让LLM制定步骤计划再按计划逐步执行(适合需要结构化解决的复杂问题),元Prompt让LLM分析自身Prompt在测试用例上的失败模式并自动改进(迭代3轮通常能提升pass_rate 20%+)。当Prompt工程从"写一句话"进化为"CoT+Self-Consistency+ReAct+Plan-Solve+元优化"的高级工具箱,复杂推理任务的准确率从"勉强能用"提升至"生产可靠"。
- 点赞
- 收藏
- 关注作者
评论(0)