Chuyển đến nội dung chính

レッスン 15: 高度なプロンプト — CoT、ToT、ReAct

高度なプロンプト手法: 思考連鎖、思考ツリー、自己一貫性、ReAct パターン。 LLM を使用して複雑な問題を解決します。

🧠 AI と ML — レッスン 14 レッスン 15: 高度なプロンプト — CoT、ToT、 反応する

AI と LLM: 基本から高度まで

パート 4: プロンプトと RAG

xdev.asia

概要

タスクが複雑である場合 (複数ステップの数学、論理的推論、コーディング)、標準的なプロンプトでは十分ではありません。高度なプロンプト技術は、LLM が推測ではなく「段階的に考える」のに役立ちます。


1. 思考の連鎖 (CoT)

検出

魏ら。 (2022) 発見: 「段階的に考えてみましょう」 を追加するか、推論を伴う例を示すだけで、モデルは問題をより適切に解決します。

ゼロショット CoT

from openai import OpenAI
client = OpenAI()

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

# Không có CoT → sai
bad = ask("Roger có 5 quả bóng tennis. Anh ấy mua 2 hộp nữa, "
          "mỗi hộp 3 quả. Hỏi Roger có bao nhiêu quả bóng?")
# Có thể trả lời "8" (sai)

# Có CoT → đúng
good = ask("Roger có 5 quả bóng tennis. Anh ấy mua 2 hộp nữa, "
           "mỗi hộp 3 quả. Hỏi Roger có bao nhiêu quả bóng?\n\n"
           "Hãy suy nghĩ từng bước:")
# "Bước 1: Roger ban đầu có 5 quả
#  Bước 2: Mua 2 hộp × 3 quả = 6 quả
#  Bước 3: Tổng = 5 + 6 = 11 quả"  ✅

少数ショットの CoT

few_shot_cot = """Q: Cửa hàng có 15 táo. Bán 3 rổ mỗi rổ 4 quả. Còn lại bao nhiêu?
A: Bước 1: Số táo đã bán = 3 rổ × 4 quả = 12 quả
   Bước 2: Còn lại = 15 - 12 = 3 quả
   Đáp án: 3 quả

Q: Xe tải chở 30 thùng hàng. Dỡ xuống 7 thùng tại điểm A, nhận thêm 5 thùng.
   Tại điểm B dỡ 12 thùng. Còn bao nhiêu thùng?
A:"""

# Model sẽ tự động follow pattern step-by-step

2. 自己一貫性

高温で CoT を何度も実行し、多数決を獲得します。

from collections import Counter
import re

def self_consistency(question: str, n_samples: int = 5) -> str:
    """Chạy n lần, lấy đáp án xuất hiện nhiều nhất"""
    answers = []

    for _ in range(n_samples):
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": f"{question}\n\nHãy suy nghĩ từng bước:"}],
            temperature=0.7  # Diversity
        )
        text = response.choices[0].message.content

        # Extract final answer (customize theo task)
        match = re.search(r'(?:đáp án|kết quả|answer)[:=\s]+(\S+)', text, re.IGNORECASE)
        if match:
            answers.append(match.group(1))

    # Majority vote
    if answers:
        most_common = Counter(answers).most_common(1)[0][0]
        return most_common
    return "Không tìm được đáp án"

result = self_consistency(
    "Tính: (15 + 7) × 3 - 12 ÷ 4",
    n_samples=5
)
print(f"Đáp án: {result}")  # "63"

どのような場合に使用するか: 重要な問題です。待ち時間とコストの増加を受け入れます。


3. 思考の木 (ToT)

CoT は単一のパスをたどります。 ToT 推論の複数の分岐を探索し、各分岐を評価します。

                     [Problem]
                    /    |    \
               [Path1] [Path2] [Path3]
              /   \      |      /  \
           [A1]  [A2]  [B1]  [C1] [C2]
            ↓     ✗    ↓      ✗    ↓
          [A1a]       [B1a]       [C2a]  ← Best path
def tree_of_thought(problem: str, n_thoughts: int = 3, depth: int = 2) -> str:
    """Simplified ToT implementation"""

    def generate_thoughts(state: str, n: int) -> list[str]:
        """Generate n candidate next thoughts"""
        prompt = f"""Bài toán: {problem}

Trạng thái hiện tại: {state}

Đề xuất {n} hướng giải quyết tiếp theo khác nhau.
Format: 1. ... | 2. ... | 3. ..."""
        resp = client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": prompt}],
            temperature=0.8
        )
        text = resp.choices[0].message.content
        return [t.strip() for t in text.split("|")]

    def evaluate_thought(thought: str) -> float:
        """Score a thought path (0-1)"""
        prompt = f"""Đánh giá mức độ hứa hẹn của hướng giải này (0-10):
Bài toán: {problem}
Hướng giải: {thought}
Chỉ trả về một số từ 0-10."""
        resp = client.chat.completions.create(
            model="gpt-4o",
            messages=[{"role": "user", "content": prompt}],
            temperature=0
        )
        try:
            return float(resp.choices[0].message.content.strip()) / 10
        except:
            return 0.5

    # BFS: expand best thoughts
    current_thoughts = ["Bắt đầu phân tích bài toán"]
    for _ in range(depth):
        all_expansions = []
        for thought in current_thoughts:
            expansions = generate_thoughts(thought, n_thoughts)
            scored = [(e, evaluate_thought(e)) for e in expansions]
            all_expansions.extend(scored)

        # Keep top n_thoughts
        all_expansions.sort(key=lambda x: x[1], reverse=True)
        current_thoughts = [t for t, _ in all_expansions[:n_thoughts]]

    return current_thoughts[0]  # Best path

