1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

CrewAIで超簡単な翻訳エージェントを作ってみた

1
Posted at

0. CrewAIとは?

複数のAIエージェントを「チーム(Crew)」として構成し、
それぞれが役割を持って連携しながらタスクを遂行するためのフレームワークです。

従来の「ひとつのAIがすべてをこなす」構成とは異なり、
CrewAIでは「複数の専門エージェント」が協力して目標を達成します。

この記事では、CrewAIの基本構造を理解しながら、
実際のコードを通して Tool中心のAIロジック を解説します。

1. Tool中心で動くAIエージェントのロジック構造

AIエージェントの基本的なロジックは、Tool(ツール)中心の設計で構成されています。
エージェント自体が直接すべての処理を実行するわけではなく、
外部のツールを呼び出してタスクを遂行するという考え方です。

たとえば「英語の文章を日本語に翻訳する」という指示が与えられた場合、
エージェントは以下のような流れで動作します。

  1. 目的(Goal)を理解する
  2. 適切なToolを選択する
  3. Toolを使って結果を取得する
  4. 得られた結果をもとに最終出力を生成する

つまり、Toolはエージェントの「手足」であり、
エージェント自体は「何を、なぜ行うか」を考える脳のような存在
です。

2. CrewAIによるエージェント構成の例

以下のサンプルコードは、CrewAIを使って
「英語の文章を韓国語に翻訳するエージェント」を定義・実行する例です。

main.py
import dotenv

dotenv.load_dotenv()

from crewai import Crew, Agent, Task
from crewai.project import CrewBase, agent, task, crew

@CrewBase
class TranslatorCrew:

    @agent
    def translator_agent(self):
        return Agent(
            config=self.agents_config["translator_agent"],
        )

    @task
    def translate_task(self):
        return Task(
            config=self.tasks_config["translate_task"],
        )

    @crew
    def assemble_crew(self):
        return Crew(
            agents=self.agents,
            tasks=self.tasks,
            verbose=True,
        )

TranslatorCrew().assemble_crew().kickoff(
    inputs={
        "sentence": "I'm Jung and I like to ride my bicicle in Napoli",
    }
)
config/tasks.yaml
translate_task:
  description: >
    Translate {sentence} from English to Korean without making mistakes.
  expected_output: >
    A well formatted translation from English to Korean using proper capitalization of names and places.
  agent: translator_agent
config/agents.yaml
translator_agent:
  role: >
    Translator to translate from English to Korean
  goal: >
    To be a good and useful translator to avoid misunderstandings.
  backstory: >
    You grew up between New York and Seoul, you can speak two languages fluently, and you can detect the cultural differences.

3. agents.yaml の属性解説

CrewAIでは、agents.yaml にエージェントの「性格」や「目的」を定義します。
これにより、同じAIでも出力のトーンや思考方針が変わります。

属性名 説明 参考URL
role エージェントの役割を定義します。
例:「Translator」「Planner」「Reviewer」など
CrewAI Docs - Agents
goal エージェントが達成すべき目標。出力の方向性や思考方針を決定します。 CrewAI Docs - Goals
backstory エージェントの人格やバックグラウンド設定。文脈理解やトーンに影響します。 CrewAI Docs - Backstory

4. tasks.yaml の属性解説

CrewAIでの tasks.yaml は、エージェントが実行する 具体的な作業(Task) を定義します。
ここで定義することで、エージェントは目的に沿った行動を取り、出力の精度や一貫性を高めることができます。

属性名 説明 参考URL
description Taskが何を実行すべきか自然言語で記述。エージェントの行動指針となる CrewAI Docs - Tasks
expected_output Task終了後に期待される出力の形式や品質を明示。翻訳や文章生成の品質基準として利用 CrewAI Docs - Tasks
agent Taskを実行する担当エージェントを指定。複数エージェントでチーム作業も可能 CrewAI Docs - Tasks

5. 実行結果

╭────────────────────────────────────────────────────────────────────────── Crew Execution Started ───────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                                                             │
│  Crew Execution Started                                                                                                                                                     │
│  Name: crew                                                                                                                                                                 │
│  ID: 9e9c3472-7e64-43ed-8ff9-2e0e603a8d21                                                                                                                                   │
│  Tool Args:                                                                                                                                                                 │
│                                                                                                                                                                             │
│                                                                                                                                                                             │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

