agentscope.message

The base class for message unit

class agentscope.message.MessageBase(name: str, content: Any, role: Literal['user', 'system', 'assistant'] = 'assistant', url: Sequence[str] | str | None = None, timestamp: str | None = None, **kwargs: Any)[源代码]

基类:dict

Base Message class, which is used to maintain information for dialog, memory and used to construct prompt.

__init__(name: str, content: Any, role: Literal['user', 'system', 'assistant'] = 'assistant', url: Sequence[str] | str | None = None, timestamp: str | None = None, **kwargs: Any) None[源代码]

Initialize the message object

参数:
  • name (str) – The name of who send the message. It’s often used in role-playing scenario to tell the name of the sender.

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

  • role (Literal[“system”, “user”, “assistant”], defaults to “assistant”) – The role of who send the message. It can be one of the “system”, “user”, or “assistant”. Default to “assistant”.

  • url (Optional[Union[list[str], str]], defaults to None) – A url to file, image, video, audio or website.

  • timestamp (Optional[str], defaults to None) – The timestamp of the message, if None, it will be set to current time.

  • **kwargs (Any) – Other attributes of the message.

to_str() str[源代码]

Return the string representation of the message

serialize() str[源代码]

Return the serialized message.

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

基类:MessageBase

The Message class.

id: str

The id of the message.

name: str

The name of who send the message.

content: Any

The content of the message.

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

The role of the message sender.

metadata: dict | None

Save the information for application’s control flow, or other purposes.

url: Sequence[str] | str | None

A url to file, image, video, audio or website.

timestamp: str

The timestamp of the message.

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

Initialize the message object

参数:
  • name (str) – The name of who send the message.

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

  • role (Literal[“system”, “user”, “assistant”]) – Used to identify the source of the message, e.g. the system information, the user input, or the model response. This argument is used to accommodate most Chat API formats.

  • url (Optional[Union[list[str], str]], defaults to None) – A url to file, image, video, audio or website.

  • timestamp (Optional[str], defaults to None) – The timestamp of the message, if None, it will be set to current time.

  • echo (bool, defaults to False) – Whether to print the message to the console.

  • metadata (Optional[Union[dict, str]], defaults to None) – Save the information for application’s control flow, or other purposes.

  • **kwargs (Any) – Other attributes of the message.

to_str() str[源代码]

Return the string representation of the message

serialize() str[源代码]

Return the serialized message.

class agentscope.message.Tht(content: Any, timestamp: str | None = None, **kwargs: Any)[源代码]

基类:MessageBase

The Thought message is used to record the thought of the agent to help them make decisions and responses. Generally, it shouldn’t be passed to or seen by the other agents.

In our framework, we formulate the thought in prompt as follows: - For OpenAI API calling:

[
    ...
    {
        "role": "assistant",
        "name": "thought",
        "content": "I should ..."
    },
    ...
]
  • For open-source models that accepts string as input:

...
{self.name} thought: I should ...
...

We admit that there maybe better ways to formulate the thought. Users are encouraged to create their own thought formulation methods by inheriting MessageBase class and rewrite the __init__ and to_str function.

class MyThought(MessageBase):
    def to_str(self) -> str:
        # implement your own thought formulation method
        pass
__init__(content: Any, timestamp: str | None = None, **kwargs: Any) None[源代码]

Initialize the message object

参数:
  • name (str) – The name of who send the message. It’s often used in role-playing scenario to tell the name of the sender.

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

  • role (Literal[“system”, “user”, “assistant”], defaults to “assistant”) – The role of who send the message. It can be one of the “system”, “user”, or “assistant”. Default to “assistant”.

  • url (Optional[Union[list[str], str]], defaults to None) – A url to file, image, video, audio or website.

  • timestamp (Optional[str], defaults to None) – The timestamp of the message, if None, it will be set to current time.

  • **kwargs (Any) – Other attributes of the message.

to_str() str[源代码]

Return the string representation of the message

serialize() str[源代码]

Return the serialized message.

class agentscope.message.PlaceholderMessage(name: str, content: Any, url: Sequence[str] | str | None = None, timestamp: str | None = None, host: str | None = None, port: int | None = None, task_id: int | None = None, client: RpcAgentClient | None = None, x: dict | None = None, **kwargs: Any)[源代码]

基类:MessageBase

A placeholder for the return message of RpcAgent.

PLACEHOLDER_ATTRS = {'_client', '_host', '_is_placeholder', '_port', '_stub', '_task_id'}
LOCAL_ATTRS = {'_client', '_host', '_is_placeholder', '_port', '_stub', '_task_id', 'name', 'timestamp'}
__init__(name: str, content: Any, url: Sequence[str] | str | None = None, timestamp: str | None = None, host: str | None = None, port: int | None = None, task_id: int | None = None, client: RpcAgentClient | None = None, x: dict | None = None, **kwargs: Any) None[源代码]

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

参数:
  • name (str) – The name of who send the message. It’s often used in role-playing scenario to tell the name of the sender. However, you can also only use role when calling openai api. The usage of name refers to https://cookbook.openai.com/examples/how_to_format_inputs_to_chatgpt_models.

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

  • role (Literal[“system”, “user”, “assistant”], defaults to “assistant”) – The role of the message, which can be one of the “system”, “user”, or “assistant”.

  • url (Optional[Union[list[str], str]], defaults to None) – A url to file, image, video, audio or website.

  • timestamp (Optional[str], defaults to None) – The timestamp of the message, if None, it will be set to current time.

  • 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 (dict, defaults to None) – Input parameters used to call rpc methods on the client.

to_str() str[源代码]

Return the string representation of the message

update_value() MessageBase[源代码]

Get attribute values from rpc agent server immediately

serialize() str[源代码]

Return the serialized message.

agentscope.message.deserialize(s: str | bytes) MessageBase | Sequence[源代码]

Deserialize json string into MessageBase

agentscope.message.serialize(messages: Sequence[MessageBase] | MessageBase) str[源代码]

Serialize multiple MessageBase instance