agentscope.message package

Submodules

Module contents

The message module of AgentScope.

class agentscope.message.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

class agentscope.message.PlaceholderMessage(host: str | None = None, port: int | None = None, task_id: int | None = None, client: RpcAgentClient | None = None, x: Sequence[Msg] | Msg | None = None)[source]

Bases: Msg

A placeholder for the return message of RpcAgent.

__init__(host: str | None = None, port: int | None = None, task_id: int | None = None, client: RpcAgentClient | None = None, x: Sequence[Msg] | Msg | None = None) None[source]

A placeholder message, records the address of the real message.

Parameters:
  • host (str, defaults to None) – The hostname of the rpc server where the real message is located.

  • port (int, defaults to None) – The port of the rpc server where the real message is located.

  • task_id (int, defaults to None) – The task id of the real message in the rpc server.

  • client (RpcAgentClient, defaults to None) – An RpcAgentClient instance used to connect to the generator of this placeholder.

  • x (Optional[Msg, Sequence[Msg]], defaults to None) – Input parameters used to call rpc methods on the client.

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.

update_value() None[source]

Get attribute values from rpc agent server immediately

to_dict() dict[source]

Serialize the placeholder message.

classmethod from_dict(serialized_dict: dict) PlaceholderMessage[source]

Create a PlaceholderMessage from a dictionary.