除了使用代理的一般说明之外,本页还介绍了特定于 AG2Agent
的功能。
准备工作
本教程假定您已阅读并遵循以下说明:
支持的操作
AG2Agent
支持以下操作:
query
:用于同步获取对查询的响应。
query
方法支持以下参数:
input
:要发送给代理的消息。max_turns
:允许的对话轮次数上限。使用工具时,至少需要max_turns=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
)
除了 input
和 max_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
文档。不过请注意,AG2Agent 模板始终会将 user_input
替换为 False
。