.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "build_tutorial/message.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_build_tutorial_message.py: .. _message: 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"`. .. GENERATED FROM PYTHON SOURCE LINES 17-21 .. code-block:: Python from agentscope.message import Msg import json .. GENERATED FROM PYTHON SOURCE LINES 22-25 Create a Message ---------------- Message can be created by specifying the `name`, `role`, and `content` fields. .. GENERATED FROM PYTHON SOURCE LINES 25-37 .. code-block:: Python 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}"') .. rst-class:: sphx-glr-script-out .. code-block:: none The sender of the message: "Jarvis" The role of the sender: "assistant" The content of the message: "Hi! How can I help you?" .. GENERATED FROM PYTHON SOURCE LINES 38-41 Serialize ---------------- Message can be serialized to a string in JSON format. .. GENERATED FROM PYTHON SOURCE LINES 41-47 .. code-block:: Python serialized_msg = msg.to_dict() print(type(serialized_msg)) print(json.dumps(serialized_msg, indent=4)) .. rst-class:: sphx-glr-script-out .. code-block:: none { "__module__": "agentscope.message.msg", "__name__": "Msg", "id": "56915c2e9a96457ebcb0596274e5b5ab", "metadata": null, "role": "assistant", "url": null, "name": "Jarvis", "timestamp": "2025-03-10 03:33:15", "content": "Hi! How can I help you?" } .. GENERATED FROM PYTHON SOURCE LINES 48-51 Deserialize ---------------- Deserialize a message from a string in JSON format. .. GENERATED FROM PYTHON SOURCE LINES 51-58 .. code-block:: Python 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}"') .. rst-class:: sphx-glr-script-out .. code-block:: none The sender of the message: "Jarvis" The role of the sender: "assistant" The content of the message: "Hi! How can I help you?" .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.002 seconds) .. _sphx_glr_download_build_tutorial_message.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: message.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: message.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: message.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_