序列化组件#

AutoGen 提供了一个 Component 配置类,该类定义了将组件序列化/反序列化为声明性规范的行为。我们可以分别通过调用 .dump_component().load_component() 来完成此操作。这对于调试、可视化,甚至与他人分享您的工作都很有用。在本笔记本中,我们将演示如何将多个组件序列化为声明性规范,例如 JSON 文件。

警告

仅从受信任的来源加载组件。

通过序列化的组件,每个组件都实现了它如何被序列化和反序列化的逻辑 - 即,如何生成声明性规范以及如何将其转换回对象。

在某些情况下,创建对象可能包括执行代码(例如,序列化的函数)。仅从受信任的来源加载组件。

终止条件示例#

在下面的示例中,我们将定义 Python 中的终止条件(代理团队的一部分),将其导出到字典/json,并演示如何从字典/json 加载终止条件对象。

from autogen_agentchat.conditions import MaxMessageTermination, StopMessageTermination

max_termination = MaxMessageTermination(5)
stop_termination = StopMessageTermination()

or_termination = max_termination | stop_termination

or_term_config = or_termination.dump_component()
print("Config: ", or_term_config.model_dump_json())

new_or_termination = or_termination.load_component(or_term_config)
Config:  {"provider":"autogen_agentchat.base.OrTerminationCondition","component_type":"termination","version":1,"component_version":1,"description":null,"config":{"conditions":[{"provider":"autogen_agentchat.conditions.MaxMessageTermination","component_type":"termination","version":1,"component_version":1,"config":{"max_messages":5}},{"provider":"autogen_agentchat.conditions.StopMessageTermination","component_type":"termination","version":1,"component_version":1,"config":{}}]}}

代理示例#

在下面的示例中,我们将定义 Python 中的代理,将其导出到字典/json,并演示如何从字典/json 加载代理对象。

from autogen_agentchat.agents import AssistantAgent, UserProxyAgent
from autogen_ext.models.openai import OpenAIChatCompletionClient

# Create an agent that uses the OpenAI GPT-4o model.
model_client = OpenAIChatCompletionClient(
    model="gpt-4o",
    # api_key="YOUR_API_KEY",
)
agent = AssistantAgent(
    name="assistant",
    model_client=model_client,
    handoffs=["flights_refunder", "user"],
    # tools=[], # serializing tools is not yet supported
    system_message="Use tools to solve tasks.",
)
user_proxy = UserProxyAgent(name="user")
user_proxy_config = user_proxy.dump_component()  # dump component
print(user_proxy_config.model_dump_json())
up_new = user_proxy.load_component(user_proxy_config)  # load component
{"provider":"autogen_agentchat.agents.UserProxyAgent","component_type":"agent","version":1,"component_version":1,"description":null,"config":{"name":"user","description":"A human user"}}
agent_config = agent.dump_component()  # dump component
print(agent_config.model_dump_json())
agent_new = agent.load_component(agent_config)  # load component
{"provider":"autogen_agentchat.agents.AssistantAgent","component_type":"agent","version":1,"component_version":1,"description":null,"config":{"name":"assistant","model_client":{"provider":"autogen_ext.models.openai.OpenAIChatCompletionClient","component_type":"model","version":1,"component_version":1,"config":{"model":"gpt-4o"}},"handoffs":[{"target":"flights_refunder","description":"Handoff to flights_refunder.","name":"transfer_to_flights_refunder","message":"Transferred to flights_refunder, adopting the role of flights_refunder immediately."},{"target":"user","description":"Handoff to user.","name":"transfer_to_user","message":"Transferred to user, adopting the role of user immediately."}],"model_context":{"provider":"autogen_core.model_context.UnboundedChatCompletionContext","component_type":"chat_completion_context","version":1,"component_version":1,"config":{}},"description":"An agent that provides assistance with ability to use tools.","system_message":"Use tools to solve tasks.","reflect_on_tool_use":false,"tool_call_summary_format":"{result}"}}

类似的方法可以用于序列化 MultiModalWebSurfer 代理。

from autogen_ext.agents.web_surfer import MultimodalWebSurfer

agent = MultimodalWebSurfer(
    name="web_surfer",
    model_client=model_client,
    headless=False,
)

web_surfer_config = agent.dump_component()  # dump component
print(web_surfer_config.model_dump_json())

团队示例#

在下面的示例中,我们将定义 Python 中的团队,将其导出到字典/json,并演示如何从字典/json 加载团队对象。

from autogen_agentchat.agents import AssistantAgent, UserProxyAgent
from autogen_agentchat.conditions import MaxMessageTermination
from autogen_agentchat.teams import RoundRobinGroupChat
from autogen_ext.models.openai import OpenAIChatCompletionClient

# Create an agent that uses the OpenAI GPT-4o model.
model_client = OpenAIChatCompletionClient(
    model="gpt-4o",
    # api_key="YOUR_API_KEY",
)
agent = AssistantAgent(
    name="assistant",
    model_client=model_client,
    handoffs=["flights_refunder", "user"],
    # tools=[], # serializing tools is not yet supported
    system_message="Use tools to solve tasks.",
)

team = RoundRobinGroupChat(participants=[agent], termination_condition=MaxMessageTermination(2))

team_config = team.dump_component()  # dump component
print(team_config.model_dump_json())

await model_client.close()
{"provider":"autogen_agentchat.teams.RoundRobinGroupChat","component_type":"team","version":1,"component_version":1,"description":null,"config":{"participants":[{"provider":"autogen_agentchat.agents.AssistantAgent","component_type":"agent","version":1,"component_version":1,"config":{"name":"assistant","model_client":{"provider":"autogen_ext.models.openai.OpenAIChatCompletionClient","component_type":"model","version":1,"component_version":1,"config":{"model":"gpt-4o"}},"handoffs":[{"target":"flights_refunder","description":"Handoff to flights_refunder.","name":"transfer_to_flights_refunder","message":"Transferred to flights_refunder, adopting the role of flights_refunder immediately."},{"target":"user","description":"Handoff to user.","name":"transfer_to_user","message":"Transferred to user, adopting the role of user immediately."}],"model_context":{"provider":"autogen_core.model_context.UnboundedChatCompletionContext","component_type":"chat_completion_context","version":1,"component_version":1,"config":{}},"description":"An agent that provides assistance with ability to use tools.","system_message":"Use tools to solve tasks.","reflect_on_tool_use":false,"tool_call_summary_format":"{result}"}}],"termination_condition":{"provider":"autogen_agentchat.conditions.MaxMessageTermination","component_type":"termination","version":1,"component_version":1,"config":{"max_messages":2}}}}