🚀 Crew: crew
└── 📋 Task: translate_task (ID: d5f3f7df-8047-4687-aa70-e8a950e1febb)
    Status: Executing Task...
╭───────────────────────────────────────────────────────────────────────────── 🤖 Agent Started ──────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                                                             │
│  Agent: Translator to translate from English to Korean                                                                                                                      │
│                                                                                                                                                                             │
│  Task: Translate I'm Jung and I like to ride my bicicle in Napoli from English to Korean without making mistakes.                                                           │
│                                                                                                                                                                             │
│                                                                                                                                                                             │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
🚀 Crew: crew
└── 📋 Task: translate_task (ID: d5f3f7df-8047-4687-aa70-e8a950e1febb)
    Status: Executing Task...
    └── 🧠 Thinking...
╭─────────────────────────────────────────────────────────────────────────── ✅ Agent Final Answer ───────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                                                             │
│  Agent: Translator to translate from English to Korean                                                                                                                      │
│                                                                                                                                                                             │
│  Final Answer:                                                                                                                                                              │
│  저는 정이고, 나폴리에서 자전거를 타는 것을 좋아합니다.                                                                                                                     │
│                                                                                                                                                                             │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
🚀 Crew: crew
└── 📋 Task: translate_task (ID: d5f3f7df-8047-4687-aa70-e8a950e1febb)
    Assigned to: Translator to translate from English to Korean
    
    Status: ✅ Completed
╭────────────────────────────────────────────────────────────────────────────── Task Completion ──────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                                                             │
│  Task Completed                                                                                                                                                             │
│  Name: translate_task                                                                                                                                                       │
│  Agent: Translator to translate from English to Korean                                                                                                                      │
│                                                                                                                                                                             │
│  Tool Args:                                                                                                                                                                 │
│                                                                                                                                                                             │
│                                                                                                                                                                             │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

╭────────────────────────────────────────────────────────────────────────────── Crew Completion ──────────────────────────────────────────────────────────────────────────────╮
│                                                                                                                                                                             │
│  Crew Execution Completed                                                                                                                                                   │
│  Name: crew                                                                                                                                                                 │
│  ID: 9e9c3472-7e64-43ed-8ff9-2e0e603a8d21                                                                                                                                   │
│  Tool Args:                                                                                                                                                                 │
│  Final Output: 저는 정이고, 나폴리에서 자전거를 타는 것을 좋아합니다.                                                                                                       │
│                                                                                                                                                                             │
│                                                                                                                                                                             │
╰─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
  1. Agent がタスクを受け取り、translate_task を実行。
  2. エージェントは agents.yaml の設定に基づき、英語文を自然に韓国語に翻訳。
  3. YAMLで定義された role、goal、backstory と description、expected_output に従って出力される。
  4. CrewAI はタスク完了後、信頼性の高い回答を生成。

7. CrewAIの構造イメージ

CrewAIは、「Crew(チーム)」「Agent(個々のエージェント)」「Task(タスク)」という3つの層で構成されています。

Crew(チーム全体)
│
├── Agent(役割ごとのエージェント)
│     └── Tool(外部操作手段)
│
└── Task(具体的な指令)

8. 城の比喩:CrewAIが得意な領域と限界

「CrewAIは“城の中”で定義された動作を制御するのには適しているが、
“城の外”にある窓を修理したり、新しい門を作るようなことは苦手だ。」

これはつまり、CrewAIは
明確に定義された領域(=CrewやAgentが扱うタスクの範囲)では極めて強力ですが、
その外側にある未知の構造(=外部アーキテクチャなど)には踏み込まないという意味です。

CrewAIは「内部でのチームワーク」を自動化するのに最適化されており、
新しいAPIを自ら定義したり、アーキテクチャそのものを拡張するような処理は想定されていません。

9. まとめ

  • CrewAIでは、YAMLを使ってエージェントやタスクの設定を明示的に定義することで、AIの回答精度や一貫性を高めることができます。
  • agents.yaml ではエージェントの role、goal、backstory を設定し、出力のトーンや文脈理解を制御。
  • tasks.yaml ではタスクの description、expected_output、agent を設定し、具体的な作業内容と成果物の品質を明示。
  • この構造により、CrewAIは 定義されたルールの範囲内で信頼性の高い回答 を生成できます。
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?