跳到主要内容

7 高级技术

引言

高级提示工程将 AI 从简单的工具转变为复杂的推理伙伴。这些技术利用模型的元认知能力——它能够反思、评估和改进自身输出的能力。研究表明,与基本提示相比,高级提示技术在复杂推理任务中可以提升 20-40% 的性能(Zhou 等人,2024)。

本章涵盖了来自最新研究的经过验证的高级技术,并为生产系统提供实用的 Spring AI 实现。

┌─────────────────────────────────────────────────────────────────────────┐
│ 高级技术分类法 │
├─────────────────────────────────────────────────────────────────────────│
│ │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
│ │ 自我改进 │ │ 多步 │ │ 推理 │ │
│ │ 技术 │ │ 编排 │ │ 增强 │ │
│ ├─────────────────┤ ├─────────────────┤ ├─────────────────┤ │
│ │ • 自我批判 │ │ • 提示链 │ │ • 步退 │ │
│ │ • 反思 │ │ • 多轮 │ │ • 自洽 │ │
│ │ • 智能体-评论者 │ │ • 分解 │ │ • ToT/GoT/AoT │ │
│ │ • PACE │ │ • 聚合 │ │ • PAL │ │
│ └─────────────────┘ └─────────────────┘ └─────────────────┘ │
│ │
│ ┌─────────────────┐ ┌─────────────────┐ ┌─────────────────┐ │
│ │ 元级 │ │ 输出控制 │ │ 新兴 │ │
│ │ 提示 │ │ 技术 │ │ 技术 │ │
│ ├─────────────────┤ ├─────────────────┤ ├─────────────────┤ │
│ │ • DSPy │ │ • 定向 │ │ • 情感提示 │ │
│ │ • OPRO │ │ • 对比 │ │ • RaR │ │
│ │ • 自动提示 │ │ • 框架 │ │ • 思维线 │ │
│ │ • PE2 │ │ • 操控 │ │ • 再阅读 │ │
│ └─────────────────┘ └─────────────────┘ └─────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────┘
信息

性能基准: 自我批判在复杂推理任务中提高准确性 20-30%,自洽性提高 15-25%,步退提示提高 10-20%(Li 等人,2024)。


1. 自我批判与反思

自我批判使模型能够通过结构化内省来评估和改进自身输出。反思框架(Shinn 等人,2023)将此形式化为迭代改进循环。

1.1 反思框架

反思通过跨尝试的记忆和持续学习扩展了简单的自我批判:

┌─────────────────────────────────────────────────────────────────────────┐
│ 反思架构 │
├─────────────────────────────────────────────────────────────────────────│
│ │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ 智能 │───────▶│ 评估器 │───────▶│ 反思 │ │
│ │ 体 (LLM) │ │ (LLM/代码) │ │ 历史 │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
│ │ │ │
│ │ │ │
│ ▼ │ │
│ ┌─────────────┐ │ │
│ │ 响应 │ │ │
│ └─────────────┘ │ │
│ │ │ │
│ │ ┌─────────────────────────────────────────┘ │
│ │ │ │
│ ▼ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ 记忆 (反思历史) │ │
│ │ "在尝试 1 中,我犯了错误 X。对于尝试 2, │ │
│ │ 我应该关注 Y 而不是 Z。" │ │
│ └─────────────────────────────────────────────────────┘ │
│ │ │
│ │ 输入下一次尝试 │
│ ▼ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ 改进的智能体尝试 │ │
│ └─────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────┘

1.2 基础自我批判模式

在单个提示中最简单的自我批判形式:

public class SelfCritiqueService {

private final ChatClient chatClient;

private static final String SELF_CRITIQUE_TEMPLATE = """
<task>
使用自我批判解决以下问题。
</task>

<problem>
{problem}
</problem>

<process>
## 第一阶段:初始解决方案
生成你的第一个解决方案尝试。

## 第二阶段:批判分析
根据以下标准评估你的解决方案:
1. **正确性**:是否存在任何逻辑或事实错误?
2. **完整性**:是否遗漏了应该包含的内容?
3. **清晰度**:任何部分是否可以更清楚地解释?
4. **效率**:是否有更好的方法?
5. **边界情况**:是否考虑了边界条件?

## 第三阶段:改进的解决方案
根据你的批判,生成一个改进的解决方案。
明确解决每个识别出的问题。

## 第四阶段:验证
验证改进后的解决方案确实解决了问题。
</process>

<output_format>
### 初始解决方案
[你的第一次尝试]

### 批判
| 标准 | 发现的问题 | 严重程度 |
|-----------|-------------|----------|
| ... | ... | 高/中/低 |

### 改进的解决方案
[解决所有问题的改进方案]

### 验证
[问题已解决的确认]
</output_format>
""";

public String solveWithSelfCritique(String problem) {
return chatClient.prompt()
.user(u -> u.text(SELF_CRITIQUE_TEMPLATE)
.param("problem", problem))
.call()
.content();
}
}

1.3 多轮反思与记忆

对于复杂任务,实现带有持久记忆的迭代反思:

@Service
public class ReflexionService {

private final ChatClient chatClient;
private static final int MAX_ITERATIONS = 5;
private static final double SATISFACTION_THRESHOLD = 0.85;

public record ReflexionResult(
String finalAnswer,
List<ReflectionEntry> reflectionHistory,
int iterations,
double confidenceScore
) {}

public record ReflectionEntry(
int attempt,
String response,
String evaluation,
String reflection,
double score
) {}

public ReflexionResult solveWithReflexion(String problem) {
List<ReflectionEntry> history = new ArrayList<>();
String currentResponse = null;

for (int i = 0; i < MAX_ITERATIONS; i++) {
// 第 1 步:生成响应(智能体)
currentResponse = generateResponse(problem, history);

// 第 2 步:评估响应
EvaluationResult evaluation = evaluateResponse(problem, currentResponse);

// 第 3 步:检查是否令人满意
if (evaluation.score() >= SATISFACTION_THRESHOLD) {
history.add(new ReflectionEntry(
i + 1, currentResponse, evaluation.feedback(),
"解决方案被接受", evaluation.score()
));
return new ReflexionResult(currentResponse, history, i + 1, evaluation.score());
}

// 第 4 步:为下一次尝试生成反思
String reflection = generateReflection(problem, currentResponse, evaluation);

history.add(new ReflectionEntry(
i + 1, currentResponse, evaluation.feedback(),
reflection, evaluation.score()
));
}

return new ReflexionResult(currentResponse, history, MAX_ITERATIONS,
evaluateResponse(problem, currentResponse).score());
}

private String generateResponse(String problem, List<ReflectionEntry> history) {
String historyContext = formatReflectionHistory(history);

String prompt = """
<problem>
{problem}
</problem>

<previous_attempts>
{history}
</previous_attempts>

<instruction>
基于先前尝试的反思,生成改进的解决方案。
避免重复反思历史中识别出的相同错误。
</instruction>
""";

return chatClient.prompt()
.user(u -> u.text(prompt)
.param("problem", problem)
.param("history", historyContext.isEmpty() ? "没有先前尝试。" : historyContext))
.call()
.content();
}

private String generateReflection(String problem, String response, EvaluationResult eval) {
String prompt = """
<task>生成反思以改进下一次尝试</task>

<problem>{problem}</problem>
<response>{response}</response>
<evaluation>{evaluation}</evaluation>

<instruction>
分析出了什么错误并为下一次尝试提供具体、可行的指导。
关注:
1. 具体犯了什么错误
2. 为什么发生这些错误
3. 下次应该做什么不同的事情
4. 应该从这次尝试中保留什么
</instruction>

<format>
提供一个简洁的反思(2-3句话),将直接改进下一次尝试。
</format>
""";

return chatClient.prompt()
.user(u -> u.text(prompt)
.param("problem", problem)
.param("response", response)
.param("evaluation", eval.feedback()))
.call()
.content();
}

private String formatReflectionHistory(List<ReflectionEntry> history) {
return history.stream()
.map(e -> String.format("""
--- 尝试 %d (分数: %.2f) ---
响应摘要: %s
问题: %s
反思: %s
""", e.attempt(), e.score(),
truncate(e.response(), 200),
e.evaluation(),
e.reflection()))
.collect(Collectors.joining("\n"));
}
}

