mcpField
T
thought-based-reasoning

thought-based-reasoning

github.com/neolabhqエージェントワークフロー1.8k1.4k 回インストール9 週間前に更新
リポジトリ

thought-based-reasoning について

thought-based-reasoningは、LLMの多段階推論を促すプロンプト手法を紹介するエージェントスキルです。思考の連鎖、一貫性評価、探索、分解、ツール利用、コードによる計算を扱います。

主な機能

  1. 01思考の連鎖プロンプト
  2. 02一貫性投票
  3. 03ツリー型の思考探索
  4. 04段階的な問題分解
  5. 05ReActとコード支援推論

活用シーン

  1. 01多段階の論理問題の解決
  2. 02複数の推論経路の比較
  3. 03外部情報に基づく回答
  4. 04コードによる計算結果の検証

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/neolabhq/context-engineering-kit --skill thought-based-reasoning

インストール後、Claude Code で/thought-based-reasoningを入力して読み込まれたか確認します。

詳細

LLM の思考ベースの推論手法

概要

Chain-of-Thought(CoT)プロンプティングとその派生手法は、最終的な回答にたどり着く前に中間的な推論ステップを生成するよう LLM に促し、複雑な推論タスクのパフォーマンスを大幅に向上させます。これらの手法は、暗黙的な推論を明示化することで、モデルが問題に取り組む方法を変えます。

クイックリファレンス

手法使用する場面複雑さ精度の向上
Zero-shot CoT素早い推論が必要で、例がない場合低+20-60%
Few-shot CoT良い例があり、一貫した形式が必要な場合中+30-70%
Self-Consistency重大な判断で、確信度が必要な場合中CoT より +10-20%
Tree of Thoughts探索が必要な複雑な問題高難しいタスクで +50-70%
Least-to-Mostサブ問題を含む複数ステップの問題中+30-80%
ReAct外部情報が必要なタスク中+15-35%
PAL数学・計算問題中+10-15%
Reflexion反復的な改善、誤りからの学習高+10-20%

中核となる手法

1. Chain-of-Thought(CoT)プロンプティング

論文: “Chain of Thought Prompting Elicits Reasoning in Large Language Models”(Wei et al., 2022) 引用数: 14,255+

使用する場面
  • 複数ステップの算術問題や文章題
  • 論理的な演繹を必要とする常識的推論
  • 記号推論タスク
  • 推論を示す優れた例がある場合
仕組み

質問と回答のペアだけでなく、中間的な推論ステップを含む few-shot の例を提示します。モデルは、同様の段階的な推論を生成する方法を学習します。

プロンプトテンプレート
Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?
A: Roger started with 5 balls. 2 cans of 3 tennis balls each is 6 tennis balls. 5 + 6 = 11. The answer is 11.

Q: The cafeteria had 23 apples. If they used 20 to make lunch and bought 6 more, how many apples do they have?
A: The cafeteria had 23 apples originally. They used 20 to make lunch. So they had 23 - 20 = 3. They bought 6 more apples, so they have 3 + 6 = 9. The answer is 9.

Q: [YOUR QUESTION HERE]
A:
強み
  • 推論タスクの精度を大幅に向上
  • 解釈可能な中間ステップ
  • 大規模モデル(>100B パラメーター)で良好に機能
制限事項
  • 優れた例を用意する必要がある
  • 小規模モデルでは効果が低い
  • 計算ミスが発生する場合がある

2. Zero-shot Chain-of-Thought

論文: “Large Language Models are Zero-Shot Reasoners”(Kojima et al., 2022) 引用数: 5,985+

使用する場面
  • 例がない場合
  • 素早い推論が必要な場合
  • さまざまなタスクにわたる汎用的な推論
  • few-shot の例を作成する前のプロトタイピング
仕組み

プロンプトに「Let's think step by step」(または類似のフレーズ)を追加するだけです。これにより、例がなくてもモデルが推論ステップを生成するよう促されます。

プロンプトテンプレート
Q: A juggler can juggle 16 balls. Half of the balls are golf balls, and half of the golf balls are blue. How many blue golf balls are there?

Let's think step by step.

代替のトリガーフレーズ:

  • 「正しい答えを得られるよう、これを段階的に考えてみましょう。」
  • 「これを分解して考えてみましょう。」
  • 「これに体系的に取り組んでみましょう。」
  • 「まず、問題を理解しましょう……」
2 段階のアプローチ(より堅牢)

ステージ 1 - 推論の抽出:

Q: [QUESTION]
A: Let's think step by step.

