CrewAI のガイドに移り、「最初の Crew の構築」を翻訳しました。複雑な問題を解決するために協力する協調型 AI チームを作成する、ステップ by ステップなチュートリアルです。
CrewAI : ガイド : 最初の Crew の構築
作成 : クラスキャット・セールスインフォメーション
作成日時 : 03/29/2025
* 本記事は github.com/crewAIInc/crewAI/docs の以下のページを独自に翻訳した上でまとめ直し、補足説明を加えています :
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
クラスキャット 人工知能 研究開発支援サービス ⭐️ リニューアルしました 😉
◆ お問合せ : 下記までお願いします。
- クラスキャット セールス・インフォメーション
- sales-info@classcat.com
- ClassCatJP
// –>
CrewAI : ガイド : 最初の Crew の構築
複雑な問題を解決するために協力する協調型 AI チームを作成する、ステップ by ステップなチュートリアルです。協調型 AI のパワーを解き放つ
複雑な問題を解決するためにシームレスに協力する専門 AI エージェントのチームを持ち、それぞれが共通のゴールを達成するためにユニークなスキルを発揮することを想像してください。これが CrewAI のパワーです - 単一の AI が単独で達成できるものを遥かに超えてタスクを達成可能な協調型 AI システムの作成を可能にするフレームワークです。 このガイドでは、トピックの調査と分析をしてから、包括的なレポートを作成する支援をする、調査 crew の作成を順を追って説明します。この実践的なサンプルは、AI エージェントが複雑なタスクを達成するためにどのように協力できるかを実演しますが、それは CrewAI で何が実現可能かの始まりに過ぎません。何を構築して学習するか
このガイドの最後までに、以下のことが成されます :- 明確なロールと責任を持つ 専門の AI 調査チームの作成
- 複数の AI エージェント間の コラボのオーケストレーション
- 情報の収集、分析とレポート生成等の 複雑なワークフローの自動化
- より野心的なプロジェクトに適用できる 基礎スキルの構築
- 専門のライター、編集者とファクトチェッカーを持つ他段階のコンテンツ作成
- 階層化されたサポートエージェントを持つ複雑なカスタマーサービス・システム
- データを収集し、視覚化を作成し、そして洞察を生成する自律的なビジネスアナリスト
- アイデアを出し、設計し、そして実装を計画する製品開発チーム
前提条件
始める前に、以下を確認してください :- インストールガイドに従って CrewAI をインストール
- 環境変数に OpenAI API キーのセットアップ
- Python の基本的な理解
Step 1: 新しい CrewAI プロジェクトの作成
最初に、CLI を使用して新しい CrewAI プロジェクトを作成しましょう。このコマンドは必要なすべてのファイルを含む完全なプロジェクト構造をセットアップし、ボイラープレート・コードをセットアップするのではなく、エージェントやそれらのタスクを定義することに集中することを可能にします。crewai create crew research_crew
cd research_crew
これは crew に必要な基本構造を持つプロジェクトを生成します。CLI は自動的に以下を作成します :
- 必要なファイルを含むプロジェクト・ディレクトリ
- エージェントとタスク用の設定ファイル
- 基本的な crew 実装
- crew を実行するための主スクリプト

