agentscope.message

The message module in agentscope.

class TextBlock[源代码]

基类:TypedDict

The text block.

type: Required['text']

The type of the block

text: str

The text content

class ThinkingBlock[源代码]

基类:TypedDict

The thinking block.

type: Required['thinking']

The type of the block

thinking: str
class Base64Source[源代码]

基类:TypedDict

The base64 source

type: Required['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[源代码]

基类:TypedDict

The URL source

type: Required['url']

The type of the src, must be url

url: Required[str]

The URL of the image or audio

class ImageBlock[源代码]

基类:TypedDict

The image block

type: Required['image']

The type of the block

source: Required[Base64Source | URLSource]

The src of the image

class AudioBlock[源代码]

基类:TypedDict

The audio block

type: Required['audio']

The type of the block

source: Required[Base64Source | URLSource]

The src of the audio

class VideoBlock[源代码]

基类:TypedDict

The video block

type: Required['video']

The type of the block

source: Required[Base64Source | URLSource]

The src of the audio

class ToolUseBlock[源代码]

基类:TypedDict

The tool use block.

type: Required['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[源代码]

基类:TypedDict

The tool result block.

type: Required['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[源代码]

基类:object

The message class in agentscope.

__init__(name, content, role, metadata=None, timestamp=None, invocation_id=None)[源代码]

Initialize the Msg object.

参数:
  • 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.

返回类型:

None

classmethod from_dict(json_data)[源代码]

Load a message object from the given JSON data.

参数:

json_data (dict)

返回类型:

Msg

get_content_blocks(block_type=None)[源代码]

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

参数:

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.

返回:

The content blocks.

返回类型:

List[ContentBlock]

get_text_content(separator='\n')[源代码]

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.

参数:

separator (str)

返回类型:

str | None

has_content_blocks(block_type=None)[源代码]

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

参数:

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.

返回类型:

bool

to_dict()[源代码]

Convert the message into JSON dict data.

返回类型:

dict