ステージ 2 - 回答の抽出:

[REASONING FROM STAGE 1]
Therefore, the answer is
強み
  • 例を用意する必要がない
  • さまざまなタスクに汎化する
  • 実装が簡単
制限事項
  • few-shot CoT より効果が低い
  • 冗長または無関係な推論を生成する場合がある
  • 正確な言い回しに敏感

3. Self-Consistency

論文: “Self-Consistency Improves Chain of Thought Reasoning in Language Models”(Wang et al., 2022) 引用数: 5,379+

使用する場面
  • 確信度が必要な重大な判断
  • 複数の有効な推論経路がある問題
  • 出力のばらつきを抑えたい場合
  • 推論の正しさを検証する場合
仕組み

多様な推論経路を複数サンプリングし、多数決によって最も一貫した回答を選びます。正しい答えには、複数の推論経路でたどり着けるという考え方に基づいています。

プロンプトテンプレート
[Use any CoT prompt - zero-shot or few-shot]

[Generate N samples with temperature > 0]

[Extract final answers from each sample]

[Return the most frequent answer (majority vote)]
実装例
def self_consistency(prompt, n_samples=5, temperature=0.7):
    answers = []
    for _ in range(n_samples):
        response = llm.generate(prompt, temperature=temperature)
        answer = extract_answer(response)
        answers.append(answer)

    # Majority vote
    return Counter(answers).most_common(1)[0][0]
強み
  • 単一経路の CoT より精度を大幅に向上
  • 確信度の指標(回答の一致度)を提示
  • タスクに依存しない改善
制限事項
  • 計算コストが高くなります(生成回数がN倍になります)
  • 抽出可能な離散的な回答が必要です
  • サンプル数が約10~20を超えると、効果が逓減します

4. Tree of Thoughts(ToT)

論文: 「Tree of Thoughts: Deliberate Problem Solving with Large Language Models」(Yao ほか、2023年) 引用数: 3,026件以上

使用する場面
  • 探索やバックトラッキングを必要とする複雑な問題
  • 最初の判断が重要なタスク
  • 創造的な問題解決(執筆、パズル)
  • CoT だけでは正解率が50%未満の場合
仕組み

CoT を一般化し、各ノードを「思考」(一貫した言語単位)とするツリー構造にします。自己評価を用いた探索アルゴリズム(BFS/DFS)で、有望な推論経路を探索して選択します。

プロンプトテンプレート

思考の生成:

Given the current state:
[STATE]

Generate 3-5 possible next steps to solve this problem.

状態の評価:

Evaluate if the following partial solution is:
- "sure" (definitely leads to solution)
- "maybe" (could potentially work)
- "impossible" (cannot lead to solution)

Partial solution:
[THOUGHTS SO FAR]

BFS/DFS 探索:

def tree_of_thoughts(problem, max_depth=3, beam_width=3):
    queue = [(problem, [])]  # (state, thought_path)

    while queue:
        state, path = queue.pop(0)

        if is_solved(state):
            return path

        # Generate candidate thoughts
        thoughts = generate_thoughts(state, k=5)

        # Evaluate and keep top-k
        evaluated = [(t, evaluate(state, t)) for t in thoughts]
        top_k = sorted(evaluated, key=lambda x: x[1])[:beam_width]

        for thought, score in top_k:
            if score != "impossible":
                new_state = apply_thought(state, thought)
                queue.append((new_state, path + [thought]))

    return None
例:24 ゲーム
Problem: Use 4, 9, 10, 13 to get 24 (use +, -, *, / and each number once)

Thought 1: 13 - 9 = 4 (Now have: 4, 4, 10)
Evaluation: "maybe" - have two 4s and 10, could work

Thought 2: 10 - 4 = 6 (Now have: 4, 6, 13)
Evaluation: "maybe" - 4 * 6 = 24, need to use 13

Thought 3: 4 + 9 = 13 (Now have: 10, 13, 13)
Evaluation: "impossible" - no way to get 24 from these
強み
  • 難しいタスクでのパフォーマンスを大幅に向上させます(24 ゲームで4% → 74%)
  • バックトラッキングと探索が可能になります
  • 自己評価によってエラーを早期に検出します
制限事項
  • 計算コストが大幅に高くなります
  • タスクに応じた思考の分解が必要です
  • 実装が複雑です

5. Least-to-Most Prompting

論文: 「Least-to-Most Prompting Enables Complex Reasoning in Large Language Models」(Zhou ほか、2022年) 引用数: 1,466件以上