Step 2 : プロジェクト構造の探究
CLI により作成されたプロジェクト構造を理解するための時間を少し取りましょう。CrewAI は Python プロジェクトのベストプラクティスに従っていて、crew がより複雑になっても簡単にコードの保守・拡張ができます。research_crew/
├── .gitignore
├── pyproject.toml
├── README.md
├── .env
└── src/
└── research_crew/
├── __init__.py
├── main.py
├── crew.py
├── tools/
│ ├── custom_tool.py
│ └── __init__.py
└── config/
├── agents.yaml
└── tasks.yaml
この構造は Python プロジェクトのベストプラクティスに従っており、コードを整理することを簡単にします。(Python の) 実装コードからの (YAML での) 設定ファイルの分離は、基礎コードを変更することなく crew の動作を変更することを容易にします。
Step 3: エージェントの設定
さて楽しいところに入ります - AI エージェントの定義です!CrewAI では、エージェントは特定のロール、目標とそれらの動作を形作る背景を持つ特殊な実在 (entity) です。それらを演劇内のキャラクターとして考えてください、各々はパーソナリティと目的を持っています。 調査 crew 用に、2 つのエージェントを作成します :- 情報の検索と整理に優れた調査員 (researcher)
- 調査結果を解釈し、洞察に優れたレポートを作成できるアナリスト (analyst)
# src/research_crew/config/agents.yaml
researcher:
role: >
Senior Research Specialist for {topic}
goal: >
Find comprehensive and accurate information about {topic}
with a focus on recent developments and key insights
backstory: >
You are an experienced research specialist with a talent for
finding relevant information from various sources. You excel at
organizing information in a clear and structured manner, making
complex topics accessible to others.
llm: openai/gpt-4o-mini
analyst:
role: >
Data Analyst and Report Writer for {topic}
goal: >
Analyze research findings and create a comprehensive, well-structured
report that presents insights in a clear and engaging way
backstory: >
You are a skilled analyst with a background in data interpretation
and technical writing. You have a talent for identifying patterns
and extracting meaningful insights from research data, then
communicating those insights effectively through well-crafted reports.
llm: openai/gpt-4o-mini
各エージェントが明確なロール、目標と背景をどのように持つか注目してください。これらの要素は単なる説明ではありません - これらはエージェントがタスクにアプローチする方法を積極的に形作ります。これらを注意深く作成することで、互いに補完しあう専門スキルと視点を持つエージェントを作成できます。
Step 4: タスクの定義
エージェントを定義したら、次に遂行する特定のタスクをそれらに与える必要があります。CrewAI のタスクは、詳細な指示と期待される出力を含む、エージェントが遂行する具体的なワークを表します。 research crew のためには、2 つの主要なタスクを定義します :- 包括的な情報を収集する 調査タスク
- 洞察に富むレポートを作成する 分析タスク
# src/research_crew/config/tasks.yaml
research_task:
description: >
Conduct thorough research on {topic}. Focus on:
1. Key concepts and definitions
2. Historical development and recent trends
3. Major challenges and opportunities
4. Notable applications or case studies
5. Future outlook and potential developments
Make sure to organize your findings in a structured format with clear sections.
expected_output: >
A comprehensive research document with well-organized sections covering
all the requested aspects of {topic}. Include specific facts, figures,
and examples where relevant.
agent: researcher
analysis_task:
description: >
Analyze the research findings and create a comprehensive report on {topic}.
Your report should:
1. Begin with an executive summary
2. Include all key information from the research
3. Provide insightful analysis of trends and patterns
4. Offer recommendations or future considerations
5. Be formatted in a professional, easy-to-read style with clear headings
expected_output: >
A polished, professional report on {topic} that presents the research
findings with added analysis and insights. The report should be well-structured
with an executive summary, main sections, and conclusion.
agent: analyst
context:
- research_task
output_file: output/report.md
analysis タスクの context フィールドに注意してください - これは強力な機能で、analyst が research タスクの出力にアクセスすることを可能にします。これは、人間のチーム内でそうであるように、エージェント間の情報が自然に流れるワークフローを作成します。
Step 5: Crew の設定
crew を設定して、一つにまとめるときです。crew は、エージェントがタスクを完了するために協力する方法を編成する (orchestrate) コンテナです。 crew.py ファイルを変更しましょう :# src/research_crew/crew.py
from crewai import Agent, Crew, Process, Task
from crewai.project import CrewBase, agent, crew, task
from crewai_tools import SerperDevTool
@CrewBase
class ResearchCrew():
"""Research crew for comprehensive topic analysis and reporting"""
@agent
def researcher(self) -> Agent:
return Agent(
config=self.agents_config['researcher'],
verbose=True,
tools=[SerperDevTool()]
)
@agent
def analyst(self) -> Agent:
return Agent(
config=self.agents_config['analyst'],
verbose=True
)
@task
def research_task(self) -> Task:
return Task(
config=self.tasks_config['research_task']
)
@task
def analysis_task(self) -> Task:
return Task(
config=self.tasks_config['analysis_task'],
output_file='output/report.md'
)
@crew
def crew(self) -> Crew:
"""Creates the research crew"""
return Crew(
agents=self.agents,
tasks=self.tasks,
process=Process.sequential,
verbose=True,
)
このコード内で、以下を行います :
- researcher エージェントを作成して、Web を検索するために SerperDevTool を装備します。
- analyst エージェントを作成します。
- research と analysis タスクのセットアップ。
- crew がタスクを順番に実行するように設定します (analyst は researcher が終了するまで待機します)
Step 6: メイン・スクリプトのセットアップ
次に、crew を実行するメイン・スクリプトをセットアップしましょう。これは、crew に調査して欲しい特定のトピックを提供するところです。#!/usr/bin/env python
# src/research_crew/main.py
import os
from research_crew.crew import ResearchCrew
# Create output directory if it doesn't exist
os.makedirs('output', exist_ok=True)
def run():
"""
Run the research crew.
"""
inputs = {
'topic': 'Artificial Intelligence in Healthcare'
}
# Create and run the crew
result = ResearchCrew().crew().kickoff(inputs=inputs)
# Print the result
print("\n\n=== FINAL REPORT ===\n\n")
print(result.raw)
print("\n\nReport has been saved to output/report.md")
if __name__ == "__main__":
run()
このスクリプトは環境を準備し、調査トピックを指定し、そして crew の作業をキックオフします。CrewAI のパワーはこのコードがどれだけ単純化である点で明らかです - 複数の AI エージェントを管理するすべての複雑さはフレームワークにより処理されます。
Step 7: 環境変数のセットアップ
プロジェクト・ルートに API キーを含む .env ファイルを作成します :OPENAI_API_KEY=your_openai_api_key
SERPER_API_KEY=your_serper_api_key
You can get a Serper API key from Serper.dev.
Step 8: 依存関係のインストール
CrewAI CLI を使用して必要な依存関係をインストールします :crewai install
このコマンドは :
- プロジェクト configuration から依存関係を読み取ります。
- 必要であれば仮想環境を作成します。
- すべての必要なパッケージをインストールします。
Step 9: Crew の実行
Now for the exciting moment - 実際に crew を実行して AI コラボを見るときです!crewai run
このコマンドを実行すれば、crew が活発に動き始めるのを見るでしょう。researcher は特定のトピックについての情報を収集し、それから analyst はその調査に基づいて包括的なレポートを作成します。エージェントが協力してタスクを完了する間、エージェントの思考プロセス、アクション、出力をリアルタイムで確認できます。
Step 10: 出力のレビュー
crew がその作業を完了すれば、output/report.md ファイルで最終的なレポートを見るでしょう。レポートは以下を含みます :- エグゼクティブ・サマリー (executive summary)
- トピックについての詳細情報
- 分析と洞察
- 推奨事項 or 今後の検討事項
他の CLI コマンドの探究
CrewAI は crew を操作するための他の幾つかの有用な CLI コマンドを提供しています :# View all available commands
crewai --help
# Run the crew
crewai run
# Test the crew
crewai test
# Reset crew memories
crewai reset-memories
# Replay from a specific task
crewai replay -t
以上