agentscope.message¶
The message module in agentscope.
- class TextBlock[source]¶
Bases:
TypedDictThe text block.
- type: Required[Literal['text']]¶
The type of the block
- text: str¶
The text content
- class ThinkingBlock[source]¶
Bases:
TypedDictThe thinking block.
- type: Required[Literal['thinking']]¶
The type of the block
- thinking: str¶
- class Base64Source[source]¶
Bases:
TypedDictThe 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:
TypedDictThe 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:
TypedDictThe image block
- type: Required[Literal['image']]¶
The type of the block
- source: Required[Base64Source | URLSource]¶
The src of the image
- class AudioBlock[source]¶
Bases:
TypedDictThe audio block
- type: Required[Literal['audio']]¶
The type of the block
- source: Required[Base64Source | URLSource]¶
The src of the audio
- class VideoBlock[source]¶
Bases:
TypedDictThe video block
- type: Required[Literal['video']]¶
The type of the block
- source: Required[Base64Source | URLSource]¶
The src of the audio
- class ToolUseBlock[source]¶
Bases:
TypedDictThe 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:
TypedDictThe 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 | VideoBlock]]¶
The output of the tool function
- name: Required[str]¶
The name of the tool function
- class Msg[source]¶
Bases:
objectThe 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
- classmethod from_dict(json_data)[source]¶
Load a message object from the given JSON data.
- Parameters:
json_data (dict)
- Return type:
- 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(separator='\n')[source]¶
Get the pure text blocks from the message content.
- Args:
separator (str, defaults to `
- `):
The separator to use when concatenating multiple text blocks. Defaults to newline character.
- Returns:
- str | None:
The concatenated text content, or None if there is no text content.
- Parameters:
separator (str)
- Return type:
str | None
- get_content_blocks(block_type: Literal['text']) Sequence[TextBlock][source]¶
- get_content_blocks(block_type: Literal['tool_use']) Sequence[ToolUseBlock]
- get_content_blocks(block_type: Literal['tool_result']) Sequence[ToolResultBlock]
- get_content_blocks(block_type: Literal['image']) Sequence[ImageBlock]
- get_content_blocks(block_type: Literal['audio']) Sequence[AudioBlock]
- get_content_blocks(block_type: Literal['video']) Sequence[VideoBlock]
- get_content_blocks(block_type: None = None) Sequence[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 (ContentBlockTypes | List[ContentBlockTypes] | 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]