使用する場面
  • 例示より難しい問題
  • 合成的一般化タスク
  • 明確なサブ問題を含む複数ステップの問題
  • 記号操作や SCAN のようなタスク
仕組み

2段階のプロセスです。

  1. 分解: 複雑な問題をより簡単なサブ問題に分解します
  2. 順次解決: 前の回答を使いながら、サブ問題を順番に解決します
プロンプトテンプレート

ステージ1:分解

Q: Four years ago, Kody was only half as old as Mohamed. If Mohamed is currently twice as old as 30 years old, how old is Kody?

To solve "Four years ago, Kody was only half as old as Mohamed. If Mohamed is currently twice as old as 30 years old, how old is Kody?", we need to first solve:
- "If Mohamed is currently twice as old as 30 years old, how old is Mohamed?"
Then we need to solve:
- "Four years ago, Kody was only half as old as Mohamed. How old is Kody?"

ステージ2:順次解決

Q: If Mohamed is currently twice as old as 30 years old, how old is Mohamed?
A: Mohamed is twice as old as 30, so Mohamed is 30 * 2 = 60 years old.

Q: Four years ago, Kody was only half as old as Mohamed. Mohamed is 60 years old. How old is Kody?
A: Four years ago, Mohamed was 60 - 4 = 56 years old. Kody was half as old, so Kody was 56 / 2 = 28 years old four years ago. Therefore, Kody is currently 28 + 4 = 32 years old.

The answer is 32.
強み
  • より難しい問題への一般化に優れています
  • 合成的なタスクで効果を発揮します
  • 問題を明示的に分解することで、解釈しやすくなります
制限事項
  • 2段階のプロンプトが必要です
  • 新しい構造では、分解の段階で失敗することがあります
  • 単段階の CoT より設定が複雑です

6. ReAct(推論 + 行動)

論文: 「ReAct: Synergizing Reasoning and Acting in Language Models」(Yao ほか、2022年) 引用数: 5,012件以上

使用する場面
  • 外部情報(検索、API)を必要とするタスク
  • 対話的な意思決定環境
  • 複数ホップの質問応答
  • 純粋な推論では幻覚が生じる場合
仕組み

推論の記録(「思考」)と行動(「行動」)、観察(「観察」)を交互に行います。推論によって行動を計画し、行動によって推論に必要な新しい情報を得ます。

プロンプトテンプレート
Question: What is the elevation range for the area that the eastern sector of the Colorado orogeny extends into?

Thought 1: I need to search Colorado orogeny, find the area that the eastern sector extends into, then find the elevation range of that area.
Action 1: Search[Colorado orogeny]
Observation 1: The Colorado orogeny was an episode of mountain building (an orogeny) in Colorado and surrounding areas.

Thought 2: It does not mention the eastern sector. So I need to look up eastern sector.
Action 2: Lookup[eastern sector]
Observation 2: The eastern sector extends into the High Plains.

Thought 3: The eastern sector extends into the High Plains. I need to search High Plains and find its elevation range.
Action 3: Search[High Plains]
Observation 3: High Plains refers to one of two distinct land regions: High Plains (United States)...

Thought 4: I need to search High Plains (United States).
Action 4: Search[High Plains (United States)]
Observation 4: The High Plains are a subregion of the Great Plains... elevation of around 1,800 to 7,000 ft (550 to 2,130 m).

Thought 5: The elevation range is 1,800 to 7,000 ft.
Action 5: Finish[1,800 to 7,000 ft]
アクションの種類
  • Search[query] - 情報を検索します
  • Lookup[keyword] - 現在のコンテキストからキーワードを検索します
  • Finish[answer] - 最終回答を返します
強み
  • 外部知識に基づくことで、幻覚を減らします
  • 行動の記録を解釈できます
  • 適応的な推論によって例外に対処します
制限事項
  • 外部ツールとの統合が必要です
  • オーケストレーションが複雑になります
  • アクション空間を定義する必要があります

7. PAL(Program-Aided Language Models)

論文: 「PAL: Program-aided Language Models」(Gao ほか、2022年) 引用数: 608件以上

使用する場面
  • 数学的/算術的な推論
  • 正確な計算が必要な問題
  • 記号操作
  • CoT で計算ミスが生じる場合
仕組み

自然言語による推論ではなく、コード(通常は Python)を生成します。コードを実行して回答を得ます。LLM が分解を行い、インタープリターが計算を処理します。

プロンプトテンプレート
Q: Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?

