的大規模言語モデル(LLM)を活用した長文ドキュメント要約は、現代のAIアプリケーションにおいて最も重要なユースケースの一つです。しかし、適切なプロンプト戦略を選択しないと、処理コストが爆発的に増加したり、応答品質が著しく低下したりします。本稿では、代表的な3つの戦略(Map-Reduce、Stuff、Refine)を徹底比較し、月間1000万トークンという現実的なワークロードでのコスト分析を交えながら、HolySheep AIを活用した実装方法を解説します。

3大要約プロンプト戦略の概要

長文ドキュメントの要約において、以下の3つのアプローチが広く採用されています。

Stuff戦略(チェイン密度化)

Stuff戦略は、ドキュメント全体を単一のプロンプトに「詰め込む」最もシンプルなアプローチです。LLMは文脈ウィンドウ内でドキュメント全体を一括処理するため、一貫性のある出力を生成しやすいのが优点です。

# Stuff戦略の実装例(HolySheep API)
import requests
import json

BASE_URL = "https://api.holysheep.ai/v1"

def summarize_with_stuff(document_text, api_key):
    """
    Stuff戦略:ドキュメント全体を1つのリクエストで処理
    短~中程度のドキュメント(最大128Kトークン)に最適
    """
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    
    prompt = f"""以下のドキュメントを包括的に要約してください。要点を5つ以内にまとめてください。

ドキュメント内容:
{document_text}

要約:"""

    payload = {
        "model": "gpt-4.1",
        "messages": [
            {"role": "user", "content": prompt}
        ],
        "max_tokens": 2000,
        "temperature": 0.3
    }
    
    response = requests.post(
        f"{BASE_URL}/chat/completions",
        headers=headers,
        json=payload
    )
    
    return response.json()

使用例

with open("document.txt", "r") as f: document = f.read() result = summarize_with_stuff(document, "YOUR_HOLYSHEEP_API_KEY") print(result["choices"][0]["message"]["content"])

Map-Reduce戦略(分割統治型)

Map-Reduceは、ドキュメントを小さなチャンクに分割し(Map)、各チャンクを個別に処理してから、最終的な要約を生成する(Reduce)二段階アプローチです。

# Map-Reduce戦略の実装例(HolySheep API)
import requests
import json
from typing import List

BASE_URL = "https://api.holysheep.ai/v1"

def chunk_text(text: str, chunk_size: int = 4000) -> List[str]:
    """ドキュメントを指定サイズのチャンクに分割"""
    words = text.split()
    chunks = []
    current_chunk = []
    current_length = 0
    
    for word in words:
        current_length += len(word) + 1
        if current_length > chunk_size:
            chunks.append(" ".join(current_chunk))
            current_chunk = [word]
            current_length = len(word) + 1
        else:
            current_chunk.append(word)
    
    if current_chunk:
        chunks.append(" ".join(current_chunk))
    return chunks

def map_phase_summary(chunk: str, api_key: str) -> str:
    """Mapフェーズ:各チャンクの核心を抽出"""
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    
    prompt = f"""このテキストセクションの核心的な内容を1-2文で抽出してください。

テキスト: {chunk}

核心ポイント:"""

    payload = {
        "model": "gpt-4.1",
        "messages": [{"role": "user", "content": prompt}],
        "max_tokens": 500,
        "temperature": 0.3
    }
    
    response = requests.post(
        f"{BASE_URL}/chat/completions",
        headers=headers,
        json=payload
    )
    return response.json()["choices"][0]["message"]["content"]

def reduce_phase_summary(summaries: List[str], api_key: str) -> str:
    """Reduceフェーズ:部分要約を統合して最終要約を生成"""
    headers = {
        "Authorization": f"Bearer {api_key}",
        "Content-Type": "application/json"
    }
    
    combined_summaries = "\n\n".join([
        f"[セクション{i+1}] {s}" for i, s in enumerate(summaries)
    ])
    
    prompt = f"""以下のセクション별 요약을統合して、ドキュメント全体の包括的な要約を生成してください。

{combined_summaries}

統合要約:"""

    payload = {
        "model": "gpt-4.1",
        "messages": [{"role": "user", "content": prompt}],
        "max_tokens": 2000,
        "temperature": 0.3
    }
    
    response = requests.post(
        f"{BASE_URL}/chat/completions",
        headers=headers,
        json=payload
    )
    return response.json()["choices"][0]["message"]["content"]

def map_reduce_summarize(document: str, api_key: str) -> str:
    """Map-Reduce戦略によるドキュメント要約"""
    # チャンク分割
    chunks = chunk_text(document, chunk_size=4000)
    print(f"ドキュメントを{len(chunks)}個のチャンクに分割")
    
    # Mapフェーズ:各チャンクを並列処理
    partial_summaries = [map_phase_summary(chunk, api_key) for chunk in chunks]
    
    # Reduceフェーズ:統合要約生成
    final_summary = reduce_phase_summary(partial_summaries, api_key)
    
    return final_summary

使用例

with open("large_document.txt", "r") as f: document = f.read() result = map_reduce_summarize(document, "YOUR_HOLYSHEEP_API_KEY") print(result)

Refine戦略(反復改善型)

Refine戦略は、ドキュメントを逐次的に処理しながら、要約を段階的に改善していくアプローチです。各チャンクを順番に処理し、既存の要約に新しい情報を統合していきます。

戦略比較表:処理方式と特徴

🔥 HolySheep AIを使ってみる

直接AI APIゲートウェイ。Claude、GPT-5、Gemini、DeepSeekに対応。VPN不要。

👉 無料登録 →

評価項目 Stuff Map-Reduce Refine
処理方式 一括処理 並列Map → 集約Reduce 逐次反復処理
文脈保持 ★★★★★(完全) ★★★☆☆(チャンク境界で損失) ★★★★☆(反復で回復)
APIコール数 1回 チャンク数+1回