You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Autogen 0.7 GraphFlow中如何准确获取归档代理的调用方代理名称

解决方案:Autogen 0.7 GraphFlow 中归档代理调用者名称传递问题

核心问题分析

当前归档代理工具调用时传入随机代理名,本质是未在触发路径中显式传递实际调用者身份和触发上下文,导致系统默认生成随机标识而非预期的分类器代理名称。以下是针对性解决方法:


方法1:在触发路径中显式传递调用者上下文

在分类器代理触发归档代理的两条核心路径(分类失败/低置信度、分类成功)中,手动传入调用者名称和触发路径标识,确保归档代理能获取准确信息。

代码示例:

  1. 定义带明确名称的代理
from autogen import AssistantAgent, UserProxyAgent

# 定义分类器代理(指定明确名称)
classifier_agent = AssistantAgent(
    name="ClassifierAgent",
    system_message="负责任务分类,置信度低于0.7或无法分类时触发归档;分类成功后触发清理归档",
    llm_config={"config_list": [{"model": "gpt-4", "api_key": "YOUR_KEY"}]}
)

# 定义归档代理
archiving_agent = AssistantAgent(
    name="ArchivingAgent",
    system_message="Pass on the caller agent name as an argument while calling the tool",
    llm_config={"config_list": [{"model": "gpt-4", "api_key": "YOUR_KEY"}]}
)

# 定义归档工具
def archive_tool(caller_agent_name, trigger_path, task_details):
    # 根据调用者和触发路径生成对应归档文件
    archive_file = f"{caller_agent_name}_{trigger_path}_archive.txt"
    with open(archive_file, "w") as f:
        f.write(f"任务详情:{task_details}")
    return f"归档完成,文件路径:{archive_file}"

# 绑定工具到归档代理
archiving_agent.register_function(
    function_map={"archive_tool": archive_tool}
)
  1. 分类器代理触发归档时传递上下文
def handle_classification_result(message, sender, receiver):
    # 模拟分类结果
    classification_confidence = 0.6
    task_details = message["content"]
    
    if classification_confidence < 0.7:
        # 低置信度触发归档,传递调用者名称和路径标识
        sender.initiate_chat(
            archiving_agent,
            message=f"任务无法高置信度分类,需归档:{task_details}",
            context={"caller_agent_name": sender.name, "trigger_path": "low_confidence_classification"}
        )
    else:
        # 分类成功触发清理归档
        sender.initiate_chat(
            archiving_agent,
            message=f"任务分类完成,需清理归档:{task_details}",
            context={"caller_agent_name": sender.name, "trigger_path": "successful_classification"}
        )

# 绑定分类器的处理逻辑
classifier_agent.register_reply(
    [UserProxyAgent, AssistantAgent], handle_classification_result, position=0
)

方法2:通过GraphFlow边配置传递调用者信息

如果使用Autogen的GraphFlow节点边定义触发逻辑,可在边的action参数中直接传递调用者名称和触发路径:

from autogen import Graph

# 创建图流
graph = Graph()
graph.add_node(classifier_agent)
graph.add_node(archiving_agent)

# 添加分类失败/低置信度到归档的边
graph.add_edge(
    classifier_agent,
    archiving_agent,
    condition=lambda msg: "低置信度" in msg["content"],
    action=lambda sender, receiver, msg: receiver.initiate_chat(
        receiver,
        message=msg["content"],
        context={"caller_agent_name": sender.name, "trigger_path": "low_confidence_classification"}
    )
)

# 添加分类成功到归档的边
graph.add_edge(
    classifier_agent,
    archiving_agent,
    condition=lambda msg: "分类完成" in msg["content"],
    action=lambda sender, receiver, msg: receiver.initiate_chat(
        receiver,
        message=msg["content"],
        context={"caller_agent_name": sender.name, "trigger_path": "successful_classification"}
    )
)

# 启动图流
graph.start()

方法3:归档代理直接从对话上下文提取调用者

修改归档代理的工具调用逻辑,直接从对话历史的from字段获取实际发送者(分类器代理)的名称,无需依赖外部参数传递:

def archive_tool(task_details):
    # 从归档代理的对话历史中获取最后一条消息的发送者
    last_message = archiving_agent.chat_messages[classifier_agent][-1]
    caller_agent_name = last_message["from"]
    # 根据对话内容判断触发路径
    trigger_path = "low_confidence_classification" if "低置信度" in task_details else "successful_classification"
    
    archive_file = f"{caller_agent_name}_{trigger_path}_archive.txt"
    with open(archive_file, "w") as f:
        f.write(f"任务详情:{task_details}")
    return f"归档完成,文件路径:{archive_file}"

# 重新绑定工具
archiving_agent.register_function(
    function_map={"archive_tool": archive_tool}
)

关键注意事项

  • 所有代理必须设置明确的name属性,避免系统生成随机名称;
  • 触发归档的每个路径都需传递一致的上下文字段,确保归档工具能稳定识别调用者和触发场景;
  • 若使用GraphFlow,需确保边的condition逻辑能准确匹配分类结果的触发条件。

内容的提问来源于stack exchange,提问作者Subramanian

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.11 22:13:11