agentscope.message.msg module

The base class for message unit

class agentscope.message.msg.Msg(name: str, content: Any, role: str | Literal['system', 'user', 'assistant'], url: str | List[str] | None = None, metadata: str | dict | None = None, echo: bool = False, **kwargs: Any)[source]

Bases: object

The message class for AgentScope, which is responsible for storing the information of a message, including

  • id: the identity of the message

  • name: who sends the message

  • content: the message content

  • role: the sender role chosen from ‘system’, ‘user’, ‘assistant’

  • url: the url(s) refers to multimodal content

  • metadata: some additional information

  • timestamp: when the message is created

__init__(name: str, content: Any, role: str | Literal['system', 'user', 'assistant'], url: str | List[str] | None = None, metadata: str | dict | None = None, echo: bool = False, **kwargs: Any) None[source]

Initialize the message object.

There are two ways to initialize a message object: - Providing name, content, role, `url`(Optional),

`metadata`(Optional) to initialize a normal message object.

  • Providing host, port, task_id to initialize a placeholder.

Normally, users only need to create a normal message object by providing name, content, role, url`(Optional) and `metadata (Optional).

The initialization of message has a high priority, which means that when name, content, role, host, port, task_id are all provided, the message will be initialized as a normal message object rather than a placeholder.

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

  • content (Any) – The content of the message.

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

  • url (Optional[Union[str, List[str]], defaults to None) – The url of the message.

  • metadata (Optional[Union[dict, str]], defaults to None) – The additional information stored in the message.

  • echo (bool, defaults to False) – Whether to print the message when initializing the message obj.

property id: str

The identity of the message.

property name: str

The name of the message sender.

property content: Any

The content of the message.

property role: Literal['system', 'user', 'assistant']

The role of the message sender, chosen from ‘system’, ‘user’, ‘assistant’.

property url: str | List[str] | None

A URL string or a list of URL strings.

property metadata: str | dict | None

The metadata of the message, which can store some additional information.

property timestamp: str

The timestamp when the message is created.

formatted_str(colored: bool = False) str[source]

Return the formatted string of the message. If the message has an url, the url will be appended to the content.

Parameters:

colored (bool, defaults to False) – Whether to color the name of the message

Returns:

The formatted string of the message.

Return type:

str

to_dict() dict[source]

Serialize the message into a dictionary, which can be deserialized by calling the from_dict function.

Returns:

The serialized dictionary.

Return type:

dict

classmethod from_dict(serialized_dict: dict) Msg[source]

Deserialize the dictionary to a Msg object.

Parameters:

serialized_dict (dict) – A dictionary that must contain the keys in Msg.__serialized_attrs, and the keys __module__ and __name__.

Returns:

A Msg object.

Return type:

Msg