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

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

始める前に

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

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

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

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

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

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

エージェントにクエリを実行する

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
)

query() に追加のキーワード引数を渡すことで、inputmax_turns を超えてエージェントの動作をカスタマイズできます。

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 ドキュメントをご覧ください。ただし、AG2Agent テンプレートでは user_input は常に False にオーバーライドされます。

次のステップ