# solution in Python:
def solution():
    """Roger has 5 tennis balls. He buys 2 more cans of tennis balls. Each can has 3 tennis balls. How many tennis balls does he have now?"""
    tennis_balls_initial = 5
    bought_cans = 2
    tennis_balls_per_can = 3
    tennis_balls_bought = bought_cans * tennis_balls_per_can
    tennis_balls_total = tennis_balls_initial + tennis_balls_bought
    return tennis_balls_total

Q: The bakers at the Beverly Hills Bakery baked 200 loaves of bread on Monday morning. They sold 93 loaves in the morning and 39 loaves in the afternoon. A grocery store returned 6 unsold loaves. How many loaves of bread did they have left?

# solution in Python:
def solution():
    """The bakers baked 200 loaves. They sold 93 in morning, 39 in afternoon. A store returned 6. How many left?"""
    loaves_baked = 200
    loaves_sold_morning = 93
    loaves_sold_afternoon = 39
    loaves_returned = 6
    loaves_left = loaves_baked - loaves_sold_morning - loaves_sold_afternoon + loaves_returned
    return loaves_left
強み
  • 算術の誤りをなくします
  • 明確な変数名によって解釈しやすくなります
  • コード実行を活用して検証できます
制限事項
  • コードインタープリターが必要です
  • 計算を伴わない推論には適していません
  • モデルが構文的に正しいコードを生成する必要があります

8. Auto-CoT

論文:「Automatic Chain of Thought Prompting in Large Language Models」(Zhang et al., 2022) 引用数:838+

使用する場面
  • 手動で作成した例がない場合
  • 少数例 CoT の設定を自動化したい場合
  • 多くのタスクに CoT を拡張する場合
  • Zero-shot CoT では不十分な場合
仕組み
  1. 多様性に基づいて質問をクラスタリングします
  2. Zero-shot CoT を使い、代表的な質問の推論チェーンを生成します
  3. これらの自動生成されたチェーンを少数例の例として使用します
プロンプトテンプレート

ステップ 1:多様なデモンストレーションを生成する

# Cluster questions
clusters = cluster_questions(all_questions, k=8)

# For each cluster, pick representative and generate CoT
demonstrations = []
for cluster in clusters:
    question = select_representative(cluster)
    reasoning = zero_shot_cot(question)  # "Let's think step by step"
    demonstrations.append((question, reasoning))

ステップ 2:少数例の例として使用する

Q: [Demo question 1]
A: Let's think step by step. [Generated reasoning 1]

Q: [Demo question 2]
A: Let's think step by step. [Generated reasoning 2]

...

Q: [New question]
A: Let's think step by step.
長所
  • 手作業で例を作成する必要がありません
  • 多様性サンプリングにより堅牢性が向上します
  • 手動の CoT と同等の性能を実現します
制限事項
  • 品質は Zero-shot CoT の品質に依存します
  • クラスタリングには類似度の指標が必要です
  • 生成されたチェーンには誤りが含まれる場合があります

9. Reflexion

論文:「Reflexion: Language Agents with Verbal Reinforcement Learning」(Shinn et al., 2023) 引用数:2,179+

使用する場面
  • 複数回の試行を通じて反復的に改善する場合
  • ファインチューニングなしで誤りから学習する場合
  • 複雑なコーディングや意思決定のタスク
  • 1 回の推論では不十分な場合
仕組み

タスクに失敗した後、エージェントは何がうまくいかなかったかを分析する口頭での「振り返り」を生成します。この振り返りはメモリに保存され、後続の試行で同じ誤りを繰り返さないように利用されます。

プロンプトテンプレート

初回の試行:

Task: [TASK DESCRIPTION]

Thought: [REASONING]
Action: [ACTION]
...
Result: [FAILURE/PARTIAL SUCCESS]

振り返り:

The previous attempt failed because:
1. [SPECIFIC ERROR ANALYSIS]
2. [WHAT SHOULD HAVE BEEN DONE]
3. [KEY INSIGHT FOR NEXT ATTEMPT]

Reflection: In the next attempt, I should...

後続の試行(メモリを使用):

Task: [TASK DESCRIPTION]

Previous reflections:
- [REFLECTION 1]
- [REFLECTION 2]

Using these insights, I will now attempt the task again.

Thought: [IMPROVED REASONING]
Action: [BETTER ACTION]
例:コード生成
Task: Write a function to find the longest palindromic substring.

Attempt 1: [CODE WITH BUG]
Test Result: Failed on "babad" - expected "bab" or "aba", got "b"

Reflection: My solution only checked single characters. I need to:
1. Consider substrings of all lengths
2. Use expand-around-center technique for efficiency
3. Track both start position and maximum length

