AutoGen core はイベント駆動型、分散型、スケーラブル、そして耐障害性の高い AI エージェント・システムを迅速に構築する簡単な方法を提供します。エージェントはアクターモデルを使用して開発されます。
AutoGen Core : クイックスタート
作成 : クラスキャット・セールスインフォメーション
作成日時 : 04/07/2025
* 本記事は github microsoft/autogen の以下のページを独自に翻訳した上でまとめ直し、補足説明を加えています :
* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。
◆ お問合せ : 下記までお願いします。
- クラスキャット セールス・インフォメーション
- sales-info@classcat.com
- ClassCatJP
AutoGen Core
AutoGen core はイベント駆動型、分散型、スケーラブル、そして耐障害性の高い AI エージェント・システムを迅速に構築する簡単な方法を提供します。エージェントはアクターモデルを使用して開発されます。エージェントシステムをローカルで構築して実行し、準備ができたらクラウド内の分散システムに簡単に移行できます。
Install using pip
pip を使用して autogen-core パッケージをインストールします :
pip install "autogen-core"
モデル・クライアント用 OpenAI のインストール
OpenAI モデルを使用するには、以下の拡張モジュールをインストールする必要があります :
pip install "autogen-ext[openai]"
AutoGen Core : クイックスタート
core API に深入りする前に、10 から 1 までカウントダウンする 2 つのエージェントの単純なサンプルから始めましょう。
最初に、エージェント・クラスとメッセージ処理用のそれぞれの手続きを定義します。2 つのエージェント・クラスを作成します : Modifier と Checker です。Modifier エージェントは指定された数字を変更し、Checker エージェントは条件に対して値をチェックします。また、Message データクラスも作成します、これはエージェント間で渡されるメッセージを定義します。
from dataclasses import dataclass
from typing import Callable
from autogen_core import DefaultTopicId, MessageContext, RoutedAgent, default_subscription, message_handler
@dataclass
class Message:
content: int
@default_subscription
class Modifier(RoutedAgent):
def __init__(self, modify_val: Callable[[int], int]) -> None:
super().__init__("A modifier agent.")
self._modify_val = modify_val
@message_handler
async def handle_message(self, message: Message, ctx: MessageContext) -> None:
val = self._modify_val(message.content)
print(f"{'-'*80}\nModifier:\nModified {message.content} to {val}")
await self.publish_message(Message(content=val), DefaultTopicId()) # type: ignore
@default_subscription
class Checker(RoutedAgent):
def __init__(self, run_until: Callable[[int], bool]) -> None:
super().__init__("A checker agent.")
self._run_until = run_until
@message_handler
async def handle_message(self, message: Message, ctx: MessageContext) -> None:
if not self._run_until(message.content):
print(f"{'-'*80}\nChecker:\n{message.content} passed the check, continue.")
await self.publish_message(Message(content=message.content), DefaultTopicId())
else:
print(f"{'-'*80}\nChecker:\n{message.content} failed the check, stopping.")
既にお気付きかもしれませんが、エージェントのロジックは、それがモデルを使用していようがコード executor を使用していようが、メッセージの配信方法からは完全に分離されています。これがコアとなる考え方です : フレームワークは通信インフラを提供し、エージェントは自身のロジックを担います。通信インフラを エージェント・ランタイム と呼びます。
エージェント・ランタイムはこのフレームワークのキーコンセプトです。メッセージを配信するだけでなく、エージェントのライフサイフルも管理します。従ってエージェントの作成はこのランタイムにより処理されます。
以下のコードは SingleThreadedAgentRuntime (ローカル埋め込みエージェントランタイム実装) を使用してエージェントを登録して実行する方法を示しています。
Note : VSCode や他のエディタを使用している場合、asyncio をインポートして、コードを async def main() -> None: でラップして、そして asyncio.run(main()) 関数でコードを実行することを忘れないでください。
from autogen_core import AgentId, SingleThreadedAgentRuntime
# Create an local embedded runtime.
runtime = SingleThreadedAgentRuntime()
# Register the modifier and checker agents by providing
# their agent types, the factory functions for creating instance and subscriptions.
await Modifier.register(
runtime,
"modifier",
# Modify the value by subtracting 1
lambda: Modifier(modify_val=lambda x: x - 1),
)
await Checker.register(
runtime,
"checker",
# Run until the value is less than or equal to 1
lambda: Checker(run_until=lambda x: x <= 1),
)
# Start the runtime and send a direct message to the checker.
runtime.start()
await runtime.send_message(Message(10), AgentId("checker", "default"))
await runtime.stop_when_idle()
-------------------------------------------------------------------------------- Checker: 10 passed the check, continue. -------------------------------------------------------------------------------- Modifier: Modified 10 to 9 -------------------------------------------------------------------------------- Checker: 9 passed the check, continue. -------------------------------------------------------------------------------- Modifier: Modified 9 to 8 -------------------------------------------------------------------------------- Checker: 8 passed the check, continue. -------------------------------------------------------------------------------- Modifier: Modified 8 to 7 -------------------------------------------------------------------------------- Checker: 7 passed the check, continue. -------------------------------------------------------------------------------- Modifier: Modified 7 to 6 -------------------------------------------------------------------------------- Checker: 6 passed the check, continue. -------------------------------------------------------------------------------- Modifier: Modified 6 to 5 -------------------------------------------------------------------------------- Checker: 5 passed the check, continue. -------------------------------------------------------------------------------- Modifier: Modified 5 to 4 -------------------------------------------------------------------------------- Checker: 4 passed the check, continue. -------------------------------------------------------------------------------- Modifier: Modified 4 to 3 -------------------------------------------------------------------------------- Checker: 3 passed the check, continue. -------------------------------------------------------------------------------- Modifier: Modified 3 to 2 -------------------------------------------------------------------------------- Checker: 2 passed the check, continue. -------------------------------------------------------------------------------- Modifier: Modified 2 to 1 -------------------------------------------------------------------------------- Checker: 1 failed the check, stopping.
エージェントの出力から、modifier と checker の条件付けのように、値が 10 から 1 に正常に減算されたことがわかります。
AutoGen はまた分散エージェントランタイムもサポートしています、これは異なる識別子、言語や依存関係を持つ異なるプロセスやマシン上で実行されるエージェントをホストできます。
エージェントランタイム、通信、メッセージ処理やサブスクリプションの使用方法を学習するには、このクイックスタートに続くセクションを読み続けてください。
以上