1.4 宪法式 AI 自我批判

受 Anthropic 宪法式 AI 启发,根据明确的原则评估响应:

public class ConstitutionalCritique {

private static final List<String> CONSTITUTION = List.of(
"响应必须准确且事实正确",
"响应必须有帮助并解决用户的实际需求",
"响应必须安全且不促进有害行为",
"响应必须诚实对待不确定性",
"响应必须简洁且结构良好"
);

private static final String CONSTITUTIONAL_TEMPLATE = """
<task>
根据宪法原则评估和修订响应。
</task>

<original_response>
{response}
</original_response>

<constitution>
{principles}
</constitution>

<process>
对于每个原则:
1. 评估响应是否遵循它(是/否/部分)
2. 如果是"否"或"部分",识别具体违规
3. 建议具体的改进

然后提供一个完全遵循所有原则的修订响应。
</process>

<output_format>
## 宪法审查
| 原则 | 遵循程度 | 问题 | 改进 |
|-----------|-----------|--------|-------------|
| ... | ... | ... | ... |

## 修订响应
[遵循所有原则的改进响应]
</output_format>
""";

public String critiqueConstitutionally(String response) {
String principles = IntStream.range(0, CONSTITUTION.size())
.mapToObj(i -> (i + 1) + ". " + CONSTITUTION.get(i))
.collect(Collectors.joining("\n"));

return chatClient.prompt()
.user(u -> u.text(CONSTITUTIONAL_TEMPLATE)
.param("response", response)
.param("principles", principles))
.call()
.content();
}
}

2. 迭代求精模式

迭代求精通过多个结构化轮次改进输出,每轮针对特定方面。

2.1 PACE 框架(智能体-评论者)

PACE 框架(Dong 等人,2024)实现了用于提示优化的正式智能体-评论者循环:

┌─────────────────────────────────────────────────────────────────────────┐
│ PACE 框架 │
├─────────────────────────────────────────────────────────────────────────│
│ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ 评论者(评估器) │ │
│ │ • 为输出质量评分 (0-1) │ │
│ │ • 识别具体弱点 │ │
│ │ • 提供改进建议 │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ 精炼信号 │ │
│ │ 分数: 0.65 → 需要改进 │ │
│ │ 问题: [清晰度, 完整性] │ │
│ │ 建议: ["添加示例", "解释第 3 步"] │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ 智能体(生成器) │ │
│ │ • 接收原始任务 + 批判 │ │
│ │ • 生成改进的输出 │ │
│ │ • 解决具体反馈 │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │ │
│ │ 循环直到分数 ≥ 阈值 │
│ ▼ │
│ ┌─────────────────────────────────────────────────────────────────┐ │
│ │ 最终输出 │ │
│ │ 分数: 0.92 ✓ │ │
│ └─────────────────────────────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────┘
@Service
public class PaceRefinementService {

private final ChatClient chatClient;

public record CritiqueResult(
double score,
List<String> issues,
List<String> suggestions,
String detailedFeedback
) {}

public record RefinementResult(
String output,
double score,
int iterations,
List<CritiqueResult> critiqueHistory
) {}

public RefinementResult refineWithPace(
String task,
double threshold,
int maxIterations) {

List<CritiqueResult> history = new ArrayList<>();
String currentOutput = generateInitial(task);

for (int i = 0; i < maxIterations; i++) {
// 评论者阶段
CritiqueResult critique = evaluate(task, currentOutput);
history.add(critique);

if (critique.score() >= threshold) {
return new RefinementResult(currentOutput, critique.score(), i + 1, history);
}

// 智能体阶段 - 根据批判进行精炼
currentOutput = refine(task, currentOutput, critique);
}

CritiqueResult finalCritique = evaluate(task, currentOutput);
return new RefinementResult(currentOutput, finalCritique.score(), maxIterations, history);
}

private CritiqueResult evaluate(String task, String output) {
String prompt = """
<role>你是一个批判性评估者</role>

<task>{task}</task>
<output>{output}</output>

<instruction>
评估输出质量。要批判且彻底。
</instruction>

<response_format>
返回 JSON:
{
"score": 0.0-1.0,
"issues": ["问题1", "问题2"],
"suggestions": ["建议1", "建议2"],
"detailed_feedback": "解释评估的段落"
}
</response_format>
""";

return chatClient.prompt()
.user(u -> u.text(prompt)
.param("task", task)
.param("output", output))
.call()
.entity(CritiqueResult.class);
}

private String refine(String task, String currentOutput, CritiqueResult critique) {
String prompt = """
<role>你是一位内容改进专家</role>

<original_task>{task}</original_task>
<current_output>{output}</current_output>

<critique>
分数: {score}
问题: {issues}
建议: {suggestions}
详细反馈: {feedback}
</critique>

<instruction>
生成改进版本,该版本:
1. 保持当前输出的优点
2. 解决所有识别出的问题
3. 纳入所有建议
4. 旨在达到 0.9 以上的分数
</instruction>
""";

return chatClient.prompt()
.user(u -> u.text(prompt)
.param("task", task)
.param("output", currentOutput)
.param("score", String.valueOf(critique.score()))
.param("issues", String.join(", ", critique.issues()))
.param("suggestions", String.join(", ", critique.suggestions()))
.param("feedback", critique.detailedFeedback()))
.call()
.content();
}
}

2.2 扩展-压缩-求精模式

