AgentOS AG2 : ユーザガイド – 基本概念 : ConversableAgent

ConversableAgent は AG2 の基礎的なビルディングブロックです – AI システムの頭脳とパーソナリティの両方と考えてください。
AG2 システムのすべてのエージェントは ConversableAgent かその上に構築されたもののいずれかで、最も重要なクラスです。

AG2 : ユーザガイド – 基本概念 : ConversableAgent

作成 : クラスキャット・セールスインフォメーション
作成日時 : 09/01/2025
バージョン : v0.9.9

* 本記事は docs.ag2.ai の以下のページを独自に翻訳した上でまとめ直し、補足説明を加えています :

* サンプルコードの動作確認はしておりますが、必要な場合には適宜、追加改変しています。
* ご自由にリンクを張って頂いてかまいませんが、sales-info@classcat.com までご一報いただけると嬉しいです。

 

 

AG2 : ユーザガイド – 基本概念 : ConversableAgent

ConversableAgent は AG2 の基礎的なビルディングブロックです – AI システムの頭脳とパーソナリティの両方と考えてください。LLM 設定が思考力を提供することで、ConversableAgent は以下を行うことができます :

  • 他のエージェントや人間とコミュニケーションする

  • 情報を処理して応答を生成する

  • システムメッセージで定義された指示に従う

  • 必要に応じてツールや関数を実行する

AG2 システムのすべてのエージェントは ConversableAgent かその上に構築されたもののいずれかで、理解するのに最も重要なクラスです。

 

ConversableAgent の作成

LLM 設定をセットアップしたら、ConversableAgent の作成は簡単です :

from autogen import ConversableAgent, LLMConfig

# Create LLM configuration first
llm_config = LLMConfig(api_type="openai", model="gpt-4o-mini")

# Create the agent using the context manager approach
my_agent = ConversableAgent(
    name="helpful_agent",  # Give your agent a unique name
    system_message="You are a helpful AI assistant",  # Define its personality and purpose
    llm_config=llm_config  # Pass the LLM configuration
)

 
キー・パラメータ

ConversableAgent を作成する際、考慮すべき幾つかの重要なパラメータがあります :

  • name: エージェントの一意な識別子

  • system_message: エージェントのロール、パーソナリティと動作を定義する指示

  • llm_config: 言語モデル用の設定 (直接渡すことも、コンテキストマネージャ経由で渡すこともできます)

 

ConversableAgent との相互作用

ConversableAgent とやり取りする最も単純な方法は、run() と process() メソッドを使用することです。基本的な例として :

# Establish the workflow
response = my_agent.run(
    message="What's the capital of France?",
    max_turns=2,  # Limit conversation length
    user_input=True  # Allow user to provide input
)

# Process the workflow
response.process()

 

財政コンプライアンス例

ConversableAgent を使用して単純な財政エージェントを作成しましょう :

from autogen import ConversableAgent, LLMConfig
import os

# Configure the LLM (we created this in the previous section)
llm_config = LLMConfig(
    api_type="openai",
    model="gpt-4o-mini",
    api_key=os.environ["OPENAI_API_KEY"],
    temperature=0.2
)

# Create a basic financial agent
with llm_config:
    finance_agent = ConversableAgent(
        name="finance_agent",
        system_message="You are a financial assistant who helps analyze financial data and transactions."
    )

# Run the agent with a prompt
response = finance_agent.run(
    message="Can you explain what makes a transaction suspicious?",
    max_turns=1
)

# Iterate through the chat automatically with console output
response.process()
--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...
finance_agent (to user):

取引が疑わしいとされる理由はいくつかあります。以下に主な要素を挙げます。

1. **異常な金額**: 通常の取引額から大きく逸脱した金額の取引は、疑わしいと見なされることがあります。

2. **頻繁な取引**: 短期間に同じ口座から多くの取引が行われる場合、特に小額の取引が多い場合は、マネーロンダリングの可能性があります。

