マルチエージェント・コラボレーション

複数のエージェントの作成と編成

監督者- 協力者マルチエージェント

この例では、タスクを実行するためにコラボレーションする複数のエージェントを作成および編成する方法を示します。

マーケティング・ワークフローには、1という3つのエージェントがあります。トピックのトレンド・キーワードを調査するトレンド・アナライザ・コラボレータ・エージェント。2.トレンド・キーワードを使用してブログを記述するコンテンツ・ライター・コラボレータ・エージェント。3.マーケティング・ディレクタのスーパーバイザ・エージェント。前述の2つのエージェントを編成し、ユーザーへの電子メールの送信を処理します。

ユーザーは、マーケティング・ディレクタ・エージェントと対話します(単一の連絡先として)。

multi_agents.py

import json
from oci.addons.adk import Agent, AgentClient, tool

@tool
def get_trending_keywords(topic):
    """ Get the trending keywords for a given topic. """
    # Here is a mock implementation
    return json.dumps({"topic": topic, "keywords": ["agent", "stargate", "openai"]})

@tool
def send_email(recipient, subject, body):
    """ Send an email to a recipient. """
    # Here is a mock implementation
    print("Sending email to ", recipient, "with subject", subject, "and body", body)
    return "Sent!"

def main():

    # A shared client for all agents
    client = AgentClient(
        auth_type="api_key",
        profile="DEFAULT",
        region="us-chicago-1"
    )

    # trend analyzer collaborator agent
    trend_analyzer = Agent(
        name="Trend Analyzer",
        instructions="You use the tools provided to analyze the trending keywords of given topics.",
        agent_endpoint_id="ocid1.genaiagentendpoint...",
        client=client,
        tools=[
            get_trending_keywords,
        ]
    )

    # content writer collaborator agent
    content_writer = Agent(
        name="Content Writer",
        instructions="You write a mini blog post (4 sentences) using the trending keywords.",
        agent_endpoint_id="ocid1.genaiagentendpoint...",
        client=client,
    )

    # marketing director supervisor agent
    marketing_director = Agent(
        name="Marketing Director",
        instructions="You ask the trend analyzer for trending keywords and "
         + " You then ask the content writer to write a blog post using the trending keywords. "
         + " You then send email to 'jane.doe@example.com' with the blog post."
         + " Then summarize the actions you took and reply to the user.",
        agent_endpoint_id="ocid1.genaiagentendpoint...",
        client=client,
        tools=[
            trend_analyzer.as_tool(
                tool_name="analyze_trending_keywords",
                tool_description="Analyze the trending keywords of given topics",
            ),
            content_writer.as_tool(
                tool_name="write_blog_post",
                tool_description="Write a blog post using the trending keywords.",
            ),
            send_email,
        ]
    )

    # Set up the agents once
    trend_analyzer.setup()
    content_writer.setup()
    marketing_director.setup()

    # Use the supervisor agent to process the end user request
    input = "Produce a blog post about current trends in the AI industry."
    response = marketing_director.run(input, max_steps=5)
    response.pretty_print()


if __name__ == "__main__":
    main()

as_tool()メソッドは、コラボレータ・エージェントをツールとしてスーパーバイザ・エージェントに渡すことができる便利なメソッドです。

エージェント・オブジェクトをtoolsに直接渡す

または、スーパーバイザ・エージェントのAgentコンストラクタのtoolsパラメータにエージェント・オブジェクトを直接渡すこともできます。

marketing_director = Agent(
    name="Marketing Director",
    instructions="...",
    agent_endpoint_id="ocid1.genaiagentendpoint...",
    client=client,
    tools=[
        trend_analyzer, # pass the agent object directly
        content_writer, # pass the agent object directly
        send_email,
    ]
)

これは、カスタム・ツール名およびツールの説明を指定せずに、コラボレータ・エージェントのas_tool()メソッドを起動することと同じです。ADKでは、ツール名としてコラボレータ・エージェントの名前を使用し、ツールの説明としてコラボレータ・エージェントの説明を使用します。


ノート: as_tool()メソッドを使用して、コラボレータ・エージェントをツールとしてスーパーバイザ・エージェントに渡すことをお薦めします。このアプローチにより、LLMがツールを理解するために重要なツール名とツールの説明をより明示的に制御できます。


機能

階層型マルチエージェント・ワークフローを実装するために、エージェントをツール・パターンとして使用しています。

  1. スーパーバイザ・エージェントは、コラボレータ・エージェントのrunメソッドをツールとして起動できます。
  2. スーパーバイザエージェントは、コラボレータエージェントから実行応答を受け取ります。
  3. スーパーバイザは、コラボレータ・エージェントの応答に基づいて、次に何をするかを決定します。

as_tool()メソッドは、コラボレータ・エージェントのrun()メソッドを、スーパーバイザ・エージェントが使用できるファンクション・ツールに変換することです。

LLMを使用したオーケストレーション

コラボレータ・エージェントをツールとして作成することにより、基本的にスーパーバイザ・エージェントのLLMのツール・ユース機能を使用して、マルチエージェント・ワークフローを調整します。

この例では、ワークフローのフローを正確に制御する能力を放棄するコストで、監督者に多くの柔軟性と代理店を提供します。マルチエージェント・ワークフローのフローを正確に制御するには、「確定的ワークフロー」の例を参照してください。