在多智能体系统开发中,我们常常面临这样的困惑:如何让不同智能体之间实现精准高效的消息传递?就像快递公司需要将包裹准确送达不同地址一样,AutoGen 框架通过主题(Topic)与订阅(Subscription)机制构建了智能体通信的 "物流网络"。今天,我们将以快递公司的业务场景为例,深入解析四种典型的广播模式,帮助你彻底掌握智能体通信的核心技术。
快递配送体系 | AutoGen 通信机制 |
---|---|
快递公司总部 | AutoGen 运行时环境 |
快递站点 | 智能体实例 |
包裹分类标准 | 主题(Topic) |
站点配送范围 | 订阅(Subscription) |
包裹分拣流程 | 消息路由逻辑 |
想象一家名为 "智能速递" 的快递公司,拥有多个区域站点,每个站点负责特定区域的配送,同时公司提供普通件、急件、冷链等不同类型的快递服务。我们将通过这家公司的四种运营模式,类比 AutoGen 的四种广播场景。
在公司初创阶段,"智能速递" 只有一个配送站点,所有快递员处理所有类型的包裹:
python
async def run_single_tenant_single_scope():
# 创建快递运行时(类比AutoGen运行时)
runtime = SingleThreadedAgentRuntime()
# 注册快递员智能体(处理不同类型包裹)
await CourierAgent.register(
runtime,
"Courier_General",
lambda: CourierAgent(
"全能快递员",
service_type="General",
system_messages=[SystemMessage(content="处理所有类型包裹")]
)
)
await CourierAgent.register(
runtime,
"Courier_Fragile",
lambda: CourierAgent(
"易碎品快递员",
service_type="Fragile",
system_messages=[SystemMessage(content="处理易碎品包裹")]
)
)
# 添加默认订阅(接收所有包裹分类)
await runtime.add_subscription(DefaultSubscription(agent_type="Courier_General"))
await runtime.add_subscription(DefaultSubscription(agent_type="Courier_Fragile"))
# 模拟包裹到达并分发
runtime.start()
await runtime.publish_message(
ParcelRequest("普通包裹:书籍"),
DefaultTopicId()
)
await runtime.stop_when_idle()
plaintext
==================================================
快递员 Courier_General 处理包裹:普通包裹:书籍
==================================================
快递员 Courier_Fragile 处理包裹:普通包裹:书籍
"智能速递" 扩展业务,在不同城市开设站点,每个站点独立运营:
python
async def run_multi_tenant_single_scope():
runtime = SingleThreadedAgentRuntime()
cities = ["北京", "上海", "广州"] # 多租户
# 注册各城市快递员
for city in cities:
for service in ["General", "Fragile"]:
agent_type = f"Courier_{city}_{service}"
await CourierAgent.register(
runtime,
agent_type,
lambda c=city, s=service: CourierAgent(
f"{c}站点{s}快递员",
city=city,
service_type=s,
system_messages=[SystemMessage(content=f"处理{c}站点{s}包裹")]
)
)
# 添加默认订阅(接收本城市包裹)
await runtime.add_subscription(
DefaultSubscription(agent_type=agent_type, topic_source=city)
)
runtime.start()
# 向不同城市站点发布包裹请求
for city in cities:
topic_id = DefaultTopicId(source=city)
await runtime.publish_message(
ParcelRequest(f"{city}站点:普通文件"),
topic_id
)
await asyncio.sleep(1)
await runtime.stop_when_idle()
plaintext
==================================================
北京站点General快递员处理包裹:北京站点:普通文件
==================================================
北京站点Fragile快递员处理包裹:北京站点:普通文件
...
==================================================
广州站点General快递员处理包裹:广州站点:普通文件
"智能速递" 单一站点内部分工细化,按包裹类型设立专门配送组:
python
async def run_single_tenant_multiple_scope():
runtime = SingleThreadedAgentRuntime()
# 注册不同类型快递员并添加类型订阅
for service in ["General", "Fragile", "ColdChain"]:
agent_type = f"Courier_{service}"
await CourierAgent.register(
runtime,
agent_type,
lambda s=service: CourierAgent(
f"{s}快递组",
service_type=s,
system_messages=[SystemMessage(content=f"专业处理{s}包裹")]
)
)
# 订阅对应包裹类型主题
await runtime.add_subscription(
TypeSubscription(topic_type=s, agent_type=agent_type)
)
runtime.start()
# 向不同类型主题发布包裹请求
for service in ["General", "Fragile", "ColdChain"]:
topic_id = TopicId(type=service, source="总部")
await runtime.publish_message(
ParcelRequest(f"{service}包裹:{service}物品"),
topic_id
)
await asyncio.sleep(1)
await runtime.stop_when_idle()
plaintext
==================================================
General快递组处理包裹:General包裹:普通物品
==================================================
Fragile快递组处理包裹:Fragile包裹:易碎物品
==================================================
ColdChain快递组处理包裹:ColdChain包裹:冷链食品
"智能速递" 发展为全国性网络,各城市站点按包裹类型细分:
python
async def run_multi_tenant_multiple_scope():
runtime = SingleThreadedAgentRuntime()
cities = ["北京", "上海", "广州"]
services = ["General", "Fragile", "ColdChain"]
# 注册多维度快递员并添加订阅
for city in cities:
for service in services:
agent_type = f"Courier_{city}_{service}"
await CourierAgent.register(
runtime,
agent_type,
lambda c=city, s=service: CourierAgent(
f"{c}{s}快递员",
city=c,
service_type=s,
system_messages=[SystemMessage(content=f"处理{c}{s}包裹")]
)
)
# 订阅"城市_包裹类型"组合主题
topic_type = f"{c}_{s}"
await runtime.add_subscription(
TypeSubscription(topic_type=topic_type, agent_type=agent_type)
)
runtime.start()
# 向多维度主题发布包裹请求
for city in cities:
for service in services:
topic_id = TopicId(
type=f"{city}_{service}",
source=city
)
await runtime.publish_message(
ParcelRequest(f"{city}{service}包裹:{service}物品"),
topic_id
)
await asyncio.sleep(1)
await runtime.stop_when_idle()
plaintext
==================================================
北京General快递员处理包裹:北京General包裹:普通物品
==================================================
北京Fragile快递员处理包裹:北京Fragile包裹:易碎物品
...
==================================================
广州ColdChain快递员处理包裹:广州ColdChain包裹:冷链食品
场景类型 | 租户维度 | 主题维度 | 核心实现 | 典型应用 |
---|---|---|---|---|
单站点单一分类 | 1 个 | 1 个 | 所有智能体订阅默认主题 | 小型团队开发 |
多站点单一分类 | 多个 | 每个租户 1 个 | 按租户 ID 隔离主题 | 多客户 SaaS 系统 |
单站点多分类 | 1 个 | 多个 | 按主题类型订阅 | 企业部门分工 |
多站点多分类 | 多个 | 租户 × 类型 | 组合维度订阅 | 跨国企业复杂协作 |
通过快递公司的生动类比,我们深入理解了 AutoGen 中主题与订阅机制的四种核心场景。从简单的单站点配送模式到复杂的全国多维度网络,每种场景都为智能体通信提供了特定的解决方案,就像快递公司通过不同的配送策略满足多样化的物流需求。
如果本文对你有帮助,别忘了点赞收藏,关注我,一起探索更高效的开发方式~