AG2 エージェントを使用する

このページでは、エージェントの使用に関する一般的な手順に加えて、AG2Agent に固有の機能について説明します。

始める前に

このチュートリアルは、次の手順を読んで理解していることを前提としています。

サポートされているオペレーション

AG2Agent でサポートされているオペレーションは次のとおりです。

  • query: クエリへのレスポンスを同期的に取得します。

query メソッドは次の引数をサポートしています。

  • input: エージェントに送信するメッセージ。
  • max_turns: 許可される会話のターンの最大数。ツールを使用する場合は、少なくとも max_turns=2 が必要です。1 ターンはツール引数の生成に、もう 1 ターンはツールの実行に使用されます。

エージェントに問い合わせる

query() メソッドは、エージェントを操作するための簡素な方法を提供します。一般的な呼び出しは次のようになります。

response = agent.query(input="What is the exchange rate from US dollars to Swedish currency?", max_turns=2)

このメソッドは、エージェントとの基盤となる通信を処理し、エージェントの最終的なレスポンスを辞書として返します。これは、次(完全な形式)と同等です。

from autogen import ConversableAgent
import dataclasses
import json

input_message: str = "What is the exchange rate from US dollars to Swedish currency?"
max_turns: int = 2

with agent._runnable._create_or_get_executor(
    tools=agent._ag2_tool_objects,            # Use the agent's existing tools
    agent_name="user",                        # Default
    agent_human_input_mode="NEVER",           # query() enforces this
) as executor:
    chat_result = executor.initiate_chat(
        agent._runnable,
        message=input_message,
        max_turns=max_turns,
        clear_history=False,                  # Default
        summary_method="last_msg"             # Default
    )

response = json.loads(
  json.dumps(dataclasses.asdict(chat_result)) # query() does this conversion
)

inputmax_turns 以外のエージェントの動作をカスタマイズするには、追加のキーワード引数を query() に渡します。

response = agent.query(
    input="What is the exchange rate from US dollars to Swedish currency?",
    max_turns=2,
    msg_to="user"  # Start the conversation with the "user" agent
)
print(response)

使用可能なパラメータの一覧については、ConversableAgent.run のドキュメントをご覧ください。ただし、user_input は AG2Agent テンプレートによって常に False にオーバーライドされることに注意してください。

次のステップ