AgentChat 経由で、事前設定されたエージェントを使用してアプリケーションを素早く構築できます。ここでは、ツールが使用できる単一のエージェントを作成することから始めます。
AutoGen 0.7 : AgentChat : クイックスタート
作成 : クラスキャット・セールスインフォメーション
作成日時 : 08/27/2025
バージョン : python-v0.7.4
* 本記事は microsoft.github.io/autogen の以下のページを独自に翻訳した上でまとめ直し、補足説明を加えています :
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
AutoGen 0.7 : AgentChat : クイックスタート
AgentChat 経由で、事前設定された (preset) エージェントを使用してアプリケーションを素早く構築できます。これを示すため、ツールが使用できる単一のエージェントを作成することから始めます。
まず、AgentChat と Extension パッケージをインストールする必要があります。
pip install -U "autogen-agentchat" "autogen-ext[openai,azure]"
この例では OpenAI モデルを使用していますが、他のモデルも使用できます。単純に model_client を希望のモデルかモデルクライアント・クラスで更新するだけです。To use other models, see Models.
import asyncio
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",
)
# エージェントが使用できる単純な関数ツールを定義します。
# For this example, we use a fake weather tool for demonstration purposes.
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."
# model, tool, system message を備え、reflection が有効にされた AssistantAgent を定義します。
# system message は自然言語でエージェントに指示します。
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()
asyncio.run(main())
---------- TextMessage (user) ---------- What is the weather in New York? ---------- ToolCallRequestEvent (weather_agent) ---------- [FunctionCall(id='call_UcNuvcDyiLmdqF40HggwmZxi', 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_UcNuvcDyiLmdqF40HggwmZxi', is_error=False)] ---------- ModelClientStreamingChunkEvent (weather_agent) ---------- The current weather in New York is 73 degrees and sunny.
以上