CrewAI は、連携して複雑な目標を達成する自律型 AI エージェントをオーケストレートするためのフレームワークです。ロール、目標、バックストーリーを指定してエージェントを定義し、タスクを定義できます。
この例では、Gemini 3 Flash を使用して、カスタマー サポート データを分析して問題を特定し、プロセス改善を提案するマルチエージェント システムを構築する方法を示します。このシステムは、最高執行責任者(COO)が読むことを想定したレポートを生成します。
このガイドでは、次のタスクを実行できる AI エージェントの「クルー」を作成する方法について説明します。
- カスタマー サポート データを取得して分析します(この例ではシミュレートされています)。
- 繰り返し発生する問題とプロセスのボトルネックを特定します。
- 具体的な改善案を提案します。
- 調査結果を COO に適した簡潔なレポートにまとめます。
Gemini API キーが必要です。キーがない場合は、Google AI Studio で取得できます。
pip install "crewai[tools]"Gemini API キーを GEMINI_API_KEY という名前の環境変数として設定し、Gemini モデルを使用するように CrewAI を構成します。
import os
from crewai import LLM
gemini_api_key = os.getenv("GEMINI_API_KEY")
gemini_llm = LLM(
model='gemini/gemini-3.5-flash',
api_key=gemini_api_key,
temperature=1.0 # Use the Gemini 3 recommended temperature
)
コンポーネントを定義する
ツール、エージェント、タスク、クルー自体を使用して CrewAI アプリケーションを構築します。以降のセクションでは、これらの各コンポーネントについて説明します。
ツール
ツールは、エージェントが外部とやり取りしたり、特定のアクションを実行したりするために使用できる機能です。ここでは、カスタマー サポート データの取得をシミュレートするプレースホルダ ツールを定義します。実際のアプリケーションでは、データベース、API、ファイル システムに接続します。ツールの詳細については、CrewAI ツールガイドをご覧ください。
from crewai.tools import BaseTool
# Placeholder tool for fetching customer support data
class CustomerSupportDataTool(BaseTool):
name: str = "Customer Support Data Fetcher"
description: str = (
"Fetches recent customer support interactions, tickets, and feedback. "
"Returns a summary string.")
def _run(self, argument: str) -> str:
# In a real scenario, this would query a database or API.
# For this example, return simulated data.
print(f"--- Fetching data for query: {argument} ---")
return (
"""Recent Support Data Summary:
- 50 tickets related to 'login issues'. High resolution time (avg 48h).
- 30 tickets about 'billing discrepancies'. Mostly resolved within 12h.
- 20 tickets on 'feature requests'. Often closed without resolution.
- Frequent feedback mentions 'confusing user interface' for password reset.
- High volume of calls related to 'account verification process'.
- Sentiment analysis shows growing frustration with 'login issues' resolution time.
- Support agent notes indicate difficulty reproducing 'login issues'."""
)
support_data_tool = CustomerSupportDataTool()
エージェント
エージェントは、クルー内の個々の AI ワーカーです。各エージェントには、特定の role、goal、backstory、割り当てられた llm、省略可能な tools があります。エージェントの詳細については、CrewAI エージェントのガイドをご覧ください。
from crewai import Agent
# Agent 1: Data analyst
data_analyst = Agent(
role='Customer Support Data Analyst',
goal='Analyze customer support data to identify trends, recurring issues, and key pain points.',
backstory=(
"""You are an expert data analyst specializing in customer support operations.
Your strength lies in identifying patterns and quantifying problems from raw support data."""
),
verbose=True,
allow_delegation=False, # This agent focuses on its specific task
tools=[support_data_tool], # Assign the data fetching tool
llm=gemini_llm # Use the configured Gemini LLM
)
# Agent 2: Process optimizer
process_optimizer = Agent(
role='Process Optimization Specialist',
goal='Identify bottlenecks and inefficiencies in current support processes based on the data analysis. Propose actionable improvements.',
backstory=(
"""You are a specialist in optimizing business processes, particularly in customer support.
You excel at pinpointing root causes of delays and inefficiencies and suggesting concrete solutions."""
),
verbose=True,
allow_delegation=False,
# No tools needed, this agent relies on the context provided by data_analyst.
llm=gemini_llm
)
# Agent 3: Report writer
report_writer = Agent(
role='Executive Report Writer',
goal='Compile the analysis and improvement suggestions into a concise, clear, and actionable report for the COO.',
backstory=