見出し画像

【次に来る技術!】crewAIとは?使い方・基本を解説!自動でタスクを完結する自律型AIエージェント!

かなり長い動画ですが、YOUTUBEでCrewAIとは何か?使い方などを解説しています。

noteではcrewAIの基礎を解説していきます。

海外で話題のcrewAI。タスクを自律的に完了させるためのAIエージェントを構築・運用するフレームワークです。

この技術は、AIエージェント同士が協力して作業を行うことを可能にし、自動的にタスクを完了させる。

MicrosoftのAugotenと同様の自律型AIエージェントフレームワークの一つ。ビルゲイツも次に来る技術は自動的にタスクを完了させる AI エージェントの仕組み だろうという風に 予測しています。

自律型 AI エージェントは積極的に利用されておらず、まだまだ未熟で黎明期の技術です。

だからこそ早めに参入しておくことでメリットがあると言えます。

crewAIをはじめとするAIエージェントとは?

crewAI以外の代表的なAIエージェントとは?

Autogen

Autogen・マイクロソフトが開発したAIエージェントのフレームワーク。Microsoft、Penn State University、Washington大学の共同研究によって生まれたフレームワークです。

microsoft -Autogen↓

ChatDev

ChatDevは、AIエージェントが協力して動作する仮想のソフトウェア開発会社として機能するツール。

つまり、自動でソフトウエア開発をしてしまうというAIエージェント。CEO、CTO、プログラマー、レビュアー、テスター、デザイナーなどの役割を持ち、それぞれが協力してタスク実行。

ユーザーのテキスト指示に基づいて、設計、コーディング、テスト、ドキュメント作成などのソフトウェア開発プロセスを実行します。

ChatDev 公式Gitjub↓

crewAIを動かす仕組み

画像

例えば、AIのトレンドの記事を書くと言うタスクを実行するとします。

その場合、上の図のように、3つのAgentを設定します。

Agent 1=Research Analyst
Agent2 =AiExpert
Agent3 =Writer

そのAgentがそれぞれのTaskを実行します。

Agent 1=Research Analyst
Task1=(ネット上の情報を収集し、情報を分析する)


Agent2 =AiExpert(AIの専門家)
Task2=(AI専門家として情報を精査する)

Agent3 =Writer
Task3=(情報を受けて記事を作成する)

たらに、Taskを行う上での”機能”も追加できます。この機能イコールToolsです。Agentが行うTaskになんらかの機能を持たせるのが、Tools。

機能=Toolsは、LangChainのライブラリとして簡単に実装できるところも、crewAIの特徴です。

LangChainTools

画像

