思维链推理与自我反思机制
思维链(Chain-of-Thought)是提升大模型推理能力的关键技术。通过展示中间推理步骤,模型能解决更复杂的多步推理问题。
CoT 变体
- Zero-shot CoT:添加"请逐步推理"提示
- Few-shot CoT:提供带推理过程的示例
- Self-Consistency:多次采样取多数票
- Reflexion:自我反思修正错误推理
python
# Self-Consistency 推理
def self_consistency(prompt, model, n_samples=5):
responses = [model.generate(prompt, temperature=0.7) for _ in range(n_samples)]
answers = [extract_answer(r) for r in responses]
# 多数投票
from collections import Counter
most_common = Counter(answers).most_common(1)[0][0]
return most_commonSelf-Consistency 的代价
Self-Consistency 需要 n 倍的推理计算量。在数学推理等高价值场景下,2-3 倍的准确率提升值得这个代价。