agentscope.msghub module

MsgHub is designed to share messages among a group of agents.

class agentscope.msghub.MsgHubManager(participants: Sequence[AgentBase], announcement: Sequence[Msg] | Msg | None = None)[源代码]

基类:object

MsgHub manager class for sharing dialog among a group of agents.

__init__(participants: Sequence[AgentBase], announcement: Sequence[Msg] | Msg | None = None) None[源代码]

Initialize a msghub manager from the given arguments.

参数:
  • participants (Sequence[AgentBase]) – The Sequence of participants in the msghub.

  • announcement – (Optional[Union[list[Msg], Msg]], defaults to None): The message that will be broadcast to all participants at the first without requiring response.

add(new_participant: Sequence[AgentBase] | AgentBase) None[源代码]

Add new participant into this hub

delete(participant: Sequence[AgentBase] | AgentBase) None[源代码]

Delete agents from participant.

broadcast(msg: Msg | Sequence[Msg]) None[源代码]

Broadcast the message to all participants.

参数:

msg (Union[Msg, Sequence[Msg]]) – One or a list of dict messages to broadcast among all participants.

agentscope.msghub.msghub(participants: Sequence[AgentBase], announcement: Sequence[Msg] | Msg | None = None) MsgHubManager[源代码]

msghub is used to share messages among a group of agents.

参数:
  • participants (Sequence[AgentBase]) – A Sequence of participated agents in the msghub.

  • announcement (Optional[Union[list[Msg], Msg]], defaults to None) – The message that will be broadcast to all participants at the very beginning without requiring response.

示例

In the following code, we create a msghub with three agents, and each message output by agent1, agent2, agent3 will be passed to all other agents, that’s what we mean msghub.

with msghub(participant=[agent1, agent2, agent3]):
    agent1()
    agent2()

Actually, it has the same effect as the following code, but much more easy and elegant!

x1 = agent1()
agent2.observe(x1)
agent3.observe(x1)

x2 = agent2()
agent1.observe(x2)
agent3.observe(x2)