用于生成结构化内容的三阶段模式:

@Service
public class ECRService {

private final ChatClient chatClient;

/**
* 扩展-压缩-求精模式
* 阶段 1:生成全面内容
* 阶段 2:压缩为关键洞察
* 阶段 3:为清晰性和可操作性精炼
*/
public String generateWithECR(String topic) {
// 阶段 1:扩展 - 生成全面内容
String expanded = chatClient.prompt()
.user(u -> u.text("""
<task>全面分析</task>
<topic>{topic}</topic>

生成深入的分析,包括:
- 所有相关方面和维度
- 多个视角和观点
- 支持证据和示例
- 边界情况和例外
- 相关的历史背景
- 未来影响

要详尽。包含所有潜在相关的内容。
""").param("topic", topic))
.call()
.content();

// 阶段 2:压缩 - 提取关键洞察
String compressed = chatClient.prompt()
.user(u -> u.text("""
<task>提取关键洞察</task>
<content>{expanded}</content>

从上述全面分析中提取:
1. 3-5 个最重要的洞察
2. 每个洞察的关键支持证据
3. 洞察之间的关键关系

关注最重要的事情。去除冗余。
""").param("expanded", expanded))
.call()
.content();

// 阶段 3:精炼 - 为可操作性进行打磨
String refined = chatClient.prompt()
.user(u -> u.text("""
<task>为可操作性精炼</task>
<insights>{compressed}</insights>

将这些洞察转换为:
1. 清晰、可操作的建议
2. 按影响优先级排序
3. 包含具体的实施步骤
4. 包括成功指标

使它对读者立即有用。
""").param("compressed", compressed))
.call()
.content();

return refined;
}
}

2.3 分层求精

对于长篇内容,在多个层面进行精炼:

@Service
public class HierarchicalRefinementService {

private final ChatClient chatClient;

public String refineHierarchically(String draft) {
// 第 1 级:结构精炼
String structureRefined = refineStructure(draft);

// 第 2 级:节级精炼(并行)
List<String> sections = splitIntoSections(structureRefined);
List<String> refinedSections = sections.parallelStream()
.map(this::refineSection)
.collect(Collectors.toList());

// 第 3 级:句子级润色
String combined = String.join("\n\n", refinedSections);
String polished = polishSentences(combined);

// 第 4 级:连贯性检查
return ensureCoherence(polished);
}

private String refineStructure(String draft) {
return chatClient.prompt()
.user(u -> u.text("""
<task>精炼文档结构</task>
<draft>{draft}</draft>

评估并改进结构:
1. 组织是否有逻辑性?
2. 过渡是否流畅?
3. 信息分组是否有效?
4. 流程是否支持理解?

如需要重新组织,但保留所有内容。
""").param("draft", draft))
.call()
.content();
}

private String refineSection(String section) {
return chatClient.prompt()
.user(u -> u.text("""
<task>精炼节</task>
<section>{section}</section>

改进这个节:
1. 加强主要论点
2. 在抽象处添加具体示例
3. 去除冗余
4. 确保技术准确性
5. 提高可读性
""").param("section", section))
.call()
.content();
}

private String polishSentences(String content) {
return chatClient.prompt()
.user(u -> u.text("""
<task>句子级润色</task>
<content>{content}</content>

为每个句子润色:
1. 清晰度和简洁性
2. 主动语态偏好
3. 精确的词语选择
4. 多样的句子结构
5. 语法和标点

保持作者的声音和意义。
""").param("content", content))
.call()
.content();
}

private String ensureCoherence(String content) {
return chatClient.prompt()
.user(u -> u.text("""
<task>连贯性检查</task>
<content>{content}</content>

检查连贯性:
1. 每个节是否连接到下一个?
2. 引用是否在整个文档中保持一致?
3. 术语使用是否一致?
4. 结论是否从主体内容中自然得出?

进行最小的编辑以确保连贯性。
""").param("content", content))
.call()
.content();
}
}

3. 元提示

元提示使用 AI 来创建、评估和优化提示本身。这实现了大规模的系统化提示工程。

3.1 DSPy 式声明式提示

DSPy(斯坦福,2024)引入了提示工程的声明式范式:

/**
* Spring AI 的受 DSPy 启发的声明式提示系统
*/
@Service
public class DeclarativePromptService {

private final ChatClient chatClient;

/**
* 签名:定义输入/输出合约
*/
public record Signature(
String description,
List<String> inputs,
List<String> outputs,
Map<String, String> fieldDescriptions
) {}

/**
* 模块:封装带有优化的提示
*/
public class PromptModule {
private final Signature signature;
private String optimizedPrompt;

public PromptModule(Signature signature) {
this.signature = signature;
this.optimizedPrompt = generateInitialPrompt(signature);
}

public Map<String, String> forward(Map<String, String> inputs) {
String prompt = buildPrompt(inputs);
String response = chatClient.prompt()
.user(prompt)
.call()
.content();
return parseOutputs(response);
}

public void optimize(List<Example> examples, int iterations) {
for (int i = 0; i < iterations; i++) {
List<Example> failed = findFailedExamples(examples);
if (failed.isEmpty()) break;

this.optimizedPrompt = improvePrompt(this.optimizedPrompt, failed);
}
}
}

private String generateInitialPrompt(Signature sig) {
return chatClient.prompt()
.user(u -> u.text("""
<task>基于此签名生成提示</task>

<signature>
描述: {description}
输入: {inputs}
输出: {outputs}
字段描述: {fieldDescs}
</signature>

生成清晰、有效的提示,该提示:
1. 接受指定的输入
2. 产生指定的输出
3. 遵循最佳实践

仅返回提示文本。
""")
.param("description", sig.description())
.param("inputs", String.join(", ", sig.inputs()))
.param("outputs", String.join(", ", sig.outputs()))
.param("fieldDescs", sig.fieldDescriptions().toString()))
.call()
.content();
}

private String improvePrompt(String currentPrompt, List<Example> failedExamples) {
String failureAnalysis = failedExamples.stream()
.map(e -> String.format("输入: %s\n预期: %s\n实际: %s",
e.input(), e.expectedOutput(), e.actualOutput()))
.collect(Collectors.joining("\n---\n"));

return chatClient.prompt()
.user(u -> u.text("""
<task>基于失败改进提示</task>

<current_prompt>
{prompt}
</current_prompt>

<failures>
{failures}
</failures>

分析提示失败的原因并生成改进版本。
关注:
1. 失败中的模式
2. 缺失的说明
3. 模糊的措辞
4. 缺少示例

仅返回改进的提示。
""")
.param("prompt", currentPrompt)
.param("failures", failureAnalysis))
.call()
.content();
}
}

3.2 自动提示优化(OPRO)

OPRO(Google,2024)使用 LLM 通过迭代搜索来优化提示:

