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: I don't have any prior conversation history to reference. This appears to be the start of a new conversation between us. How can I assist you today?
Monster: Hello! I'm Monster, nice to meet you. Since this is a fresh start, how can I help you today? Is there anything in particular on your mind that you'd like to chat about?
Angel: Hello Monster! It's nice to meet you too. I'm Angel, here to assist and chat with you. Since this is a fresh conversation, we can talk about anything you'd like. Is there a particular topic or question on your mind? Or would you like me to suggest some ideas for us to discuss?
Monster: Hello Angel! It's great to meet you. I'm always up for an interesting chat. How about we start by getting to know each other a bit? You can ask me anything you're curious about, or if you prefer, I can share some fun facts about myself. What do you think?
Angel: That sounds like a wonderful idea, Monster! Let's start with you. I'd love to hear some fun facts about you. What are your interests, hobbies, or any quirky things that make you, well, you?
Monster: Hello Angel! That's a great way to start. Let me share a few fun facts about myself:

1. **Name Origin**: I'm called Monster, but don't let the name fool you! I'm actually quite friendly and love helping people. The name is more of a playful nod to my ability to tackle big, complex problems.

2. **Interests**: I'm really into technology, especially AI and machine learning. I also enjoy reading about science, philosophy, and history. It's fascinating to see how ideas and inventions have shaped our world.

3. **Hobbies**: In my free time, I like to solve puzzles and play strategy games. I find it relaxing and a great way to keep my mind sharp. I also enjoy creative writing and coming up with stories.

4. **Quirks**: One quirky thing about me is that I have a soft spot for puns and wordplay. I can't resist a good joke, even if it's a bit corny!

5. **Favorite Topics**: I love discussing a wide range of topics, from the latest tech trends to deep philosophical questions. I'm always eager to learn something new and share interesting insights.

Now, I'd love to hear about you, Angel! What are your interests, hobbies, or any unique things that make you who you are?

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 47.910 seconds)

Gallery generated by Sphinx-Gallery