GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing doc.agentscope.io — Create Message - AgentScope Note Go to the end to download the full example code. Create Message¶Message is the core concept in AgentScope, used to support multimodal data, tools API, information storage/exchange and prompt construction. A message consists of four fields:
The types and descriptions of these fields are as follows:
Tip
Next, we introduce the supported blocks in the from agentscope.message import ( Msg, Base64Source, TextBlock, ThinkingBlock, ImageBlock, AudioBlock, VideoBlock, ToolUseBlock, ToolResultBlock, ) import json Creating Textual Message¶Creating a message object by providing the msg = Msg( name="Jarvis", role="assistant", content="Hi! How can I help you?", ) print(f"The name of the sender: {msg.name}") print(f"The role of the sender: {msg.role}") print(f"The content of the message: {msg.content}") The name of the sender: Jarvis The role of the sender: assistant The content of the message: Hi! How can I help you? Creating Multimodal Message¶The message class supports multimodal content by providing different content blocks:
For msg = Msg( name="Jarvis", role="assistant", content=[ TextBlock( type="text", text="This is a multimodal message with base64 encoded data.", ), ImageBlock( type="image", source=Base64Source( type="base64", media_type="image/jpeg", data="/9j/4AAQSkZ...", ), ), AudioBlock( type="audio", source=Base64Source( type="base64", media_type="audio/mpeg", data="SUQzBAAAAA...", ), ), VideoBlock( type="video", source=Base64Source( type="base64", media_type="video/mp4", data="AAAAIGZ0eX...", ), ), ], ) Creating Thinking Message¶The msg_thinking = Msg( name="Jarvis", role="assistant", content=[ ThinkingBlock( type="thinking", thinking="I'm building an example for thinking block in AgentScope.", ), TextBlock( type="text", text="This is an example for thinking block.", ), ], ) Creating Tool Use/Result Message¶The msg_tool_call = Msg( name="Jarvis", role="assistant", content=[ ToolUseBlock( type="tool_use", id="343", name="get_weather", input={ "location": "Beijing", }, ), ], ) msg_tool_res = Msg( name="system", role="system", content=[ ToolResultBlock( type="tool_result", id="343", name="get_weather", output="The weather in Beijing is sunny with a temperature of 25°C.", ), ], ) Tip Refer to the Tool section for more information about tools API in AgentScope. Serialization and Deserialization¶Message object can be serialized and deserialized by serialized_msg = msg.to_dict() print(type(serialized_msg)) print(json.dumps(serialized_msg, indent=4)) <class 'dict'>
{
"id": "E6TotD4eqLarzHYHUtJCtw",
"name": "Jarvis",
"role": "assistant",
"content": [
{
"type": "text",
"text": "This is a multimodal message with base64 encoded data."
},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "/9j/4AAQSkZ..."
}
},
{
"type": "audio",
"source": {
"type": "base64",
"media_type": "audio/mpeg",
"data": "SUQzBAAAAA..."
}
},
{
"type": "video",
"source": {
"type": "base64",
"media_type": "video/mp4",
"data": "AAAAIGZ0eX..."
}
}
],
"metadata": {},
"timestamp": "2026-05-25 08:41:13.020"
}
Deserialize a message from a string in JSON format. new_msg = Msg.from_dict(serialized_msg) print(type(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: "{json.dumps(new_msg.content, indent=4)}"') <class 'agentscope.message._message_base.Msg'>
The sender of the message: "Jarvis"
The role of the sender: "assistant"
The content of the message: "[
{
"type": "text",
"text": "This is a multimodal message with base64 encoded data."
},
{
"type": "image",
"source": {
"type": "base64",
"media_type": "image/jpeg",
"data": "/9j/4AAQSkZ..."
}
},
{
"type": "audio",
"source": {
"type": "base64",
"media_type": "audio/mpeg",
"data": "SUQzBAAAAA..."
}
},
{
"type": "video",
"source": {
"type": "base64",
"media_type": "video/mp4",
"data": "AAAAIGZ0eX..."
}
}
]"
Property Functions¶To ease the use of message object, AgentScope provides these functions:
Total running time of the script: (0 minutes 0.004 seconds) | ||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |