agentscope.agents.agent module

Base class for Agent

class agentscope.agents.agent.AgentBase(*args: tuple, **kwargs: dict)[源代码]

基类:Operator

Base class for all agents.

All agents should inherit from this class and implement the reply function.

__init__(name: str, sys_prompt: str | None = None, model_config_name: str | None = None, use_memory: bool = True, to_dist: DistConf | bool | None = False) None[源代码]

Initialize an agent from the given arguments.

参数:
  • name (str) – The name of the agent.

  • sys_prompt (Optional[str]) – The system prompt of the agent, which can be passed by args or hard-coded in the agent.

  • model_config_name (str, defaults to None) – The name of the model config, which is used to load model from configuration.

  • use_memory (bool, defaults to True) – Whether the agent has memory.

  • to_dist (Optional[Union[DistConf, bool]], default to False) –

    The configurations passed to to_dist() method. Used in RpcMeta, when this parameter is provided, the agent will automatically be converted into its distributed version. Below are some examples:

    # run as a sub process
    agent = XXXAgent(
        # ... other parameters
        to_dist=True,
    )
    
    # connect to an existing agent server
    agent = XXXAgent(
        # ... other parameters
        to_dist=DistConf(
            host="<ip of your server>",
            port=<port of your server>,
            # other parameters
        ),
    )
    

    See Tutorial for detail.

classmethod generate_agent_id() str[源代码]

Generate the agent_id of this agent instance

reply(x: Sequence[Msg] | Msg | None = None) Msg[源代码]

Define the actions taken by this agent.

参数:

x (Optional[Union[Msg, Sequence[Msg]]], defaults to None) – The input message(s) to the agent, which also can be omitted if the agent doesn’t need any input.

返回:

The output message generated by the agent.

返回类型:

Msg

备注

Given that some agents are in an adversarial environment, their input doesn’t include the thoughts of other agents.

speak(content: str | Msg | Generator[Tuple[bool, str], None, None]) None[源代码]

Speak out the message generated by the agent. If a string is given, a Msg object will be created with the string as the content.

参数:
  • content

  • (`Union[str

    The content of the message to be spoken out. If a string is given, a Msg object will be created with the agent’s name, role as “assistant”, and the given string as the content. If the content is a Generator, the agent will speak out the message chunk by chunk.

  • Msg – The content of the message to be spoken out. If a string is given, a Msg object will be created with the agent’s name, role as “assistant”, and the given string as the content. If the content is a Generator, the agent will speak out the message chunk by chunk.

  • Generator[Tuple[bool – The content of the message to be spoken out. If a string is given, a Msg object will be created with the agent’s name, role as “assistant”, and the given string as the content. If the content is a Generator, the agent will speak out the message chunk by chunk.

  • str] – The content of the message to be spoken out. If a string is given, a Msg object will be created with the agent’s name, role as “assistant”, and the given string as the content. If the content is a Generator, the agent will speak out the message chunk by chunk.

  • None – The content of the message to be spoken out. If a string is given, a Msg object will be created with the agent’s name, role as “assistant”, and the given string as the content. If the content is a Generator, the agent will speak out the message chunk by chunk.

  • None]]`) – The content of the message to be spoken out. If a string is given, a Msg object will be created with the agent’s name, role as “assistant”, and the given string as the content. If the content is a Generator, the agent will speak out the message chunk by chunk.

observe(x: Msg | Sequence[Msg]) None[源代码]

Observe the input, store it in memory without response to it.

参数:

x (Union[Msg, Sequence[Msg]]) – The input message to be recorded in memory.

reset_audience(audience: Sequence[AgentBase]) None[源代码]

Set the audience of this agent, which means if this agent generates a response, it will be passed to all audiences.

参数:

audience (Sequence[AgentBase]) – The audience of this agent, which will be notified when this agent generates a response message.

clear_audience() None[源代码]

Remove the audience of this agent.

rm_audience(audience: Sequence[AgentBase] | AgentBase) None[源代码]

Remove the given audience from the Sequence

to_dist(host: str = 'localhost', port: int | None = None, max_pool_size: int = 8192, max_expire_time: int = 7200, max_timeout_seconds: int = 5, local_mode: bool = True, retry_strategy: ~agentscope.rpc.retry_strategy.RetryBase = <agentscope.rpc.retry_strategy.RetryFixedTimes object>) Any

Convert current object into its distributed version.

参数:
  • host (str, defaults to “localhost”) – Hostname of the rpc agent server.

  • port (int, defaults to None) – Port of the rpc agent server.

  • max_pool_size (int, defaults to 8192) – Only takes effect when host and port are not filled in. The max number of agent reply messages that the started agent server can accommodate. Note that the oldest message will be deleted after exceeding the pool size.

  • max_expire_time (int, defaults to 7200) – Only takes effect when host and port are not filled in. Maximum time for reply messages to be cached in the launched agent server. Note that expired messages will be deleted.

  • max_timeout_seconds (int, defaults to 5) – Max timeout seconds for the rpc call.

  • local_mode (bool, defaults to True) – Only takes effect when host and port are not filled in. Whether the started agent server only listens to local requests.

  • retry_strategy (RetryBase, defaults to _DEAFULT_RETRY_STRATEGY) – The retry strategy for the async rpc call.

返回:

the wrapped agent instance with distributed functionality

返回类型:

RpcObject

property agent_id: str

The unique id of this agent.

返回:

agent_id

返回类型:

str