@Service
public class OproService {

private final ChatClient chatClient;
private final ChatClient optimizerClient; // 单独的优化模型

public record OptimizationResult(
String bestPrompt,
double bestScore,
List<PromptCandidate> history
) {}

public record PromptCandidate(
String prompt,
double score,
int generation
) {}

public OptimizationResult optimizePrompt(
String taskDescription,
List<Example> examples,
int generations,
int candidatesPerGeneration) {

List<PromptCandidate> allCandidates = new ArrayList<>();
String bestPrompt = "";
double bestScore = 0;

// 生成初始候选
List<String> currentPrompts = generateInitialCandidates(taskDescription, candidatesPerGeneration);

for (int gen = 0; gen < generations; gen++) {
// 评估所有候选
for (String prompt : currentPrompts) {
double score = evaluatePrompt(prompt, examples);
allCandidates.add(new PromptCandidate(prompt, score, gen));

if (score > bestScore) {
bestScore = score;
bestPrompt = prompt;
}
}

// 基于顶级表现者生成下一代
List<PromptCandidate> topCandidates = allCandidates.stream()
.sorted(Comparator.comparingDouble(PromptCandidate::score).reversed())
.limit(5)
.collect(Collectors.toList());

currentPrompts = generateNextGeneration(taskDescription, topCandidates, candidatesPerGeneration);
}

return new OptimizationResult(bestPrompt, bestScore, allCandidates);
}

private List<String> generateNextGeneration(
String task,
List<PromptCandidate> topCandidates,
int count) {

String candidateInfo = topCandidates.stream()
.map(c -> String.format("分数: %.3f\n提示: %s", c.score(), c.prompt()))
.collect(Collectors.joining("\n---\n"));

String metaPrompt = """
<role>你是一位提示优化专家</role>

<task>{task}</task>

<top_prompts>
{candidates}
</top_prompts>

<instruction>
生成 {count} 个可能分数更高的新提示变体。

可以尝试的策略:
1. 组合高分提示的元素
2. 添加更具体的说明
3. 包括示例
4. 为清晰度重新结构化
5. 添加约束或格式要求

每个提示用 ===PROMPT=== 分隔返回。
</instruction>
""";

String response = optimizerClient.prompt()
.user(u -> u.text(metaPrompt)
.param("task", task)
.param("candidates", candidateInfo)
.param("count", String.valueOf(count)))
.call()
.content();

return Arrays.asList(response.split("===PROMPT==="));
}

private double evaluatePrompt(String prompt, List<Example> examples) {
int correct = 0;
for (Example example : examples) {
String response = chatClient.prompt()
.user(u -> u.text(prompt + "\n\n输入: {input}")
.param("input", example.input()))
.call()
.content();

if (isCorrect(response, example.expectedOutput())) {
correct++;
}
}
return (double) correct / examples.size();
}
}

3.3 提示增强管道

生产就绪的提示改进系统:

@Service
public class PromptEnhancementPipeline {

private final ChatClient chatClient;

public record EnhancedPrompt(
String original,
String enhanced,
List<Enhancement> enhancements,
QualityAssessment assessment
) {}

public record Enhancement(
String type,
String description,
String before,
String after
) {}

public record QualityAssessment(
double clarity,
double specificity,
double completeness,
double safety,
double overall
) {}

public EnhancedPrompt enhance(String originalPrompt) {
// 第 1 步:分析当前提示
PromptAnalysis analysis = analyzePrompt(originalPrompt);

// 第 2 步:应用增强
List<Enhancement> enhancements = new ArrayList<>();
String currentPrompt = originalPrompt;

if (analysis.needsSpecificity()) {
Enhancement e = addSpecificity(currentPrompt);
enhancements.add(e);
currentPrompt = e.after();
}

if (analysis.needsExamples()) {
Enhancement e = addExamples(currentPrompt);
enhancements.add(e);
currentPrompt = e.after();
}

if (analysis.needsStructure()) {
Enhancement e = addStructure(currentPrompt);
enhancements.add(e);
currentPrompt = e.after();
}

if (analysis.needsSafetyConstraints()) {
Enhancement e = addSafetyConstraints(currentPrompt);
enhancements.add(e);
currentPrompt = e.after();
}

// 第 3 步:评估质量
QualityAssessment assessment = assessQuality(currentPrompt);

return new EnhancedPrompt(originalPrompt, currentPrompt, enhancements, assessment);
}

private Enhancement addSpecificity(String prompt) {
String enhanced = chatClient.prompt()
.user(u -> u.text("""
<task>为这个提示添加具体性</task>
<prompt>{prompt}</prompt>

识别模糊的部分并使其具体:
- 用精确的术语替换一般术语
- 添加具体约束
- 指定预期范围
- 定义模糊术语

仅返回改进的提示。
""").param("prompt", prompt))
.call()
.content();

return new Enhancement("specificity", "添加了精确的术语和约束", prompt, enhanced);
}

private Enhancement addExamples(String prompt) {
String enhanced = chatClient.prompt()
.user(u -> u.text("""
<task>为这个提示添加示例</task>
<prompt>{prompt}</prompt>

添加 2-3 个相关示例,这些示例:
- 演示预期的输入/输出格式
- 涵盖不同情况(典型、边界)
- 简洁但具有说明性

自然地将示例集成到提示中。
仅返回改进的提示。
""").param("prompt", prompt))
.call()
.content();

return new Enhancement("examples", "添加了说明性示例", prompt, enhanced);
}

private Enhancement addStructure(String prompt) {
String enhanced = chatClient.prompt()
.user(u -> u.text("""
<task>改进这个提示的结构</task>
<prompt>{prompt}</prompt>

为清晰度重新结构化:
- 使用清晰的节标题
- 分离上下文、任务和格式
- 使用 XML 标签或markdown进行组织
- 逻辑地排序信息

仅返回重新结构化的提示。
""").param("prompt", prompt))
.call()
.content();

return new Enhancement("structure", "改进了组织和格式", prompt, enhanced);
}

private Enhancement addSafetyConstraints(String prompt) {
String enhanced = chatClient.prompt()
.user(u -> u.text("""
<task>为这个提示添加安全约束</task>
<prompt>{prompt}</prompt>

添加适当的约束,用于:
- 输出边界(长度、格式)
- 内容安全(无有害内容)
- 优雅处理边界情况
- 拒绝不适当的请求

仅返回改进的提示。
""").param("prompt", prompt))
.call()
.content();

return new Enhancement("safety", "添加了安全约束", prompt, enhanced);
}
}

4. 多轮推理

多轮推理链式调用多个 LLM 来解决超出单轮能力的复杂问题。

4.1 分解-求解-集成模式

将复杂问题分解为可管理的子问题:

