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

基类: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

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

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

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

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

Get attribute values from rpc agent server immediately

to_dict() dict[源代码]

Serialize the placeholder message.

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

Create a PlaceholderMessage from a dictionary.