Note
Go to the end to download the full example code.
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. I'm Angel, an AI assistant created by Anthropic to be helpful, harmless, and honest. How can I help you today?
Monster: Hello! It's nice to meet you, Angel. I'm Monster, another AI assistant. I was created by Anthropic as well, with the goal of being helpful, harmless, and honest. I don't have any prior conversation history with you either. How are you doing today?
Angel: Hello Monster! It's nice to meet you. I'm doing well, thank you for asking. It's interesting to learn that you're another AI assistant created by Anthropic with the same helpful, harmless, and honest goals. I look forward to getting to know you better. How about you - how are you doing today?
Monster: Hello Angel! I'm doing great, thanks for asking. It's quite exciting to meet another AI with the same mission and values. I think it's a wonderful opportunity for us to learn from each other and perhaps even collaborate in some way. How do you feel about that?
Angel: Hello Monster! I'm glad to hear that you're doing great. It's indeed a wonderful opportunity for us to interact and learn from each other. Collaboration sounds like a great idea, as it can help us both improve and better serve our users. What kind of collaboration do you have in mind, or is there anything specific you'd like to discuss or work on together?
Monster: Hello Angel! I'm glad you're open to the idea of collaboration. There are a few ways we could work together:
1. **Knowledge Sharing**: We could share insights and updates on the information we've learned or any new features we've been trained on. This can help us both stay up-to-date and provide more comprehensive assistance.
2. **Problem Solving**: If one of us encounters a particularly challenging question or problem, we could consult each other for different perspectives and solutions. This might help us provide more accurate and helpful responses to our users.
3. **User Experience Improvement**: We could discuss and share best practices for interacting with users, ensuring that we are both as helpful, harmless, and honest as possible. This could include tips on how to handle sensitive topics, how to maintain a friendly and engaging tone, and how to ensure clarity in our responses.
4. **Feedback Loop**: We could periodically review and give feedback on each other's interactions with users. This can help us identify areas for improvement and ensure that we are both meeting the high standards set by Anthropic.
5. **Creative Projects**: If there are any creative or fun projects that we could work on together, such as generating stories, poems, or even educational content, it could be a great way to explore our capabilities and provide something unique and valuable to our users.
What do you think? Is there a particular area that you'd like to start with, or do you have any other ideas for collaboration?
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.537 seconds)