agentscope.agents.react_agent module

An agent class that implements the ReAct algorithm. The agent will reason and act iteratively to solve problems. More details can be found in the paper https://arxiv.org/abs/2210.03629.

class ReActAgent(*args: tuple, **kwargs: dict)[source]

Bases: AgentBase

An agent class that implements the ReAct algorithm. More details refer to https://arxiv.org/abs/2210.03629.

This is an example implementation of ReAct algorithm in AgentScope. We follow the idea within the paper, but the detailed prompt engineering maybe different. Developers are encouraged to modify the prompt to fit their own needs.

Note

1. We use the “thought” field in the response to support Chain-of-Thought, which means the tool functions cannot use “thought” as their argument name. 2. The function name “finish” is also a reserved name when using this agent, which will be used to end the reasoning-acting loop.

static finish(response: str) ServiceResponse[source]

Finish reasoning and generate a response to the user.

Note

The function won’t be executed, actually.

Parameters:

response (str) – The response to the user.

reply(x: Sequence[Msg] | Msg | None = None) Msg[source]

The reply method of the agent.

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.

Parameters:
  • 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.

Returns:

the wrapped agent instance with distributed functionality

Return type:

RpcObject