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 agentscope.agents.react_agent.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.

__init__(name: str, model_config_name: str, service_toolkit: ServiceToolkit, sys_prompt: str = "You're a helpful assistant named {name}.", max_iters: int = 10, verbose: bool = True) None[source]

Initialize the ReAct agent with the given name, model config name and tools.

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

  • sys_prompt (str) – The system prompt of the agent.

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

  • service_toolkit (ServiceToolkit) – A ServiceToolkit object that contains the tool functions.

  • max_iters (int, defaults to 10) – The maximum number of iterations of the reasoning-acting loops.

  • verbose (bool, defaults to True) – Whether to print the detailed information during reasoning and acting steps. If False, only the content in speak field will be print out.

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

The reply method of the agent.

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.