react

Module Contents

Classes

ReActProtocol

A wrapper of ReAct prompt which manages the response from LLM and

ReAct

An implementation of ReAct (https://arxiv.org/abs/2210.03629)

Attributes

CALL_PROTOCOL_CN

FORCE_STOP_PROMPT_CN

CALL_PROTOCOL_EN

FORCE_STOP_PROMPT_EN

lagent.agents.react.CALL_PROTOCOL_CN = Multiline-String
Show Value
"""你是一个可以调用外部工具的助手,可以使用的工具包括:
{tool_description}
如果使用工具请遵循以下格式回复:
```
{thought}思考你当前步骤需要解决什么问题,是否需要使用工具
{action}工具名称,你的工具必须从 [{action_names}] 选择
{action_input}工具输入参数
```
工具返回按照以下格式回复:
```
{response}调用工具后的结果
```
如果你已经知道了答案,或者你不需要工具,请遵循以下格式回复
```
{thought}给出最终答案的思考过程
{finish}最终答案
```
开始!"""
lagent.agents.react.FORCE_STOP_PROMPT_CN = '你需要基于历史消息返回一个最终结果'
lagent.agents.react.CALL_PROTOCOL_EN = Multiline-String
Show Value
"""You are a assistant who can utilize external tools.
{tool_description}
To use a tool, please use the following format:
```
{thought}Think what you need to solve, do you need to use tools?
{action}the tool name, should be one of [{action_names}]
{action_input}the input to the action
```
The response after utilizing tools should using the following format:
```
{response}the results after call the tool.
```
If you already know the answer, or you do not need to use tools,
please using the following format to reply:
```
{thought}the thought process to get the final answer
{finish}final answer
```
Begin!"""
lagent.agents.react.FORCE_STOP_PROMPT_EN = Multiline-String
Show Value
"""You should directly give results
 based on history information."""
class lagent.agents.react.ReActProtocol(thought=dict(role='THOUGHT', begin='Thought:', end='\n', belong='assistant'), action=dict(role='ACTION', begin='Action:', end='\n'), action_input=dict(role='ARGS', begin='Action Input:', end='\n'), response=dict(role='RESPONSE', begin='Response:', end='\n'), finish=dict(role='FINISH', begin='Final Answer:', end='\n'), call_protocol=CALL_PROTOCOL_EN, force_stop=FORCE_STOP_PROMPT_EN)

A wrapper of ReAct prompt which manages the response from LLM and generate desired prompts in a ReAct format.

Parameters:
  • thought (dict) – the information of thought pattern

  • action (dict) – the information of action pattern

  • action_input (dict) – the information of action_input pattern

  • response (dict) – the information of response pattern

  • finish (dict) – the information of finish pattern

  • call_protocol (str) – the format of ReAct

  • force_stop (str) – the prompt to force LLM to generate response

format(chat_history, inner_step, action_executor, force_stop=False)

Generate the ReAct format prompt.

Parameters:
  • chat_history (List[Dict]) – The history log in previous runs.

  • inner_step (List[Dict]) – The log in the current run.

  • action_executor (ActionExecutor) – the action manager to execute actions.

  • force_stop (boolean) – whether force the agent to give responses under pre-defined turns.

Returns:

ReAct format prompt.

Return type:

List[Dict]

parse(message, action_executor)

Parse the action returns in a ReAct format.

Parameters:
  • message (str) – The response from LLM with ReAct format.

  • action_executor (ActionExecutor) – Action executor to provide no_action/finish_action name.

Returns:

the return value is a tuple contains:
  • thought (str): contain LLM thought of the current step.

  • action (str): contain action scheduled by LLM.

  • action_input (str): contain the required action input

    for current action.

Return type:

tuple

format_response(action_return)

format the final response at current step.

Parameters:

action_return (ActionReturn) – return value of the current action.

Returns:

the final response at current step.

Return type:

dict

class lagent.agents.react.ReAct(llm, action_executor, protocol=ReActProtocol(), max_turn=4)

Bases: lagent.agents.base_agent.BaseAgent

An implementation of ReAct (https://arxiv.org/abs/2210.03629)

Parameters:
  • llm (BaseModel or BaseAPIModel) – a LLM service which can chat and act as backend.

  • action_executor (ActionExecutor) – an action executor to manage all actions and their response.

  • protocol (ReActProtocol) – a wrapper to generate prompt and parse the response from LLM / actions.

  • max_turn (int) – the maximum number of trails for LLM to generate plans that can be successfully parsed by ReAct protocol. Defaults to 4.

chat(message, **kwargs)
Parameters:

message (Union[str, dict, List[dict]]) –

Return type:

lagent.schema.AgentReturn