【AutoGen0.7.1】 SelectorGroupChat(3)Agent毎LLM割付: 2025/7/30

エージェント

今回は各エージェントに異なる LLM(大規模言語モデル)を割り当てる SelectorGroupChat のサンプルコードを、行ごとに揃った形でコメント付きで解説していきます。


🎯 目的

  • AssistantAgentA:GPT-4 を使う
  • AssistantAgentB:GPT-3.5 を使う
  • UserProxyAgent:人間が割り込めるように準備

🧠 事前準備

pythonCopyEdit# ===== ライブラリのインポート =====
from autogen_agentchat.agents import AssistantAgent, UserProxyAgent # 各種エージェント
from autogen_agentchat.teams import SelectorGroupChat # セレクターベースのグループチャット
from autogen_agentchat.termination import MaxMessageTermination # 終了条件
from autogen_ext.models.openai import OpenAIChatCompletionClient # OpenAI用のモデルクライアント
import asyncio # 非同期処理用

🔧 各LLMクライアントの用意

pythonCopyEdit# ===== モデルクライアントを個別に作成 =====
gpt4_client = OpenAIChatCompletionClient(model="gpt-4") #GPT-4 用クライアント
gpt35_client = OpenAIChatCompletionClient(model="gpt-3.5-turbo") #GPT-3.5用クライアント

👤 エージェントの定義(それぞれ異なるLLMを割り当て)

pythonCopyEdit# ===== AssistantAgent A(GPT-4) =====
agent_a = AssistantAgent(
name="ResearcherGPT4", # エージェント名
model_client=gpt4_client, # GPT-4 を割り当て
description="複雑なリサーチタスクを担当します。" # 役割説明
)

# ===== AssistantAgent B(GPT-3.5) =====
agent_b = AssistantAgent(
name="SummarizerGPT3", # エージェント名
model_client=gpt35_client, # GPT-3.5 を割り当て
description="要約や簡単な説明を担当します。" # 役割説明
)

# ===== User(人間)エージェント =====
user = UserProxyAgent(
name="HumanUser", # エージェント名
human_input_mode="ALWAYS", # 呼ばれたら人間が入力できる
description="必要に応じてアドバイスや質問をします。" # 役割説明
)

✅ チーム(SelectorGroupChat)の構成

pythonCopyEdit# ===== SelectorGroupChat を作成 =====
team = SelectorGroupChat(
agents=[user, agent_a, agent_b], # すべての参加エージェントを登録
model_client=gpt4_client, # selector用LLM(誰を話者にするかを選ぶ)
termination_condition=MaxMessageTermination(max_messages=10), # 10発言で自動終了
allow_repeated_speaker=True   # 同じエージェントが連続OK
)

💡 model_client= は「選択用」の LLM。実際に話す LLM とは独立している!


🚀 実行関数の定義と実行処理

pythonCopyEdit# ===== メイン関数を定義(async) =====
async def main():
await team.run_async("AI に関する技術の最近の進展を調べて、要約してください。")

# タスクを渡して会話を開始

# ===== 非同期で実行するための処理 =====
if __name__ == "__main__":
asyncio.run(main()) # Python の非同期実行

✅ 実行結果のイメージ(流れ)

nginxCopyEditSelector → ResearcherGPT4 を選択
→ GPT-4 でリサーチ実行 → 要約が必要 → SummarizerGPT3 にバトンタッチ
→ GPT-3.5 で要約 → 途中で HumanUser を選択 → 人間が入力
→ 10ターンで自動終了

📌 全体まとめ

要素説明
AssistantAgent個別に model_client を設定できる
SelectorGroupChat「誰が話すか」は selector側の LLM で選択(共通でもOK)
UserProxyAgent人間が割り込みできるように設定可能
run_async()会話を非同期に走らせて、外部から人間が send() で差し込みも可能

コメント

タイトルとURLをコピーしました