3. **不自然なパターン**: 取引のタイミングや頻度が不自然である場合、例えば、特定の時間帯にのみ行われる場合などは疑わしいとされます。

4. **匿名性の高い取引**: 現金やプリペイドカードなど、身元を特定しにくい方法での取引は、リスクが高いと見なされます。

5. **取引先の不明確さ**: 取引先が不明確であったり、実在しない企業や個人との取引が行われている場合も疑わしいとされます。

6. **国際的な取引**: 高リスク国との取引や、制裁対象国との取引は特に注意が必要です。

7. **顧客の行動**: 顧客が取引の理由を説明できない、または矛盾した説明をする場合も疑わしいとされます。

これらの要素は、金融機関や企業が取引を監視し、疑わしい活動を報告する際の基準となります。

--------------------------------------------------------------------------------

この単純な例では :

  • 基本的なシステムメッセージを使用して財政エージェントを作成しました

  • 疑わしい取引きについて簡単な質問をしています

  • max_turns=1 の設定で、会話を 1 ターンだけに制限しています。これは会話を終了させる多くの方法の一つです。Ending a chat により多くのオプションがあります。

 

How to Run This Example

  • 上記のコードをファイル (e.g., financial_compliance.py) に保存します。

  • OpenAI キーを環境変数に設定するか、好みのモデルプロバイダーを使用してください。

  • AG2 がインストールされていることを確認してください: pip install ag2[openai]

  • スクリプトの実行: python financial_compliance.py

 
出力例

コードを実行すると、このような出力が表示されるはずです :

user (to finance_agent):

Can you explain what makes a transaction suspicious?

--------------------------------------------------------------------------------

>>>>>>>> USING AUTO REPLY...
finance_agent (to user):

Certainly! A transaction may be considered suspicious for several reasons, often related to patterns or behaviors that deviate from the norm. Here are some common indicators:

1. **Unusual Amounts**: Transactions that are significantly larger or smaller than typical amounts for a particular account or customer can raise red flags.

2. **Inconsistent Behavior**: If a customer's transaction history suddenly changes—such as a sudden increase in frequency or volume of transactions—it may warrant further investigation.

3. **Geographic Anomalies**: Transactions involving countries or regions known for high levels of fraud or money laundering can be suspicious, especially if the customer has no known connections to those areas.

4. **Rapid Movement of Funds**: Transactions that involve moving money quickly between accounts, especially if they are done in a short time frame, can indicate attempts to obscure the source of funds.

5. **Structuring or Smurfing**: Breaking up large transactions into smaller ones to avoid detection or reporting thresholds can be a sign of suspicious activity.

6. **Lack of Clear Purpose**: Transactions that lack a clear business or personal purpose, or that do not align with the customer's known activities, can be suspicious.

7. **High-Risk Industries**: Transactions involving businesses in industries that are considered high-risk for fraud, such as gambling, adult entertainment, or cryptocurrency, may be flagged.

8. **Unusual Account Activity**: Sudden changes in account behavior, such as a dormant account suddenly becoming active or frequent withdrawals without corresponding deposits, can be suspicious.

9. **Third-Party Transactions**: Transactions involving third parties who are not directly related to the account holder can raise concerns, especially if they are frequent or involve large sums.

10. **Negative Media Reports**: If a person or entity is associated with negative news reports related to fraud, money laundering, or other illicit activities, their transactions may be scrutinized.

Financial institutions often use automated systems to monitor transactions for these and other indicators, and they may file Suspicious Activity Reports (SARs) when they identify potentially suspicious transactions.

--------------------------------------------------------------------------------

>>>>>>>> TERMINATING RUN (16d4d75c-ca0d-4952-bfff-f7802a57e5e7): Maximum turns (1) reached

これは、ConversableAgent でできることの始まりに過ぎません。続くセクションでは、この例を拡張して、より複雑な財政タスクとインタラクションを処理するようにします。

 

以上