AgentChat を介して、プリセットされたエージェントを使用してアプリケーションを迅速に構築することができます。AgentChat はマルチエージェント・アプリケーションを構築するための高水準 API で、autogen-core パッケージの上に構築されています。初心者ユーザは、AgentChat が推奨される開始点です。
AutoGen AgentChat : クイックスタート
作成 : クラスキャット・セールスインフォメーション
作成日時 : 04/17/2025
* 本記事は github microsoft/autogen の以下のページを独自に翻訳した上でまとめ直し、補足説明を加えています :
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
◆ お問合せ : 下記までお願いします。
- クラスキャット セールス・インフォメーション
- sales-info@classcat.com
- ClassCatJP
AutoGen AgentChat : クイックスタート
AgentChat はマルチエージェント・アプリケーションを構築するための高水準 API です。それは autogen-core パッケージの上に構築されています。初心者ユーザは、AgentChat が推奨される開始点です。上級者ユーザには、autogen-core のイベント駆動型プログラミングモデルが、基盤コンポーネントに対してより柔軟さや制御を提供します。
AgentChat は、事前設定された動作を持つ エージェント や マルチエージェント設計パターン が事前定義された チーム のような、直感的なデフォルト (機能) を提供します。
クイックスタート
AgentChat を介して、プリセットされたエージェントを使用してアプリケーションを迅速に構築することができます。これを例示するため、ツールを使用できる単一のエージェントを作成することから始めましょう。
まず、AgentChat と拡張パッケージをインストールする必要があります。
pip install -U "autogen-agentchat" "autogen-ext[openai,azure]"
この例は OpenAI モデルを使用していますが、他のモデルも同様に使用できます。単に model_client を希望のモデルやモデルクライアント・クラスに変更するだけです。
Azure OpenAI モデルと AAD 認証を使用するには、ここ の手順に従うことができます。To use other models, see Models.
from autogen_agentchat.agents import AssistantAgent
from autogen_agentchat.ui import Console
from autogen_ext.models.openai import OpenAIChatCompletionClient
# モデルクライアントの定義。`ChatCompletionClient` I/F を実装する他のモデルクライアントも使用可能。
model_client = OpenAIChatCompletionClient(
model="gpt-4o",
# api_key="YOUR_API_KEY",
)
# エージェントが使用可能な単純な関数ツールの定義。この例では、デモ目的で fake 天気ツールを使用。
async def get_weather(city: str) -> str:
"""Get the weather for a given city."""
return f"The weather in {city} is 73 degrees and Sunny."
# モデル、ツール、システムメッセージ、そしてリフレクションを有効にして AssistantAgent を定義します。
# システムメッセージは自然言語でエージェントに指示します。
agent = AssistantAgent(
name="weather_agent",
model_client=model_client,
tools=[get_weather],
system_message="You are a helpful assistant.",
reflect_on_tool_use=True,
model_client_stream=True, # Enable streaming tokens from the model client.
)
# Run the agent and stream the messages to the console.
async def main() -> None:
await Console(agent.run_stream(task="What is the weather in New York?"))
# Close the connection to the model client.
await model_client.close()
# NOTE: if running this inside a Python script you'll need to use asyncio.run(main()).
await main()
---------- user ---------- What is the weather in New York? ---------- weather_agent ---------- [FunctionCall(id='call_bE5CYAwB7OlOdNAyPjwOkej1', arguments='{"city":"New York"}', name='get_weather')] ---------- weather_agent ---------- [FunctionExecutionResult(content='The weather in New York is 73 degrees and Sunny.', call_id='call_bE5CYAwB7OlOdNAyPjwOkej1', is_error=False)] ---------- weather_agent ---------- The current weather in New York is 73 degrees and sunny.
実行例
---------- TextMessage (user) ---------- What is the weather in New York? ---------- ToolCallRequestEvent (weather_agent) ---------- [FunctionCall(id='call_hPldq9J0Yw8swO0a5s7vRSQ1', arguments='{"city":"New York"}', name='get_weather')] ---------- ToolCallExecutionEvent (weather_agent) ---------- [FunctionExecutionResult(content='The weather in New York is 73 degrees and Sunny.', name='get_weather', call_id='call_hPldq9J0Yw8swO0a5s7vRSQ1', is_error=False)] ---------- ModelClientStreamingChunkEvent (weather_agent) ---------- The current weather in New York is 73 degrees and sunny.
以上