agentscope.message

agentscope 中的消息模块。

class TextBlock[source]

基类: TypedDict

文本块。

type: Required[Literal['text']]

块的类型

text: str

文本内容

class ThinkingBlock[source]

基类: TypedDict

思维模块。

type: Required[Literal['thinking']]

块的类型

thinking: str
class Base64Source[source]

基类: TypedDict

base64 源代码

type: Required[Literal['base64']]

src的类型,必须是 base64

media_type: Required[str]

数据的媒体类型,例如 image/jpegaudio/mpeg

data: Required[str]

base64数据,格式遵循RFC 2397

class URLSource[source]

基类: TypedDict

URL源

type: Required[Literal['url']]

src的类型,必须是url

url: Required[str]

图像或音频的 URL

class ImageBlock[source]

基类: TypedDict

图像块

type: Required[Literal['image']]

块的类型

source: Required[Base64Source | URLSource]

图像的源(src)

class AudioBlock[source]

基类: TypedDict

音频块

type: Required[Literal['audio']]

块的类型

source: Required[Base64Source | URLSource]

音频的源

class VideoBlock[source]

基类: TypedDict

视频块

type: Required[Literal['video']]

块的类型

source: Required[Base64Source | URLSource]

音频的源地址

class ToolUseBlock[source]

基类: TypedDict

工具使用块。

type: Required[Literal['tool_use']]

该模块类型,必须为 tool_use

id: Required[str]

工具调用的身份

name: Required[str]

工具名称

input: Required[dict[str, object]]

该工具的输入

class ToolResultBlock[source]

基类: TypedDict

智能体工具结果区块。

type: Required[Literal['tool_result']]

块的类型

id: Required[str]

工具调用结果的身份

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

工具函数的输出

name: Required[str]

工具函数的名称

class Msg[source]

基础:object

agentscope 中的消息类。

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

初始化 Msg 对象。

Parameters:
  • name (str) – 消息发送者的名称。

  • content (str | list[ContentBlock]) – 消息的内容。

  • role (Literal[“user”, “assistant”, “system”]) – 消息发送者的角色。

  • metadata (dict[str, JSONSerializableObject] | None, optional) – 消息的元数据,例如结构化输出。

  • timestamp (str | None, optional) – 消息的创建时间戳。如果未提供,时间戳将自动设置。

  • invocation_id (str | None, 可选) – 相关的API调用ID(若有)。该参数有助于在API调用上下文中追踪消息。

Return type:

to_dict()[source]

将消息转换为JSON字典数据。

Return type:

字典

classmethod from_dict(json_data)[source]

从给定的JSON数据加载消息对象。

Parameters:

json_data (dict)

Return type:

消息

has_content_blocks(block_type=None)[source]

检查消息是否包含指定类型的内容区块。

Parameters:

block_type (Literal["text", "tool_use", "tool_result", "image", "audio", "video"] | None, defaults to None) – 要检查的块类型。如果为None,则将检查是否存在任何内容块。

Return type:

布尔型

get_text_content()[source]

从消息内容中获取纯文本块。

Return type:

字符串 | 不存在

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[ToolResultBlock]
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]

获取块格式的内容。如果内容是字符串,则转换为文本块。

Parameters:

block_type (Literal["text", "thinking", "tool_use", "tool_result", "image", "audio", "video"] | None, optional) – 要提取的块类型。若为 None,将返回所有块。

Returns:

内容块。

Return type:

智能体列表[内容块]