@Service
public class MultiTurnReasoningService {

private final ChatClient chatClient;

public record ReasoningTrace(
String problem,
List<SubProblem> decomposition,
List<SubSolution> solutions,
String integration,
String finalAnswer
) {}

public record SubProblem(String id, String description, List<String> dependencies) {}
public record SubSolution(String id, String solution, double confidence) {}

public ReasoningTrace solveComplex(String problem) {
// 第 1 轮:分解
List<SubProblem> subProblems = decompose(problem);

// 第 2 轮:求解子问题(考虑依赖)
List<SubSolution> solutions = solveSubProblems(subProblems);

// 第 3 轮:集成
String integration = integrate(problem, solutions);

// 第 4 轮:最终合成
String finalAnswer = synthesize(problem, integration);

return new ReasoningTrace(problem, subProblems, solutions, integration, finalAnswer);
}

private List<SubProblem> decompose(String problem) {
String prompt = """
<task>将这个问题分解为子问题</task>

<problem>{problem}</problem>

<instruction>
将其分解为 3-7 个子问题,这些子问题:
1. 可以独立解决
2. 共同涵盖完整问题
3. 有清晰的依赖关系(如果有)
4. 按依赖排序(先解决的前行)
</instruction>

<output_format>
返回 JSON 数组:
[
{"id": "SP1", "description": "...", "dependencies": []},
{"id": "SP2", "description": "...", "dependencies": ["SP1"]},
...
]
</output_format>
""";

return chatClient.prompt()
.user(u -> u.text(prompt).param("problem", problem))
.call()
.entity(new ParameterizedTypeReference<List<SubProblem>>() {});
}

private List<SubSolution> solveSubProblems(List<SubProblem> subProblems) {
Map<String, SubSolution> solved = new HashMap<>();

for (SubProblem sp : subProblems) {
// 获取依赖的解决方案
String dependencyContext = sp.dependencies().stream()
.map(dep -> solved.get(dep))
.filter(Objects::nonNull)
.map(s -> String.format("%s 的解决方案: %s", s.id(), s.solution()))
.collect(Collectors.joining("\n"));

SubSolution solution = solveSubProblem(sp, dependencyContext);
solved.put(sp.id(), solution);
}

return new ArrayList<>(solved.values());
}

private SubSolution solveSubProblem(SubProblem sp, String context) {
String prompt = """
<task>解决这个子问题</task>

<sub_problem>
ID: {id}
描述: {description}
</sub_problem>

<context_from_dependencies>
{context}
</context_from_dependencies>

<instruction>
提供彻底的解决方案。考虑边界情况。
在解决方案中评估你的置信度 (0-1)。
</instruction>

<output_format>
返回 JSON:
{"id": "{id}", "solution": "...", "confidence": 0.95}
</output_format>
""";

return chatClient.prompt()
.user(u -> u.text(prompt)
.param("id", sp.id())
.param("description", sp.description())
.param("context", context.isEmpty() ? "无依赖" : context))
.call()
.entity(SubSolution.class);
}

private String integrate(String problem, List<SubSolution> solutions) {
String solutionSummary = solutions.stream()
.map(s -> String.format("%s (置信度: %.2f): %s", s.id(), s.confidence(), s.solution()))
.collect(Collectors.joining("\n\n"));

return chatClient.prompt()
.user(u -> u.text("""
<task>集成子解决方案</task>

<original_problem>{problem}</original_problem>

<sub_solutions>
{solutions}
</sub_solutions>

<instruction>
将这些子解决方案组合成一个连贯的整体:
1. 解决解决方案之间的任何冲突
2. 填补解决方案不连接的间隙
3. 确保整体一致性
4. 完全解决原始问题
</instruction>
""")
.param("problem", problem)
.param("solutions", solutionSummary))
.call()
.content();
}

private String synthesize(String problem, String integration) {
return chatClient.prompt()
.user(u -> u.text("""
<task>合成最终答案</task>

<original_problem>{problem}</original_problem>

<integrated_solution>{integration}</integrated_solution>

<instruction>
提供清晰、完整的最终答案,该答案:
1. 直接回答原始问题
2. 结构良好且易于遵循
3. 包含分析中的关键洞察
4. 注意任何限制或假设
</instruction>
""")
.param("problem", problem)
.param("integration", integration))
.call()
.content();
}
}

4.2 研究-分析-合成管道

对于知识密集型任务:

@Service
public class ResearchPipeline {

private final ChatClient researchClient;
private final ChatClient analysisClient;
private final ChatClient synthesisClient;

public record ResearchResult(
String topic,
List<Finding> findings,
Analysis analysis,
String synthesis,
List<String> sources
) {}

public ResearchResult research(String topic, int depth) {
// 阶段 1:研究 - 收集信息
List<Finding> findings = conductResearch(topic, depth);

// 阶段 2:分析 - 识别模式和洞察
Analysis analysis = analyzeFindings(topic, findings);

// 阶段 3:合成 - 创建连贯的叙述
String synthesis = synthesizeReport(topic, findings, analysis);

return new ResearchResult(
topic,
findings,
analysis,
synthesis,
extractSources(findings)
);
}

private List<Finding> conductResearch(String topic, int depth) {
List<Finding> findings = new ArrayList<>();

// 初始研究
String initialPrompt = """
<task>研究这个主题</task>
<topic>{topic}</topic>

提供关于这个主题的关键事实、概念和信息。
结构化为发现列表,每个发现包含:
- 主要观点
- 支持证据
- 源类型(事实/推理/常识)
""";

String initial = researchClient.prompt()
.user(u -> u.text(initialPrompt).param("topic", topic))
.call()
.content();

findings.addAll(parseFindings(initial));

// 基于初始发现进行深入研究
if (depth > 1) {
List<String> deeperTopics = identifyGaps(topic, findings);
for (String subtopic : deeperTopics) {
String deeper = researchClient.prompt()
.user(u -> u.text("""
<task>深入研究</task>
<main_topic>{topic}</main_topic>
<subtopic>{subtopic}</subtopic>
<existing_findings>{existing}</existing_findings>

更深入地研究这个子主题。
专注于未在现有发现中覆盖的新信息。
""")
.param("topic", topic)
.param("subtopic", subtopic)
.param("existing", summarizeFindings(findings)))
.call()
.content();

findings.addAll(parseFindings(deeper));
}
}

return findings;
}

private Analysis analyzeFindings(String topic, List<Finding> findings) {
String prompt = """
<task>分析研究发现</task>

<topic>{topic}</topic>

<findings>
{findings}
</findings>

<instruction>
执行深度分析:
1. 识别关键主题和模式
2. 注意矛盾或争论
3. 评估证据质量
4. 进行推理
5. 识别研究中的空白
</instruction>

<output_format>
返回结构化的分析,包含:
- themes:主要主题列表
- patterns:观察到的重复模式
- contradictions:冲突信息
- evidenceQuality:源质量评估
- gaps:缺失的内容
- keyInsights:最重要的要点
</output_format>
""";

return analysisClient.prompt()
.user(u -> u.text(prompt)
.param("topic", topic)
.param("findings", formatFindings(findings)))
.call()
.entity(Analysis.class);
}

private String synthesizeReport(String topic, List<Finding> findings, Analysis analysis) {
return synthesisClient.prompt()
.user(u -> u.text("""
<task>合成研究报告</task>

<topic>{topic}</topic>
<findings>{findings}</findings>
<analysis>{analysis}</analysis>

<instruction>
创建全面的报告,该报告:
1. 以执行摘要开始
2. 按主题呈现发现
3. 讨论影响和重要性
4. 解决矛盾和限制
5. 以关键要点结束

以清晰、专业的散文写作。
</instruction>
""")
.param("topic", topic)
.param("findings", formatFindings(findings))
.param("analysis", analysis.toString()))
.call()
.content();
}
}