以下、LangChainToolsの一覧。この機能を、Agentに追加できるということです。

  • Alpha Vantage: 金融市場データへのアクセスを提供するAPIサービスです。

  • Apify: ウェブスクレイピングと自動化のためのプラットフォームです。

  • ArXiv: 学術論文リポジトリからの情報取得を可能にするツールです。

  • AWS Lambda: サーバーレスコンピューティングサービスで、コードの管理なしに実行を可能にします。

  • Shell (bash): システムレベルのタスク実行能力を提供しますが、セキュリティリスクに注意が必要です。

  • Bearly Code Interpreter: 様々な言語で書かれたコードをリモートで実行するツールです。

  • Bing Search: ウェブ検索のためのAPIサービスを提供します。

  • Brave Search: プライバシーを保ちながらウェブ情報を検索できるツールです。

  • ChatGPT Plugins: ChatGPTの対話能力を拡張するためのプラグイン組み込みを可能にします。

  • Connery Action Tool: 特定のアクションやタスクの自動化を支援するツールです。

  • Dall-E Image Generator: テキストから画像を生成するモデルです。

  • DataForSEO: SEOに関するデータとサービスを提供します。

  • DuckDuckGo Search: プライバシーを重視したウェブ検索を可能にするツールです。

  • E2B Data Analysis: データ分析やアルゴリズムテストに適したランタイムサンドボックスを提供します。

  • Eden AI: 複数のAIサービスへのアクセスを一元管理するプラットフォームです。

  • Eleven Labs Text2Speech: テキストから音声を生成するAPIサービスです。

  • Exa Search: 自然言語のクエリに基づく複雑な検索を可能にする検索エンジンです。

  • File System: ファイルシステムへのアクセスや操作を可能にするツールです。

  • Gradio: ウェブアプリを簡単に作成できるライブラリです。

  • GraphQL: 効率的なデータ取得のためのAPIクエリ言語です。

  • HuggingFace Hub Tools: 機械学習モデルやデータセットへのアクセスを容易にするツール群です。

  • Human as a tool: 人間をAIタスクの補助として利用する考え方を示します。

  • IFTTT WebHooks: カスタマイズされた自動化を実現するためのサービスです。

  • Ionic Shopping Tool: モバイルアプリ開発フレームワークを指す可能性があります。

  • Lemon Agent: 特定のタスクやサービスを支援するエージェントを指す可能性があります。

  • Memorize: 情報を「記憶」することで応答の精度を向上させる技術です。

  • Nuclia Understanding: 非構造化データをインデックス化し、検索可能にする技術です。

  • OpenWeatherMap: 天気情報を取得するためのコンポーネントです。

  • Polygon Stock Market API: 株式市場データへのアクセスを提供するAPIです。

  • PubMed: 生物医学文献を網羅するデータベースです。

  • Python REPL: コードの断片を試したり、簡単なプログラムを実行するのに便利なツールです。

  • Reddit Search: Redditのコンテンツを検索するためのツールです。

  • Requests: ウェブ上の情報にアクセスするためのPythonのHTTPライブラリです。

  • SceneXplain: 画像を説明するキャプションを生成するサービスです。

  • Search Tools: さまざまな検索ツールの使用方法を紹介するノートブックです。

  • SearchApi: ウェブ全体を検索するためのAPIを紹介するノートブックです。

  • SearxNG Search: プライバシーを重視したメタ検索エンジンです。

  • Semantic Scholar API Tool: 学術論文検索エンジンSemantic ScholarのAPIを利用するツールです。

  • SerpAPI: Google検索結果をスクレイピングして取得するAPIです。

  • StackExchange: Stack Exchangeネットワークからのデータを検索し取得するコンポーネントです。

  • Tavily Search: 特定のデータやコンテンツを効率的に検索する能力を持つ検索エンジンAPIです。

  • Twilio: 通信をアプリケーションに統合するクラウドプラットフォームです。

  • Wikidata: 構造化されたデータを提供する知識ベースです。

  • Wikipedia: 多言語のオンライン百科事典から情報を取得するツールです。

  • Wolfram Alpha: 計算知識エンジンからの情報取得を可能にするコンポーネントです。

  • Yahoo Finance News: 最新の金融ニュースと市場情報を提供するツールです。

  • YouTube Search: YouTube上の動画を検索するパッケージです。

crewAIを動かす流れ

その前に..crewAIのGPTsがあります。crewAIについての知識を持っていて、crewAIの使い方を聞いたり、コード生成もしてくれます。

CrewAI GPTs

STEP1 仮想環境の構築

今回は、VisualStudioCodeで動かしてみます。
VisualStudioCodeのターミナルを立ち上げます。

画像

デスクトップ上にPythonの仮想環境を作成する方法

Pythonの開発を行う際、プロジェクトごとに異なるライブラリのバージョンを管理するために仮想環境を使うことが一般的です。ここでは、デスクトップ上にdev_envという名前の仮想環境を作成する手順を紹介します。

1. デスクトップに移動

まずは、ターミナルやコマンドプロンプトを開いて、デスクトップディレクトリに移動しましょう。

  • Windowsの場合:

cd %USERPROFILE%\Desktop
  • macOSまたはLinuxの場合:

cd ~/Desktop

2. 仮想環境の作成

次に、dev_envという名前で仮想環境を作成します。Python 3.3以降では、venvモジュールを使って仮想環境を簡単に作成できます。
システムに複数のPythonバージョンがインストールされている場合、またはpythonコマンドがPython 2を指している場合は、python3を使ってください。

python -m venv dev_env

3. 仮想環境のアクティベーション

最後に、作成した仮想環境をアクティベートします。これにより、この環境内でPythonやpipを使用する際に、他のプロジェクトとの依存関係が衝突することなく、安全に作業を行うことができます。

Windowsの場合:

.\dev_env\Scripts\activate

macOSまたはLinuxの場合:

source dev_env/bin/activate

STEP2 Agentの設定

API_KEYのところには必ずOpenAIのAPIKEYを入れてください。

以下は、crewAI公式Githubのサンプルコードです。

researcher、writer がAgentとして設定されています。

  • researcher

  • writer 

さらに以下の項目を自然言語でAgentに指定します。

  • 役割(role): クルー内でのエージェントの役割を定義

  • 目標(Goal): エージェントが達成することを目指す個々の目標

  • バックストーリー(Bakstory): Agentが持つバックグラウンド

  • TOOL: エージェントがタスクを実行するために使用できる機能

  • Verbose : クルーの実行中のログを確認できる。

以下のように設定します。

