从 Chain-of-Thought 到 Graph of Thoughts:LLM 推理范式的演进

近年来,大语言模型(LLM)的能力提升不仅依赖模型规模,也越来越依赖 Prompt 设计范式。其中,"思维链"(Chain-of-Thought, CoT)相关方法的发展,几乎构成了一条清晰的演进路径:

线性(List) → 多路径采样 → 树结构(Tree) → 图结构(Graph)

CoT(Chain-of-Thought):线性思维链

核心思想

CoT(Chain-of-Thought)将推理过程显式化,把原本"黑盒"的推理变成:

<输入, 推理过程, 输出>

模型通过模仿示例中的推理步骤,生成中间 reasoning,再得出答案。

关键价值

  1. 分解复杂问题:将多步问题拆解为可处理的子步骤
  2. 提升可解释性:用户可以检查每一步推理
  3. 泛化能力强:数学、常识、符号推理均适用
  4. 易用性高:Few-shot 即可触发能力

本质抽象

👉 线性结构(List)

复制代码
Step1 → Step2 → Step3 → Answer

局限性

  • 单路径推理
  • 容易"一步错,步步错"
  • 对随机性敏感

代码展示

👉 核心:在 prompt 里示范"推理过程"

python 复制代码
from openai import OpenAI
client = OpenAI()

def cot(question):
    prompt = f"""
你需要一步一步推理再给出答案。

示例:
问:小明有3个苹果,又买了2个,一共有多少?
答:3 + 2 = 5,所以答案是5。

现在回答:
问:{question}
答:
"""

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


print(cot("一个袋子里有5个球,又放进去3个,现在有多少个?"))

CoT-SC(Self-Consistency):多路径投票

核心思想

Self-Consistency(SC)是对 CoT 的增强:

多次采样 CoT → 多条推理路径 → 投票选最一致答案

类比

"一题多解,结果一致才可信"

方法

  • 多次生成 reasoning

  • 使用:

    • Majority Vote(最佳)
    • Weighted Sum / Avg(略差)

优势

  • 显著降低随机性带来的错误
  • 提升准确率(特别是数学推理)

本质抽象

👉 多条 List + 聚合

复制代码
Path1 → Answer A
Path2 → Answer B
Path3 → Answer A

→ 投票 → Answer A

局限性

  • 计算成本高(多次采样)
  • 推理仍是"独立路径",无交互

代码实现

👉 核心:多次生成 + 投票选答案

python 复制代码
from collections import Counter

def extract_answer(text):
    # 简单提取最后的数字(实际可以更复杂)
    import re
    nums = re.findall(r'\d+', text)
    return nums[-1] if nums else text


def cot_sc(question, n=5):
    answers = []

    for _ in range(n):
        resp = client.chat.completions.create(
            model="gpt-4o-mini",
            messages=[{
                "role": "user",
                "content": f"请一步一步推理并回答:{question}"
            }],
            temperature=0.7  # 关键:提高随机性
        )
        output = resp.choices[0].message.content
        answers.append(extract_answer(output))

    # 投票
    final = Counter(answers).most_common(1)[0][0]
    return final, answers


result, paths = cot_sc("一个人有10元,花了3元,又赚了5元,还剩多少?")
print("所有路径:", paths)
print("最终答案:", result)

三、ToT(Tree of Thoughts):树状思维

核心思想

ToT(Tree of Thoughts)认为:

LLM 不擅长探索和规划 → 需要显式搜索过程

因此引入:多分支思考、状态评估、搜索策略(BFS / DFS)

四个核心问题

  1. 思维拆解(Thought Decomposition):将问题拆成多个"思考单元"
  2. 思维生成(Thought Generation):Sampling(多样性)+ Proposal(受约束生成)
  3. 状态评估(Evaluation):Value function + Voting(通常由 LLM 自评)
  4. 搜索算法:BFS(广度优先)+ DFS(深度优先 + 回溯)

本质抽象

👉 树结构(Tree)

关键突破

  • 引入"搜索"概念
  • 支持回溯(Backtracking)
  • 模拟 System 2 思维

局限性

  • 搜索空间爆炸
  • 控制策略复杂
  • 仍然是"层级结构",表达能力有限

代码实现

👉 核心:多路径展开 + 评估 + 剪枝

python 复制代码
def generate_thoughts(state, k=3):
    prompt = f"""
当前问题状态:{state}
请给出接下来{ k }种可能的思路(简短)。
"""

    resp = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.7
    )

    thoughts = resp.choices[0].message.content.split("\n")
    return thoughts[:k]


def evaluate_state(state):
    prompt = f"""
请评估以下思路是否有助于解决问题(0-10分):
{state}
只输出分数。
"""

    resp = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}],
        temperature=0
    )

    return float(resp.choices[0].message.content.strip())


def tot(question, depth=3, beam_width=2):
    states = [question]

    for _ in range(depth):
        candidates = []

        for state in states:
            thoughts = generate_thoughts(state)

            for t in thoughts:
                new_state = state + " -> " + t
                score = evaluate_state(new_state)
                candidates.append((new_state, score))

        # 保留最好的几个
        candidates.sort(key=lambda x: x[1], reverse=True)
        states = [c[0] for c in candidates[:beam_width]]

    return states