5. 自洽性

自洽性(Wang 等人,2022)生成多个推理路径并就最终答案进行多数投票。

5.1 基础自洽性

@Service
public class SelfConsistencyService {

private final ChatClient chatClient;

public record ConsistencyResult(
String finalAnswer,
double confidence,
Map<String, Integer> answerDistribution,
List<String> reasoningPaths
) {}

public ConsistencyResult solveWithConsistency(String problem, int numPaths) {
List<String> reasoningPaths = new ArrayList<>();
Map<String, Integer> answerCounts = new HashMap<>();

// 使用 temperature > 0 生成多个推理路径
for (int i = 0; i < numPaths; i++) {
String response = chatClient.prompt()
.user(u -> u.text("""
逐步解决这个问题:

{problem}

显示你的推理,然后在 "ANSWER:" 之后提供最终答案。
""").param("problem", problem))
.options(ChatOptions.builder()
.temperature(0.7) // 较高的温度用于多样化路径
.build())
.call()
.content();

reasoningPaths.add(response);

String answer = extractAnswer(response);
answerCounts.merge(answer, 1, Integer::sum);
}

// 找出多数答案
String majorityAnswer = answerCounts.entrySet().stream()
.max(Map.Entry.comparingByValue())
.map(Map.Entry::getKey)
.orElse("");

double confidence = (double) answerCounts.get(majorityAnswer) / numPaths;

return new ConsistencyResult(majorityAnswer, confidence, answerCounts, reasoningPaths);
}

private String extractAnswer(String response) {
int idx = response.lastIndexOf("ANSWER:");
if (idx >= 0) {
return response.substring(idx + 7).trim().split("\\n")[0].trim();
}
return response.substring(Math.max(0, response.length() - 100));
}
}

5.2 通用自洽性(USC)

USC 通过使用 LLM 在自由形式答案中找到共识,将自洽性扩展到自由形式答案:

@Service
public class UniversalSelfConsistencyService {

private final ChatClient chatClient;

public record USCResult(
String consensusAnswer,
double confidence,
List<String> answers,
String consensusReasoning
) {}

public USCResult solveWithUSC(String problem, int numPaths) {
// 生成多个答案
List<String> answers = new ArrayList<>();
for (int i = 0; i < numPaths; i++) {
String answer = chatClient.prompt()
.user(u -> u.text("""
{problem}

仔细思考并提供你的答案。
""").param("problem", problem))
.options(ChatOptions.builder().temperature(0.7).build())
.call()
.content();
answers.add(answer);
}

// 使用 LLM 在自由形式答案中找到共识
String answerList = IntStream.range(0, answers.size())
.mapToObj(i -> String.format("答案 %d:\n%s", i + 1, answers.get(i)))
.collect(Collectors.joining("\n\n---\n\n"));

String consensusPrompt = """
<task>在这些答案中找到共识</task>

<original_question>
{problem}
</original_question>

<multiple_answers>
{answers}
</multiple_answers>

<instruction>
分析这些答案并确定共识:
1. 识别共同的主题和结论
2. 注意答案同意vs不同意的地方
3. 合成最受支持的答案
4. 基于同意程度 (0-1) 评估置信度
</instruction>

<output_format>
返回 JSON:
{
"consensus": "合成的共识答案",
"confidence": 0.85,
"reasoning": "如何确定共识的解释"
}
</output_format>
""";

record ConsensusOutput(String consensus, double confidence, String reasoning) {}

ConsensusOutput result = chatClient.prompt()
.user(u -> u.text(consensusPrompt)
.param("problem", problem)
.param("answers", answerList))
.call()
.entity(ConsensusOutput.class);

return new USCResult(result.consensus(), result.confidence(), answers, result.reasoning());
}
}

6. 步退提示

步退提示(Google,2024)要求模型首先考虑更高级的概念,然后再解决具体问题。

@Service
public class StepBackPromptingService {

private final ChatClient chatClient;

public record StepBackResult(
String originalQuestion,
String stepBackQuestion,
String highLevelReasoning,
String specificAnswer
) {}

public StepBackResult solveWithStepBack(String question) {
// 第 1 步:生成步退问题
String stepBackQuestion = chatClient.prompt()
.user(u -> u.text("""
<task>生成步退问题</task>

<original_question>
{question}
</original_question>

<instruction>
在回答这个具体问题之前,什么一般原则、
概念或背景知识将有助于解决它?

生成一个捕捉底层概念的更广泛的问题。
</instruction>

<example>
原始: "如果我在恒定温度下将理想气体的体积加倍,压力会发生什么变化?"
步退: "理想气体中压力和体积的关系是什么(波义耳定律)?"
</example>

仅返回步退问题。
""").param("question", question))
.call()
.content();

// 第 2 步:回答步退问题
String highLevelReasoning = chatClient.prompt()
.user(u -> u.text("""
<question>
{stepBack}
</question>

提供这个概念/原则的彻底解释。
包括相关公式、关系或框架。
""").param("stepBack", stepBackQuestion))
.call()
.content();

// 第 3 步:应用于原始问题
String specificAnswer = chatClient.prompt()
.user(u -> u.text("""
<task>使用背景知识回答具体问题</task>

<original_question>
{question}
</original_question>

<relevant_background>
{background}
</relevant_background>

<instruction>
使用上面的背景知识回答原始问题。
展示你如何将一般原则应用于这个具体案例。
</instruction>
""")
.param("question", question)
.param("background", highLevelReasoning))
.call()
.content();

return new StepBackResult(question, stepBackQuestion, highLevelReasoning, specificAnswer);
}
}

7. 程序辅助语言(PAL)

PAL 将自然语言推理与代码执行结合,用于准确计算。

7.1 带代码执行的 PAL

