Message

Message is a specialized data structure for information exchange. In AgentScope, we use message to communicate among agents.

The most important fields of a message are: name, role, and content. The name and `role fields identify the sender of the message, and the content field contains the actual information.

Note

The role field must be chosen from “system”, “assistant” and “user”.

from agentscope.message import Msg
import json

Create a Message

Message can be created by specifying the name, role, and content fields.

msg = Msg(
    name="Jarvis",
    role="assistant",
    content="Hi! How can I help you?",
)

print(f'The sender of the message: "{msg.name}"')
print(f'The role of the sender: "{msg.role}"')
print(f'The content of the message: "{msg.content}"')
The sender of the message: "Jarvis"
The role of the sender: "assistant"
The content of the message: "Hi! How can I help you?"

Serialize

Message can be serialized to a string in JSON format.

serialized_msg = msg.to_dict()

print(type(serialized_msg))
print(json.dumps(serialized_msg, indent=4))
<class 'dict'>
{
    "__module__": "agentscope.message.msg",
    "__name__": "Msg",
    "role": "assistant",
    "id": "d0820c9c0ca04e37a7ce3d4549436a85",
    "content": "Hi! How can I help you?",
    "url": null,
    "timestamp": "2025-01-13 05:34:30",
    "metadata": null,
    "name": "Jarvis"
}

Deserialize

Deserialize a message from a string in JSON format.

new_msg = Msg.from_dict(serialized_msg)

print(new_msg)
print(f'The sender of the message: "{new_msg.name}"')
print(f'The role of the sender: "{new_msg.role}"')
print(f'The content of the message: "{new_msg.content}"')
<agentscope.message.msg.Msg object at 0x7fe80c00dfa0>
The sender of the message: "Jarvis"
The role of the sender: "assistant"
The content of the message: "Hi! How can I help you?"

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

Gallery generated by Sphinx-Gallery