Attempt 2: [IMPROVED CODE USING REFLECTION]
Test Result: Passed all tests
長所
  • 重みを更新せずに誤りから学習します
  • HumanEval で 91%を達成します(GPT-4 の 80%を上回ります)
  • 洞察のエピソード記憶を構築します
制限事項
  • 複数回の試行が必要です
  • 長時間のセッションではメモリの管理が必要です
  • 振り返りの品質が改善度に影響します

意思決定マトリックス:使用する手法の選び方

                           Need Examples?
                          /              \
                        No                Yes
                        |                  |
                Zero-shot CoT          Few-shot CoT
                        |                  |
                Need higher accuracy?  Need computation?
                /                \           |
              Yes               No          PAL
               |                |
    Self-Consistency    Done with CoT
               |
        Still not enough?
        /              \
      Yes              No
       |                |
  Problem decomposable?  Done
  /                    \
Yes                    No
 |                      |
Least-to-Most     Need exploration?
                  /              \
                Yes              No
                 |                |
          Tree of Thoughts   Need external info?
                             /              \
                           Yes              No
                            |                |
                          ReAct         Need iteration?
                                        /           \
                                      Yes           No
                                       |             |
                                   Reflexion      Use CoT

ベストプラクティス

1. シンプルに始める

まず Zero-shot CoT(「段階的に考えてみましょう」)を使い、必要に応じてより複雑な手法に進みます。

2. タスクに合った手法を選ぶ
  • 数学/論理:CoT、PAL、Self-Consistency
  • マルチホップ QA:ReAct、Least-to-Most
  • 創造的なタスク/パズル:Tree of Thoughts
  • 反復的なタスク:Reflexion
3. 手法を組み合わせる

手法は相互に補完することがよくあります:

  • 堅牢な事実に基づく回答には ReAct + Self-Consistency
  • 複雑な計算の探索には ToT + PAL
  • 難しい多段階問題には Least-to-Most + Reflexion
4. プロンプトエンジニアリングのヒント
  • 明確なステップマーカー(「Step 1:」、「First,」など)を使用します
  • エッジケースを網羅する多様な例を含めます
  • すべての例で一貫した形式を使用します
  • 検証の手順(「確認してみましょう……」)を追加します

よくある間違い

間違い問題となる理由対処法
単純な検索に CoT を使う不要なトークンとレイテンシが増えます多段階の推論に限って使用します
Self-Consistency のサンプル数が少なすぎる多数決には十分なサンプルが必要です最低 5~10 個のサンプルを使用します
出力を確認せず、汎用的な「段階的に考える」を使うモデルが無関係な推論を生成する場合があります推論があるかどうかだけでなく、その品質を検証します
トレードオフを理解せずに手法を混在させる利点がないまま計算コストが増えます各手法がどのような価値をもたらすかを理解します
コードインタープリターなしで PAL を使う実行できなければコード生成は役に立ちません実行環境が利用可能であることを確認します
少数例 CoT で例の品質をテストしない質の悪い例は推論の質を下げます例が問題を正しく解けることを検証します
線形な問題に Tree of Thoughts を適用する効果がないのに大きなオーバーヘッドが発生します探索が必要な場合にのみ ToT を使用します

参考文献

  1. Wei, J. et al. (2022). "思考の連鎖プロンプトは大規模言語モデルの推論を引き出す。" arXiv:2201.11903

  2. Kojima, T. et al. (2022). "大規模言語モデルはゼロショット推論を行う。" arXiv:2205.11916

  3. Wang, X. et al. (2022). "自己一貫性は言語モデルの思考の連鎖による推論を向上させる。" arXiv:2203.11171

  4. Yao, S. et al. (2023). "思考の木:大規模言語モデルによる熟考を伴う問題解決。" arXiv:2305.10601

  5. Zhou, D. et al. (2022). "最小から最大へのプロンプトは大規模言語モデルによる複雑な推論を可能にする。" arXiv:2205.10625

  6. Yao, S. et al. (2022). "ReAct:言語モデルにおける推論と行動の相乗効果。" arXiv:2210.03629

  7. Gao, L. et al. (2022). "PAL:プログラム支援型言語モデル。" arXiv:2211.10435

  8. Zhang, Z. et al. (2022). "大規模言語モデルにおける思考の連鎖プロンプトの自動化。" arXiv:2210.03493

  9. Shinn, N. et al. (2023). "Reflexion:言語による強化学習を用いた言語エージェント。" arXiv:2303.11366

コメント