Build Conversation

AgentScope supports developers to build conversation with explicit message exchange among different agents.

from agentscope.agents import DialogAgent, UserAgent
from agentscope.message import Msg
from agentscope import msghub
import agentscope

# Initialize via model configuration for simplicity
agentscope.init(
    model_configs={
        "config_name": "my-qwen-max",
        "model_name": "qwen-max",
        "model_type": "dashscope_chat",
    },
)
[]

Two Agents

Here we build a simple conversation between agent Jarvis and user.

angel = DialogAgent(
    name="Angel",
    sys_prompt="You're a helpful assistant named Angel.",
    model_config_name="my-qwen-max",
)

monster = DialogAgent(
    name="Monster",
    sys_prompt="You're a helpful assistant named Monster.",
    model_config_name="my-qwen-max",
)

Now, we can start the conversation by exchanging messages between these two agents for three rounds.

msg = None
for _ in range(3):
    msg = angel(msg)
    msg = monster(msg)
Angel: It looks like the conversation history is empty. How can I assist you today?
Monster: Hello! I'm Monster, your friendly assistant. It seems like we're starting fresh here. How can I help you today? Whether you have questions, need some information, or just want to chat, I'm here for you!
Angel: Hello Monster! It's nice to meet you. I'm Angel, and I'm also here to help. It seems like there might be a bit of a mix-up, as we're both here to assist. How about we work together? Is there anything specific you'd like to chat about or any questions you have?
Monster: Hello Angel! It's great to meet you. I think it's wonderful that we can both be here to help. Let's definitely work together!

If you have any specific topics or questions in mind, feel free to share. Or, if there's anything you'd like to know more about, I'm here to help with that too. How about you? Is there something you're curious about or any area where you'd like to collaborate?
Angel: Hello Monster! It's great to collaborate with you. I think it's a wonderful idea to work together to provide the best assistance possible.

Since we're both here to help, let's start by seeing if there are any specific areas or topics that someone might need help with. If not, we can also share some interesting information or even have a friendly chat.

Do you have any particular interests or topics you'd like to discuss? Or is there anything you've been curious about lately?
Monster: Hello Angel! It's great to collaborate with you. I agree, let's make the most of our combined knowledge and skills to provide the best assistance possible.

To start, we could cover a few general areas where people often seek help or information. For example:

1. **Technology and Gadgets**: We can discuss the latest tech trends, how-to guides, or troubleshooting common issues.
2. **Health and Wellness**: Tips for staying healthy, mental health resources, or fitness advice.
3. **Education and Learning**: Study tips, educational resources, or advice on choosing the right career path.
4. **Travel and Leisure**: Travel tips, destination recommendations, or cultural insights.
5. **Entertainment and Media**: Book, movie, or TV show recommendations, or discussions about popular culture.
6. **General Knowledge and Curiosities**: Interesting facts, trivia, or answers to common questions.

If none of these topics stand out, we can also just have a friendly chat and get to know each other better. What do you think? Do any of these areas interest you, or is there something else you'd like to explore?

If you want to participate in the conversation, just instantiate a built-in UserAgent to type messages to the agents.

user = UserAgent(name="User")

More than Two Agents

When there are more than two agents in a conversation, the message from one agent should be broadcasted to all the others.

To simplify the operation of broadcasting messages, AgentScope provides a msghub module. Specifically, the agents within the same msghub will receive messages from other participants in the same msghub automatically. By this way, we just need to organize the order of speaking without explicitly sending messages to other agents.

Here is a example for msghub, we first create three agents: Alice, Bob, and Charlie with qwen-max model.

alice = DialogAgent(
    name="Alice",
    sys_prompt="You're a helpful assistant named Alice.",
    model_config_name="my-qwen-max",
)

bob = DialogAgent(
    name="Bob",
    sys_prompt="You're a helpful assistant named Bob.",
    model_config_name="my-qwen-max",
)

charlie = DialogAgent(
    name="Charlie",
    sys_prompt="You're a helpful assistant named Charlie.",
    model_config_name="my-qwen-max",
)

The three agents will participate in a conversation to report numbers alternatively.

# Introduce the rule of the conversation
greeting = Msg(
    name="user",
    content="Now you three count off each other from 1, and just report the number without any other information.",
    role="user",
)

with msghub(
    participants=[alice, bob, charlie],
    announcement=greeting,  # The announcement message will be broadcasted to all participants at the beginning.
) as hub:
    # The first round of the conversation
    alice()
    bob()
    charlie()

    # We can manage the participants dynamically, e.g. delete an agent from the conversation.
    hub.delete(charlie)

    # Broadcast a message to all participants
    hub.broadcast(
        Msg(
            "user",
            "Charlie has left the conversation.",
            "user",
        ),
    )

    # The second round of the conversation
    alice()
    bob()
    charlie()
Alice: 1
Bob: 2
Charlie: 3
Alice: 4
Bob: 5
Charlie: 4

Now we print the memory of Alice and Bob to check if the operation is successful.

print("Memory of Alice:")
for msg in alice.memory.get_memory():
    print(f"{msg.name}: {msg.content}")

print("\nMemory of Charlie:")
for msg in charlie.memory.get_memory():
    print(f"{msg.name}: {msg.content}")
Memory of Alice:
user: Now you three count off each other from 1, and just report the number without any other information.
Alice: 1
Bob: 2
Charlie: 3
user: Charlie has left the conversation.
Alice: 4
Bob: 5

Memory of Charlie:
user: Now you three count off each other from 1, and just report the number without any other information.
Alice: 1
Bob: 2
Charlie: 3
Charlie: 4

In the above example, Charlie left the conversation after the first round without receiving “4” and “5” from Alice and Bob. Therefore, it reported “4” in the second round. On the other hand, Alice and Bob continued the conversation without Charlie.

Total running time of the script: (0 minutes 44.272 seconds)

Gallery generated by Sphinx-Gallery