Graph rag
Microsoft GraphRAGを使ってドキュメント検索システムを構築・運用する際に使用。「GraphRAGでドキュメントを検索したい」「ナレッジグラフベースのRAGを作りたい」「ローカルとグローバル両方の検索がしたい」という依頼で起動。セットアップ→インデックス構築→クエリ(Local/Global/DRIFT)の流れで進める。From its SKILL.md
npx -y skills add mostlyfine/rag-lab --skill graph-ragAssembled from the repository path, not quoted from the project. Check it against their README if it does not work.
2 things to look at
- no licenseNo license file was found in the repository. Code published without one is not open source by default, so using it at work is a question for whoever answers licensing questions where you are.
- 0 stars0 stars. Stars are a popularity signal and not a quality one, but at this level it is likely that nobody has read this closely except its author, and you would be relying on your own review.
SKILL.md
11.8 KB, as published. Nobody here has run it
GraphRAG Document Search
Microsoft GraphRAGを使ったドキュメント検索システムの構築・運用スキル。 LLMでテキストからナレッジグラフを生成し、Local/Global/DRIFT検索で高品質な回答を返す。
前提条件チェック
# MUSTチェック — 欠けていれば中止
missing_must=0
command -v uv >/dev/null 2>&1 || { echo "❌ [MUST] uv が見つかりません"; missing_must=1; }
if [ "$missing_must" -eq 1 ]; then
echo "必須コマンドが不足しています。下記の手順でインストール後、再実行してください。"
return 1 2>/dev/null || exit 1
fi
echo "✅ 必須コマンドの確認 OK。処理を続行します。"
# WANTチェック — チェックのみ
command -v gh >/dev/null 2>&1 || echo "⚠️ [WANT] gh なし(Copilot APIキーは手動設定も可: export GITHUB_COPILOT_API_KEY=...)"
command -v pdftotext >/dev/null 2>&1 || echo "ℹ️ [WANT] pdftotext なし(PDF を扱う場合は要インストール)"
| コマンド | 区分 | インストール方法 |
|---|---|---|
uv | MUST | curl -LsSf https://astral.sh/uv/install.sh | sh |
gh | WANT | macOS: brew install gh / 公式: https://cli.github.com |
pdftotext | WANT | macOS: brew install poppler / Ubuntu: apt install poppler-utils |
gh のセットアップ(初回のみ、Copilot API を使う場合):
gh auth login
概要
GraphRAGの主要コンセプト:
- インデックス: ドキュメントをLLMで解析 → エンティティ・リレーション抽出 → コミュニティ構造化
- Local Search: 特定エンティティ・詳細な質問に最適(例:「Xの特性は?」)
- Global Search: データセット全体の傾向・高レベル質問に最適(例:「主なテーマは?」)
- DRIFT Search: Localの強化版。コミュニティ情報も組み合わせて広範な回答を生成
| 検索方式 | Embedding必要 | Copilot APIで利用可 |
|---|---|---|
| Global Search | ✗ | ✅ |
| Local Search | ✅ | ❌(プラン次第) |
| DRIFT Search | ✅ | ❌(プラン次第) |
| Basic Search | ✅ | ❌(プラン次第) |
Copilot APIを使う場合は Global Search のみ利用可能なことが多い。 Local/DRIFT/Basic を使いたい場合は Embedding を別プロバイダー(OpenAI API)に切り替えること(Step 1参照)。
⚠️ コスト注意: インデックス構築はLLM APIを大量消費。必ず小規模データで検証してから本番投入。
Step 1: LLMモデル設定
GitHub Copilot(推奨)
GitHub Copilot APIはOpenAI互換。APIキーは gh auth token で取得した値を使用。
- キー名は必ず default_completion_model にすること
# settings.yaml
completion_models:
default_completion_model:
model_provider: openai
model: gpt-5-mini
auth_method: api_key
api_key: ${GITHUB_COPILOT_API_KEY}
api_base: https://api.githubcopilot.com
retry:
type: exponential_backoff
max_retries: 5
base_delay: 2.0
embedding_models:
default_embedding_model:
model_provider: openai
model: text-embedding-3-small
auth_method: api_key
api_key: ${GITHUB_COPILOT_API_KEY}
api_base: https://api.githubcopilot.com
APIキーの取得と設定:
# gh CLIで現在のトークンを取得(推奨)
export GITHUB_COPILOT_API_KEY=$(gh auth token)
# .env(プロジェクトルートに配置)でも可
echo "GITHUB_COPILOT_API_KEY=$(gh auth token)" > .env
⚠️ Embeddingエンドポイントの可用性はCopilotプランにより異なる。使えない場合は別プロバイダーを検討。
カスタムモデルIDを使いたい場合(非対称設定)
カスタム名を使う場合は 全ワークフローに completion_model_id を明示指定すること:
completion_models:
indexing_model:
# ...
query_model:
# ...
# 明示しないワークフローは default_completion_model を探してエラーになる
extract_graph:
completion_model_id: indexing_model
global_search:
completion_model_id: query_model
local_search:
completion_model_id: query_model
drift_search:
completion_model_id: query_model
create_community_reports: # ← これも忘れずに
completion_model_id: indexing_model
OpenAI(Copilot Embeddingが使えない場合のフォールバック)
embedding_models:
default_embedding_model:
model_provider: openai
model: text-embedding-3-small
auth_method: api_key
api_key: ${OPENAI_API_KEY}
# api_base を省略するとデフォルトのOpenAIエンドポイントを使用
Step 2: ドキュメントの配置
GraphRAG v3 では 入力ディレクトリは input_storage.base_dir(トップレベルの独立セクション)で指定する。
- file_pattern はシングルクォートで囲むこと -コメント内を含め settings.yaml 全体でドル記号は禁止
input:
type: text
file_pattern: '.*\.(txt|md|log|json|yaml|toml|tsv)'
input_storage:
type: file
base_dir: "/absolute/path/to/docs" # 絶対パス指定可。相対パスは settings.yaml 基点で解決
uv run scripts/setup.py -d <ディレクトリ> で自動設定(推奨)。デフォルトの出力先は rag/graph-rag/。
対応形式: .txt, .md, .log, .json, .yaml, .toml, .tsv(デフォルト)
⚠️ PDF について: 事前に pdftotext でテキスト変換してから対象ディレクトリに配置すること。
# PDFを一括でテキストに変換(macOS: brew install poppler / Ubuntu: apt install poppler-utils)
for f in /path/to/docs/*.pdf; do
command -v pdftotext &>/dev/null && pdftotext -layout "$f" "${f%.pdf}.txt" &
done
wait
# 変換後のディレクトリを setup.py に渡す
uv run scripts/setup.py -d /path/to/docs
JSON/CSVで構造化データを扱う場合は input.type: csv に変更しカラムを指定:
input:
type: csv
text_column: "content"
title_column: "title"
id_column: "id"
Step 3: インデックス構築
# uv run経由(graphragをインストール済みでない場合)
uv run --with graphrag graphrag index --root rag/graph-rag
# graphragをインストール済みの場合
graphrag index --root rag/graph-rag
# Copilot APIのembedding preflight検証が失敗する場合は --skip-validation を追加
# (実際のインデックス処理には影響しない)
uv run --with graphrag graphrag index --root rag/graph-rag --skip-validation
完了の確認:
ls rag/graph-rag/output/
# artifacts/, logs/ などが存在すればOK
Step 4: クエリ実行
検索方式の選び方
| 質問の種類 | 推奨方式 | 例 |
|---|---|---|
| 全体傾向・テーマ・方針 | Global | 「主なセキュリティ方針は?」 |
| 特定エンティティ・固有名詞 | Local | 「CMEKとは何か?」「ツールXの使い方は?」 |
| 固有名詞を特定するQA(特定の名前・値) | Local(Embeddingが必要) | 「○○の正式名称は?」 |
注意: Global SearchはコミュニティレポートをLLMで集約する方式のため、特定の固有名詞が答えの場合は「I am sorry but I am unable to answer」と返すことがある。その場合は①クエリをより一般的な問いに言い換える、②答えがドキュメントに存在しない可能性を疑う、③Local Searchに切り替える(Embedding必要)。
CLIでの基本クエリ
# Global Search(データセット全体への高レベル質問 / Embedding不要)
uv run --with graphrag graphrag query --root rag/graph-rag "このドキュメント群の主要テーマは何ですか?"
# Local Search(特定エンティティへの詳細質問 / Embedding必要)
uv run --with graphrag graphrag query --root rag/graph-rag "Xとはどのようなものですか?" --method local
# DRIFT Search(LocalとGlobalを組み合わせた広範な検索 / Embedding必要)
uv run --with graphrag graphrag query --root rag/graph-rag "Xに関連する概念を教えてください" --method drift
# Basic Search(シンプルなベクトル検索 / Embedding必要)
uv run --with graphrag graphrag query --root rag/graph-rag "キーワード検索のテスト" --method basic
Python APIでのクエリ
詳細は scripts/query.py を参照。
Step 5: トラブルシューティング
| 問題 | 対処 |
|---|---|
Model ID default_completion_model not found | completion_models のキー名を default_completion_model に統一するか、全ワークフローに completion_model_id を明示指定 |
Invalid placeholder in string | settings.yaml のコメント内を含む 全ての $ が対象。コメントでも $ は禁止(ドル記号 と書く) |
file_pattern で YAML パースエラー | file_pattern は必ずシングルクォートで囲む。ダブルクォート内の \. は YAML invalid escape |
ファイルが見つからない(No matches found) | input.base_dir は v3 で無視される。input_storage.base_dir(トップレベルの独立セクション)に絶対パスまたは settings.yaml からの相対パスで指定すること |
generate_text_embeddings で pydantic エラー | Copilot API は embedding エンドポイント非対応のプランが多い。Local/DRIFT/Basic Search は使用不可となる。Global Search のみなら影響なし。 Local Search が必要な場合は OpenAI API キーで別途 embedding を設定すること(Step 1参照) |
| Global Search が「I am sorry but I am unable to answer」を返す | ①クエリが特定固有名詞の特定など詳細すぎる(Global Searchはテーマ・傾向質問向け)。より一般的な問いに言い換える。②答えがインデックス済みドキュメントに存在しない。③固有名詞の特定が必要な場合はLocal Searchを使う(Embedding必要) |
| Embedding preflight 検証エラー | --skip-validation を追加(実際の処理には影響しない) |
| インデックスが途中で止まる | レート制限の可能性。settings.yaml で retry.max_retries を設定 |
| クエリ結果が空 | output/ ディレクトリにparquetファイルが存在するか確認 |
| JSONエラーが出る | モデルがJSON構造化出力に対応しているか確認 |
| コストが高い | gpt-4o-mini 等の安価なモデルに変更、チャンクサイズを調整 |
ベストプラクティス
- まず小規模テスト: サンプルドキュメント数件でインデックス構築を試してから本番へ
- Prompt Tuning:
graphrag prompt-tuneでドメイン特化のプロンプト最適化を実施 - マイナーバージョンアップ時:
graphrag init --forceを実行して設定を更新 - メジャーバージョンアップ時: migration notebook を確認(再インデックスを避けるため)
参照リソース
- 公式ドキュメント・リンク集:
references/docs_links.md - ベストプラクティス詳細:
references/best_practices.md - クエリスクリプト例:
scripts/query.py→uv run scripts/query.py - セットアップスクリプト例:
scripts/setup.py→uv run scripts/setup.py