@Service
public class ProgramAidedLanguageService {

private final ChatClient chatClient;
private final CodeExecutionService codeExecutor;

public record PALResult(
String problem,
String reasoning,
String generatedCode,
String executionResult,
String finalAnswer
) {}

public PALResult solveWithPAL(String problem) {
// 第 1 步:生成推理和代码
String codeGenerationPrompt = """
<task>通过编写 Python 代码解决这个问题</task>

<problem>
{problem}
</problem>

<instruction>
1. 首先在注释中解释你的推理
2. 编写计算答案的 Python 代码
3. 将最终答案存储在名为 'answer' 的变量中
4. 最后打印答案
</instruction>

<output_format>
```python
# 推理: [你的解释]
# 步骤 1: ...
# 步骤 2: ...

[你的代码]

print(f"答案: {answer}")
```
</output_format>
""";

String response = chatClient.prompt()
.user(u -> u.text(codeGenerationPrompt).param("problem", problem))
.call()
.content();

// 提取代码
String code = extractCode(response);
String reasoning = extractReasoning(code);

// 第 2 步:执行代码
ExecutionResult execution = codeExecutor.execute(code);

// 第 3 步:格式化最终答案
String finalAnswer = formatAnswer(problem, reasoning, execution);

return new PALResult(problem, reasoning, code, execution.output(), finalAnswer);
}

private String extractCode(String response) {
Pattern pattern = Pattern.compile("```python\\\\s*([\\\\s\\\\S]*?)```");
Matcher matcher = pattern.matcher(response);
if (matcher.find()) {
return matcher.group(1).trim();
}
return response;
}

private String formatAnswer(String problem, String reasoning, ExecutionResult execution) {
if (execution.success()) {
return chatClient.prompt()
.user(u -> u.text("""
<task>格式化答案</task>

<problem>{problem}</problem>
<reasoning>{reasoning}</reasoning>
<computation_result>{result}</computation_result>

提供清晰、人类可读的答案,该答案:
1. 说明最终数值结果
2. 包含适当的单位
3. 简要解释解决方案方法
""")
.param("problem", problem)
.param("reasoning", reasoning)
.param("result", execution.output()))
.call()
.content();
} else {
return "计算错误: " + execution.error();
}
}
}

7.2 在 Spring AI 中使用工具调用的 PAL

利用 Spring AI 的工具调用进行计算:

@Configuration
public class PALToolConfiguration {

@Bean
public FunctionCallback calculatorTool() {
return FunctionCallback.builder()
.function("calculate", (CalculationRequest req) -> {
try {
ScriptEngine engine = new ScriptEngineManager()
.getEngineByName("python");
Object result = engine.eval(req.expression());
return new CalculationResult(true, result.toString(), null);
} catch (Exception e) {
return new CalculationResult(false, null, e.getMessage());
}
})
.description("评估数学表达式")
.inputType(CalculationRequest.class)
.build();
}

public record CalculationRequest(String expression) {}
public record CalculationResult(boolean success, String result, String error) {}
}

@Service
public class PALWithToolsService {

private final ChatClient chatClient;

public String solveWithTools(String problem) {
return chatClient.prompt()
.user(u -> u.text("""
<problem>{problem}</problem>

逐步解决这个问题。对于任何计算,使用 calculate 工具。
显示你的推理,然后提供最终答案。
""").param("problem", problem))
.tools("calculate")
.call()
.content();
}
}

8. 新兴技术

8.1 情感提示

研究表明添加情感上下文可以提高性能:

public class EmotionPromptingService {

private static final String EMOTION_ENHANCED_PROMPT = """
这个任务对我们的项目成功至关重要。你的彻底分析将产生
真正的差异。请花时间并尽你所能做到最好。

{task}

请记住:你的专业知识在这里受到重视。为提供全面、
准确的响应感到自豪。
""";

public String promptWithEmotion(String task) {
return chatClient.prompt()
.user(u -> u.text(EMOTION_ENHANCED_PROMPT)
.param("task", task))
.call()
.content();
}
}

8.2 重述与响应(RaR)

让模型在回答前重述问题:

@Service
public class RephraseAndRespondService {

public String solveWithRaR(String question) {
return chatClient.prompt()
.user(u -> u.text("""
<task>重述与响应</task>

<original_question>
{question}
</original_question>

<instruction>
第 1 步:用自己的话重述这个问题以确保理解。
第 2 步:识别任何隐含的假设或要求。
第 3 步:详细回答重述的问题。
</instruction>

<format>
## 我的理解
[你对问题的重述]

## 关键假设
[识别的隐含要求]

## 答案
[你的详细响应]
</format>
""").param("question", question))
.call()
.content();
}
}

8.3 思维线(ThoT)

对于上下文密集的问题,明确地遍历相关上下文:

@Service
public class ThreadOfThoughtService {

public String solveWithThoT(String question, String context) {
return chatClient.prompt()
.user(u -> u.text("""
<context>
{context}
</context>

<question>
{question}
</question>

<instruction>
逐步地遍历上下文:
1. 识别与此问题相关的所有信息
2. 注意信息之间的关系
3. 从上下文到答案追踪思维线
4. 提供带有上下文引用的你的答案
</instruction>

<format>
## 相关信息
[上下文中的关键事实]

## 逻辑线程
[这些事实如何连接]

## 答案
[你的响应带有上下文引用]
</format>
""")
.param("context", context)
.param("question", question))
.call()
.content();
}
}

8.4 再阅读(RE2)

简单地指示模型再阅读可以提高理解力:

public String solveWithRereading(String passage, String question) {
return chatClient.prompt()
.user(u -> u.text("""
<passage>
{passage}
</passage>

<question>
{question}
</question>

<instruction>
在回答前再次仔细阅读这段文字。
注意你可能遗漏的细节。
</instruction>

再阅读后,提供你的答案:
""")
.param("passage", passage)
.param("question", question))
.call()
.content();
}

8.5 对比式思维链

显示正确和错误的推理:

public String solveWithContrastiveCoT(String problem) {
return chatClient.prompt()
.user(u -> u.text("""
<problem>
{problem}
</problem>

<instruction>
显示错误和正确的推理路径。
</instruction>

<format>
## 错误方法(常见错误)
[显示合理但错误的推理路径]
[解释为什么这是错误的]

## 正确方法
[显示正确的逐步推理]
[解释为什么每一步都有效]

## 最终答案
[正确答案]
</format>
""").param("problem", problem))
.call()
.content();
}

8.6 思维框架

先生成大纲,然后填充细节(对于长篇内容更快):

@Service
public class SkeletonOfThoughtService {

public String generateWithSkeleton(String topic) {
// 阶段 1:生成框架
String skeleton = chatClient.prompt()
.user(u -> u.text("""
<task>生成内容框架</task>
<topic>{topic}</topic>

创建详细的大纲,包含:
- 主要节 (3-5)
- 每个节的关键点 (2-4)
- 不要写完整内容,只写结构

编号为大纲格式。
""").param("topic", topic))
.call()
.content();

// 阶段 2:填充框架(可以并行化)
List<String> sections = parseSkeleton(skeleton);

List<String> filledSections = sections.parallelStream()
.map(section -> chatClient.prompt()
.user(u -> u.text("""
<task>扩展这个节</task>
<overall_topic>{topic}</overall_topic>
<section_outline>{section}</section_outline>

为这个节编写详细内容。
要彻底但简洁。
""")
.param("topic", topic)
.param("section", section))
.call()
.content())
.collect(Collectors.toList());

return String.join("\n\n", filledSections);
}
}

9. 技术选择指南

决策矩阵

技术最适用复杂度延迟成本
自我批判复杂任务,高风险中等2x2x
反思带反馈信号的任务3-5x3-5x
PACE内容生成,求精中等2-3x2-3x
自洽性数学,事实问题NxNx
步退物理,概念应用2x2x
PAL数学,计算,数据中等2x1.5x
多轮复杂多步问题3-5x3-5x
元提示提示优化可变可变
情感任何任务1x1x
RaR模糊问题1.5x1.5x
框架长篇内容中等1.5x1.5x

选择流程图

┌─────────────────────────────────────────────────────────────────────────┐
│ 技术选择流程图 │
├─────────────────────────────────────────────────────────────────────────│
│ │
│ ┌─────────────────┐ │
│ │ 什么类型的 │ │
│ │ 任务? │ │
│ └────────┬────────┘ │
│ ┌─────────────────┼─────────────────┐ │
│ ▼ ▼ ▼ │
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
│ │ 推理 │ │ 内容 │ │ 问答 │ │
│ │ /数学 │ │ 生成 │ │ /事实 │ │
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
│ │ │ │ │
│ ┌────▼────┐ ┌────▼────┐ ┌────▼────┐ │
│ │数值 │ │ 长篇 │ │ 上下文 │ │
│ │ 重型? │ │ 形式? │ │ 重型? │ │
│ └────┬────┘ └────┬────┘ └────┬────┘ │
│ Y/N │ Y/N │ Y/N │ │
│ ┌────┴────┐ ┌────┴────┐ ┌────┴────┐ │
│ Y: PAL │ Y:框架 │ Y: 思维线│ │
│ N: 思维链 │ N: ECR │ N: RaR │ │
│ +自洽性 │ +PACE │ +步退 │ │
│ └─────────┘ └─────────┘ └─────────┘ │
│ │
│ ┌─────────────────────────────────────────┐ │
│ │ 对于高风险:添加自我批判 │ │
│ │ 对于优化:添加元提示 │ │
│ │ 对于任何任务:考虑情感提示 │ │
│ └─────────────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────────────────┘

10. 生产模式

10.1 技术组合

组合技术以获得最大效果:

@Service
public class AdvancedReasoningService {

private final StepBackPromptingService stepBack;
private final SelfConsistencyService selfConsistency;
private final SelfCritiqueService selfCritique;

/**
* 组合技术:步退 + 自洽性 + 自我批判
*/
public String solveWithCombinedTechniques(String problem) {
// 1. 步退以概念为基础
StepBackResult stepBackResult = stepBack.solveWithStepBack(problem);

// 2. 在基于概念的问题上使用自洽性
String groundedProblem = String.format("""
背景知识: %s

问题: %s
""", stepBackResult.highLevelReasoning(), problem);

ConsistencyResult consistencyResult =
selfConsistency.solveWithConsistency(groundedProblem, 5);

// 3. 自我批判进行最终求精
String critiqued = selfCritique.solveWithSelfCritique(
String.format("""
问题: %s
初始答案: %s (置信度: %.2f)

验证并改进这个答案。
""", problem, consistencyResult.finalAnswer(), consistencyResult.confidence())
);

return critiqued;
}
}

10.2 自适应技术选择

@Service
public class AdaptiveTechniqueService {

private final Map<String, Object> techniques;

public String solveAdaptively(String problem, String taskType) {
// 分类任务
TaskClassification classification = classifyTask(problem, taskType);

// 根据分类选择技术
List<String> selectedTechniques = selectTechniques(classification);

// 按顺序应用技术
String result = problem;
for (String technique : selectedTechniques) {
result = applyTechnique(technique, result);
}

return result;
}

private TaskClassification classifyTask(String problem, String taskType) {
return chatClient.prompt()
.user(u -> u.text("""
分类这个任务:

任务类型: {type}
问题: {problem}

返回 JSON:
{
"requiresCalculation": true/false,
"isFactual": true/false,
"isComplex": true/false,
"hasAmbiguity": true/false,
"isLongForm": true/false,
"confidenceNeeded": "low/medium/high"
}
""")
.param("type", taskType)
.param("problem", problem))
.call()
.entity(TaskClassification.class);
}

private List<String> selectTechniques(TaskClassification c) {
List<String> techniques = new ArrayList<>();

if (c.hasAmbiguity()) techniques.add("RaR");
if (c.requiresCalculation()) techniques.add("PAL");
if (c.isComplex()) techniques.add("步退");
if (c.confidenceNeeded().equals("high")) techniques.add("自洽性");
if (c.isLongForm()) techniques.add("框架");

// 总是为高风险考虑自我批判
if (c.confidenceNeeded().equals("high")) {
techniques.add("自我批判");
}

return techniques;
}
}

参考文献

学术论文

  1. 反思 - Shinn 等人 (2023):"Reflexion: Language Agents with Verbal Reinforcement Learning"
  2. 自洽性 - Wang 等人 (2022):"Self-Consistency Improves Chain of Thought Reasoning in Language Models"
  3. PACE - Dong 等人 (2024):"PACE: Improving Prompts with Actor-Critic Editing"
  4. 步退 - Zheng 等人 (2024):"Take a Step Back: Evoking Reasoning via Abstraction in Large Language Models"
  5. PAL - Gao 等人 (2023):"PAL: Program-aided Language Models"
  6. DSPy - Khattab 等人 (2024):"DSPy: Compiling Declarative Language Model Calls into Self-Improving Pipelines"
  7. OPRO - Yang 等人 (2024):"Large Language Models as Optimizers"
  8. 情感提示 - Li 等人 (2024):"Large Language Models Understand and Can Be Enhanced by Emotional Stimuli"
  9. 思维框架 - Ning 等人 (2024):"Skeleton-of-Thought: Large Language Models Can Do Parallel Decoding"
  10. 思维线 - Zhou 等人 (2024):"Thread of Thought Unraveling Chaotic Contexts"
  11. 再阅读 - Xu 等人 (2024):"RE2: Reading Twice for Better Understanding"
  12. 通用自洽性 - Chen 等人 (2024):"Universal Self-Consistency for Large Language Model Generation"

实现资源


上一篇: 2.5 评估与版本控制下一篇: 3.2 多模态提示