AI業務活用 2026.04.30
【2026年版】RAG vs ファインチューニング vs プロンプト工夫|選び方と使い分け
3手法の基本比較
| 項目 | プロンプト工夫 | RAG | ファインチューニング |
|---|---|---|---|
| 導入コスト | 無料 | 中(DB構築) | 高(学習費用) |
| 精度改善 | 5〜20% | 20〜50% | 30〜70% |
| 最新情報対応 | 不可 | ○(DB更新のみ) | △(再学習必要) |
| 導入期間 | 即日 | 1〜2週間 | 1〜2ヶ月 |
| 推奨場面 | まず試す | 社内データQ&A | 専門語調・形式習得 |
選択フローチャート
社内ドキュメントに基づいた回答が必要?
YES → RAGを選択
NO ↓
特定の語調・文体・専門用語の習得が必要?
YES → ファインチューニングを検討
NO ↓
まず安く試したい?
YES → プロンプト工夫(few-shot含む)から開始
プロンプト工夫の実装(最も安価)
# few_shot_prompting.py
# few-shot examplesで出力フォーマットをコントロールする
import anthropic
client = anthropic.Anthropic()
FEW_SHOT_SYSTEM = """あなたはカスタマーサポート担当者です。
回答フォーマット例:
---
ユーザー質問: パスワードを忘れました
回答:
申し訳ございません。パスワードのリセット方法をご案内します。
1. ログインページの「パスワードを忘れた方」をクリック
2. 登録メールアドレスを入力
3. 受信メールのリンクから新しいパスワードを設定
ご不明点があればお気軽にお問い合わせください。
---"""
def customer_support(question: str) -> str:
response = client.messages.create(
model="claude-haiku-4-5-20251001", # カスタマーサポートはHaikuで十分
max_tokens=512,
system=FEW_SHOT_SYSTEM,
messages=[{"role": "user", "content": f"ユーザー質問: {question}"}]
)
cost = (response.usage.input_tokens * 0.80 + response.usage.output_tokens * 4.00) / 1_000_000
print(f"コスト: ${cost:.5f}")
return response.content[0].text
result = customer_support("注文したのにまだ届きません")
print(result)
RAGの実装(社内データQ&A向け)
# simple_rag.py
# ChromaDB + Claude APIでシンプルなRAGを構築する
# pip install chromadb anthropic
import anthropic
import chromadb
client = anthropic.Anthropic()
chroma = chromadb.Client()
collection = chroma.create_collection("knowledge_base")
def add_knowledge(texts: list[str], sources: list[str]) -> None:
"""知識ベースにドキュメントを追加する"""
collection.add(
documents=texts,
metadatas=[{"source": s} for s in sources],
ids=[f"doc_{i}" for i in range(len(texts))]
)
def rag_query(question: str) -> dict:
"""RAGで質問に回答する"""
# Step 1: 関連ドキュメントを検索
results = collection.query(query_texts=[question], n_results=3)
context = "\n\n".join(results["documents"][0])
sources = [m["source"] for m in results["metadatas"][0]]
# Step 2: コンテキストを使って回答
response = client.messages.create(
model="claude-haiku-4-5-20251001",
max_tokens=512,
system=f"""以下のコンテキストを使って質問に答えてください。
コンテキストに情報がない場合は「情報が見つかりません」と答えてください。
コンテキスト:
{context}""",
messages=[{"role": "user", "content": question}]
)
cost = (response.usage.input_tokens * 0.80 + response.usage.output_tokens * 4.00) / 1_000_000
return {
"answer": response.content[0].text,
"sources": sources,
"cost_usd": round(cost, 5)
}
# 使用例
add_knowledge(
texts=["返品ポリシー: 購入から30日以内は理由不問で返品可能", "配送時間: 通常2〜3営業日"],
sources=["faq.md", "shipping.md"]
)
result = rag_query("返品できますか?")
print(f"回答: {result['answer']}")
print(f"出典: {result['sources']}")
print(f"コスト: ${result['cost_usd']:.5f}")