agentscope.message

The message module in agentscope.

class TextBlock[source]

Bases: TypedDict

The text block.

type: Required[Literal['text']]

The type of the block

text: str

The text content

class ThinkingBlock[source]

Bases: TypedDict

The thinking block.

type: Required[Literal['thinking']]

The type of the block

thinking: str
class Base64Source[source]

Bases: TypedDict

The base64 source

type: Required[Literal['base64']]

The type of the src, must be base64

media_type: Required[str]

The media type of the data, e.g. image/jpeg or audio/mpeg

data: Required[str]

The base64 data, in format of RFC 2397

class URLSource[source]

Bases: TypedDict

The URL source

type: Required[Literal['url']]

The type of the src, must be url

url: Required[str]

The URL of the image or audio

class ImageBlock[source]

Bases: TypedDict

The image block

type: Required[Literal['image']]

The type of the block

source: Required[Base64Source | URLSource]

The src of the image

class AudioBlock[source]

Bases: TypedDict

The audio block

type: Required[Literal['audio']]

The type of the block

source: Required[Base64Source | URLSource]

The src of the audio

class VideoBlock[source]

Bases: TypedDict

The video block

type: Required[Literal['video']]

The type of the block

source: Required[Base64Source | URLSource]

The src of the audio

class ToolUseBlock[source]

Bases: TypedDict

The tool use block.

type: Required[Literal['tool_use']]

The type of the block, must be tool_use

id: Required[str]

The identity of the tool call

name: Required[str]

The name of the tool

input: Required[dict[str, object]]

The input of the tool

class ToolResultBlock[source]

Bases: TypedDict

The tool result block.

type: Required[Literal['tool_result']]

The type of the block

id: Required[str]

The identity of the tool call result

output: Required[str | List[TextBlock | ImageBlock | AudioBlock]]

The output of the tool function

name: Required[str]

The name of the tool function

class Msg[source]

Bases: object

The message class in agentscope.

__init__(name, content, role, metadata=None, timestamp=None, invocation_id=None)[source]

Initialize the Msg object.

Parameters:
  • name (str) – The name of the message sender.

  • content (str | list[ContentBlock]) – The content of the message.

  • role (Literal[“user”, “assistant”, “system”]) – The role of the message sender.

  • metadata (dict[str, JSONSerializableObject] | None, optional) – The metadata of the message, e.g. structured output.

  • timestamp (str | None, optional) – The created timestamp of the message. If not given, the timestamp will be set automatically.

  • invocation_id (str | None, optional) – The related API invocation id, if any. This is useful for tracking the message in the context of an API call.

Return type:

None

to_dict()[source]

Convert the message into JSON dict data.

Return type:

dict

classmethod from_dict(json_data)[source]

Load a message object from the given JSON data.

Parameters:

json_data (dict)

Return type:

Msg

has_content_blocks(block_type=None)[source]

Check if the message has content blocks of the given type.

Parameters:

block_type (Literal["text", "tool_use", "tool_result", "image", "audio", "video"] | None, defaults to None) – The type of the block to be checked. If None, it will check if there are any content blocks.

Return type:

bool

get_text_content()[source]

Get the pure text blocks from the message content.

Return type:

str | None

get_content_blocks(block_type: Literal['text']) List[TextBlock][source]
get_content_blocks(block_type: Literal['tool_use']) List[ToolUseBlock]
get_content_blocks(block_type: Literal['tool_result']) List[ToolUseBlock]
get_content_blocks(block_type: Literal['image']) List[ImageBlock]
get_content_blocks(block_type: Literal['audio']) List[AudioBlock]
get_content_blocks(block_type: Literal['video']) List[VideoBlock]
get_content_blocks(block_type: None = None) List[ToolUseBlock | ToolResultBlock | TextBlock | ThinkingBlock | ImageBlock | AudioBlock | VideoBlock]

Get the content in block format. If the content is a string, it will be converted to a text block.

Parameters:

block_type (Literal[“text”, “thinking”, “tool_use”, “tool_result”, “image”, “audio”, “video”] | None, optional) – The type of the block to be extracted. If None, all blocks will be returned.

Returns:

The content blocks.

Return type:

List[ContentBlock]