print(tot("用四个4得到24"))

GoT(Graph of Thoughts):图状思维

核心思想

GoT(Graph of Thoughts)进一步提出:

人类思维不是线性或树状,而是网络结构

不同想法之间:可以交叉、可以合并、可以反复修正

核心操作

  1. Generation(生成) : A → B, C
  2. Aggregation(融合) : A + B → D
  3. Refinement(反思) : A → A'

结构特征

  • 有向图(Directed Graph)
  • 异构节点(不同类型 thoughts)
  • 节点间依赖关系

框架模块

  • Prompter(图编码)
  • Parser(解析 thoughts)
  • Scoring & Validation(评估)
  • Controller(决策控制)
  • GRS(状态记录)

本质抽象

👉 图结构(Graph)

优势

  • 更接近真实思维
  • 支持信息复用
  • 支持循环优化

局限性

  • 实现复杂度高
  • 强依赖任务设计(metric)
  • 工程成本大

代码实现

👉 核心:图结构 + 多操作(生成 / 融合 / 反思)

python 复制代码
class ThoughtGraph:
    def __init__(self):
        self.nodes = []
        self.edges = []

    def add_node(self, content):
        node_id = len(self.nodes)
        self.nodes.append(content)
        return node_id

    def add_edge(self, a, b):
        self.edges.append((a, b))


def generate(graph, node_id):
    content = graph.nodes[node_id]

    prompt = f"""
基于以下思路,生成2个新的想法:
{content}
"""

    resp = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}],
        temperature=0.7
    )

    new_thoughts = resp.choices[0].message.content.split("\n")[:2]

    new_ids = []
    for t in new_thoughts:
        nid = graph.add_node(t)
        graph.add_edge(node_id, nid)
        new_ids.append(nid)

    return new_ids


def refine(graph, node_id):
    content = graph.nodes[node_id]

    prompt = f"""
优化这个思路,让它更清晰:
{content}
"""

    resp = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}],
        temperature=0
    )

    improved = resp.choices[0].message.content
    new_id = graph.add_node(improved)
    graph.add_edge(node_id, new_id)

    return new_id


def merge(graph, id1, id2):
    prompt = f"""
融合以下两个思路:
A: {graph.nodes[id1]}
B: {graph.nodes[id2]}
"""

    resp = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=[{"role": "user", "content": prompt}],
        temperature=0
    )

    merged = resp.choices[0].message.content
    new_id = graph.add_node(merged)
    graph.add_edge(id1, new_id)
    graph.add_edge(id2, new_id)

    return new_id


def got(question):
    graph = ThoughtGraph()

    root = graph.add_node(question)

    # 生成
    children = generate(graph, root)

    # refine
    refined = refine(graph, children[0])

    # merge
    final = merge(graph, refined, children[1])

    return graph


g = got("如何提高学习效率?")
print("节点:", g.nodes)
print("边:", g.edges)

方法对比

方法 结构 复杂度 核心创新 主要问题
CoT List 显式推理链 单路径
CoT-SC 多 List ⭐⭐ 多路径投票 成本高
ToT Tree ⭐⭐⭐ 搜索+回溯 搜索爆炸
GoT Graph ⭐⭐⭐⭐ 非线性思维建模 实现复杂

总结:CoT 让模型"会解释",ToT 让模型"会探索",GoT 让模型"会思考"。

相关资料

CoT: https://export.arxiv.org/pdf/2201.11903v6

CoT-SC: https://export.arxiv.org/pdf/2203.11171v4

ToT: https://export.arxiv.org/pdf/2305.10601v1

GoT: https://export.arxiv.org/pdf/2308.09687v2

相关推荐
LarryHai613 天前
AI 大模型思维链原理:从COT到AOT,解锁大模型的推理潜力
人工智能·aot·cot·tot·大模型推理·大模型思维链
最初的↘那颗心21 天前
Prompt高级推理:COT思维链、Self-Consistency与ReAct模式实战
大模型·prompt·react·cot·思维链
YoanAILab24 天前
从 CoT、RAG 到 Dify、Deep Research:一篇讲清 AI 问答系统的两条进化路线
人工智能·cot·dify·rag·deepresearch
山顶夕景1 个月前
【VLM】HopChain视觉语言推理多跳数据合成框架
大模型·llm·cot·vlm·视觉模型
吐个泡泡v1 个月前
AI Agent 核心认知框架详解
react·cot·ai agent·认知框架
董厂长2 个月前
思维链:计算长度换计算深度,获取多次的向前传播
cot·思维链
AI资源库3 个月前
Intern-S1-Pro模型深入解析
langchain·cot·ai agent·moe·lmdeploy·intern-s1-pro·open source llm
AndrewHZ3 个月前
【AI黑话日日新】什么是隐式CoT?
人工智能·深度学习·算法·llm·cot·复杂推理
juhanishen4 个月前
Agentic AI: Chain of Thoughts (COT) CrewAI 一步步解释,从 0 到 1
人工智能·chatgpt·cot·agentic ai·agenticai·chain of though