4. ReAct (理由 + 行動)

ReAct は、推論 と アクション (ツールの呼び出し) をループ内で組み合わせます。

Thought: Tôi cần tìm dân số Việt Nam
Action: search("dân số Việt Nam 2024")
Observation: Kết quả: ~98 triệu người
Thought: Tôi có đủ thông tin để trả lời
Answer: Dân số Việt Nam năm 2024 khoảng 98 triệu người
from openai import OpenAI
import json

client = OpenAI()

# Định nghĩa tools
tools = [
    {
        "type": "function",
        "function": {
            "name": "search_web",
            "description": "Tìm kiếm thông tin trên web",
            "parameters": {
                "type": "object",
                "properties": {
                    "query": {"type": "string", "description": "Search query"}
                },
                "required": ["query"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "calculate",
            "description": "Tính toán biểu thức toán học",
            "parameters": {
                "type": "object",
                "properties": {
                    "expression": {"type": "string"}
                },
                "required": ["expression"]
            }
        }
    }
]

def execute_tool(name: str, args: dict) -> str:
    """Thực thi tool và trả về kết quả"""
    if name == "search_web":
        # Trong thực tế: gọi search API (SerpAPI, Tavily, etc.)
        return f"[Mock] Kết quả tìm kiếm cho '{args['query']}': ..."
    elif name == "calculate":
        try:
            result = eval(args["expression"])  # Chỉ demo, không dùng eval production!
            return str(result)
        except Exception as e:
            return f"Error: {e}"
    return "Tool không tồn tại"

def react_agent(user_question: str, max_iterations: int = 5) -> str:
    """ReAct agent loop"""
    messages = [
        {"role": "system", "content": "Bạn là assistant thông minh. Dùng tools khi cần thiết."},
        {"role": "user", "content": user_question}
    ]

    for iteration in range(max_iterations):
        response = client.chat.completions.create(
            model="gpt-4o",
            messages=messages,
            tools=tools,
            tool_choice="auto"
        )

        msg = response.choices[0].message

        # Không có tool call → trả lời cuối
        if not msg.tool_calls:
            return msg.content

        # Có tool calls → thực thi và loop tiếp
        messages.append(msg)
        for tool_call in msg.tool_calls:
            args = json.loads(tool_call.function.arguments)
            result = execute_tool(tool_call.function.name, args)
            messages.append({
                "role": "tool",
                "tool_call_id": tool_call.id,
                "content": result
            })

    return "Đã hết số vòng lặp tối đa"

# Chạy
answer = react_agent("Tính GDP bình quân đầu người của Việt Nam năm 2023?")
print(answer)

5. 反省と自己批判

def with_reflection(question: str) -> str:
    """Generate → Critique → Improve"""

    # Bước 1: Initial response
    initial = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": question}]
    ).choices[0].message.content

    # Bước 2: Tự phê bình
    critique = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "user", "content": question},
            {"role": "assistant", "content": initial},
            {"role": "user", "content": "Hãy phê bình câu trả lời trên. "
             "Chỉ ra điểm yếu, thiếu sót, hoặc có thể cải thiện."}
        ]
    ).choices[0].message.content

    # Bước 3: Cải thiện dựa trên critique
    improved = client.chat.completions.create(
        model="gpt-4o",
        messages=[
            {"role": "user", "content": question},
            {"role": "assistant", "content": initial},
            {"role": "user", "content": f"Phê bình: {critique}\n\nViết lại câu trả lời cải thiện:"}
        ]
    ).choices[0].message.content

    return improved

6. テクニックを比較する

エンジニアリングコスト (トークン)レイテンシ良いこと
標準最低最速簡単なタスク
ゼロショットCoT低い速い数学、基本的な論理
少数ショットCoT平均平均タスクには特定のパターンがあります
自己一貫性高 (×N)遅い (×N)重要な精度
へ非常に高い非常に遅い複雑な計画
反応する平均バリエーションツールの使い方、検索
反射高 (×3)遅い品質重視の出力

概要

Nguyên tắc chọn kỹ thuật:
- Task đơn giản           → Standard / Zero-shot CoT
- Math / Logic / Coding   → Few-shot CoT hoặc Self-Consistency
- Cần external knowledge  → ReAct với search tool
- Complex multi-step      → ToT hoặc Reflection
- Accuracy > Speed        → Self-Consistency (majority vote)

次の記事: RAG (Retrieval Augmented Generation) — LLM が新しい情報や個人データを知らないという問題の解決策。