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)[源代码]

基类: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[源代码]

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.

参数:
  • 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[源代码]

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

参数:

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

返回:

The formatted string of the message.

返回类型:

str

to_dict() dict[源代码]

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

返回:

The serialized dictionary.

返回类型:

dict

classmethod from_dict(serialized_dict: dict) Msg[源代码]

Deserialize the dictionary to a Msg object.

参数:

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

返回:

A Msg object.

返回类型:

Msg