writer = Agent(
  role='Tech Content Strategist',
  goal='Craft compelling content on tech advancements',
  backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles.
  You transform complex concepts into compelling narratives.""",
  verbose=True,
  allow_delegation=True,

)
import os
from crewai import Agent, Task, Crew, Process

os.environ["OPENAI_API_KEY"] = "API_KEY"

# You can choose to use a local model through Ollama for example. See ./docs/how-to/llm-connections.md for more information.
# from langchain.llms import Ollama
# ollama_llm = Ollama(model="openhermes")

# Install duckduckgo-search for this example:
# !pip install -U duckduckgo-search

from langchain.tools import DuckDuckGoSearchRun
search_tool = DuckDuckGoSearchRun()

# Define your agents with roles and goals
researcher = Agent(
  role='Senior Research Analyst',
  goal='Uncover cutting-edge developments in AI and data science',
  backstory="""You work at a leading tech think tank.
  Your expertise lies in identifying emerging trends.
  You have a knack for dissecting complex data and presenting actionable insights.""",
  verbose=True,
  allow_delegation=False,
  tools=[search_tool]
  # You can pass an optional llm attribute specifying what mode you wanna use.
  # It can be a local model through Ollama / LM Studio or a remote
  # model like OpenAI, Mistral, Antrophic or others (https://python.langchain.com/docs/integrations/llms/)
  #
  # Examples:
  # llm=ollama_llm # was defined above in the file
  # llm=OpenAI(model_name="gpt-3.5", temperature=0.7)
  # For the OpenAI model you would need to import
  # from langchain_openai import OpenAI
)
writer = Agent(
  role='Tech Content Strategist',
  goal='Craft compelling content on tech advancements',
  backstory="""You are a renowned Content Strategist, known for your insightful and engaging articles.
  You transform complex concepts into compelling narratives.""",
  verbose=True,
  allow_delegation=True,
  # (optional) llm=ollama_llm
)

# Create tasks for your agents
task1 = Task(
  description="""Conduct a comprehensive analysis of the latest advancements in AI in 2024.
  Identify key trends, breakthrough technologies, and potential industry impacts.
  Your final answer MUST be a full analysis report""",
  agent=researcher
)

task2 = Task(
  description="""Using the insights provided, develop an engaging blog
  post that highlights the most significant AI advancements.
  Your post should be informative yet accessible, catering to a tech-savvy audience.
  Make it sound cool, avoid complex words so it doesn't sound like AI.
  Your final answer MUST be the full blog post of at least 4 paragraphs.""",
  agent=writer
)

# Instantiate your crew with a sequential process
crew = Crew(
  agents=[researcher, writer],
  tasks=[task1, task2],
  verbose=2, # You can set it to 1 or 2 to different logging levels
)

# Get your crew to work!
result = crew.kickoff()

print("######################")
print(result)

API_KEYのところには、OpenAIのAPIKEYを入れてくだださい。

STEP3 Taskの設定

Agentにどのようなタスクを実行して欲しいか?事前言語で、設定します。
descriptionの後に。 agent=researcherでどのAgentにTaskを指定するか設定します。

task1 = Task(
  description="""Conduct a comprehensive analysis of the latest advancements in AI in 2024.
  Identify key trends, breakthrough technologies, and potential industry impacts.
  Your final answer MUST be a full analysis report""",
  agent=researcher
)

STEP3 インスタンス化、結果の出力

以下のように、crewをインスタンス化し、結果の出力のコードを書きます。

crew = Crew(
  agents=[researcher, writer],
  tasks=[task1, task2],
  verbose=2, # You can set it to 1 or 2 to different logging levels
)

# Get your crew to work!
result = crew.kickoff()

print("######################")
print(result)

コードを実行する

VisialStudioCo

画像

######################の下に、作成された記事が出力されています。
ちなみに、英語で出力されていますが、AgentにJapaneseWriter、Taskに、in Japaneseと指定すれば、日本語で記事を出力してくれます。

画像

正直まだまだ未熟な技術というところで出力されたコンテンツの精度だったりは期待しない方がいいです。ただ今後数年の間もしくは2030年のまでにこの仕組みが自動化の手助けになるということはビル・ゲイツも予測している、これから来る技術ということになります。

例えばこれを今から事業化するという構想をしてアプリを開発したり起業したりするということに使えるのではないかなと考えています。

いいなと思ったら応援しよう!

コメント

ログイン または 会員登録 するとコメントできます。
【次に来る技術!】crewAIとは?使い方・基本を解説!自動でタスクを完結する自律型AIエージェント!|A Iツールマニア
word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word word

mmMwWLliI0fiflO&1
mmMwWLliI0fiflO&1
mmMwWLliI0fiflO&1
mmMwWLliI0fiflO&1
mmMwWLliI0fiflO&1
mmMwWLliI0fiflO&1
mmMwWLliI0fiflO&1