The ReAct pattern
The ReAct pattern enables a language model to reason and act by giving it access to additional actions it can take, such as searching Wikipedia, or running calculations or code. The model is taught to request these actions, and their results are fed back into the model. For more information, see ReAct: Synergizing Reasoning and Acting in Language Models.
To implement the ReAct pattern:
- Define the additional actions that the model can take.
- Implement the logic for running these actions securely.
- Teach the model to request these actions using specific syntax, for example: "wikipedia: <search term>", "calculate: <expression>", or "run: <code>".
- Process the model's output to identify action requests, perform the corresponding actions, and feed the results back into the model.
- Repeat steps 3 to 4 until the model produces a final answer.
The key steps in the ReAct loop are:
- Thought: The model thinks through the problem and decides what information or action is needed next.
- Action: The model performs an action based on its thought, such as searching for information or performing a calculation. When the agent proposes an action, external “tools or functions” are called with the necessary parameters to obtain the relevant results or information regarding the action.
- Observation: The model receives the result of its action, which it then uses to inform its next thought or action.
Example
Suppose we want to find the capital of the country that hosts the Eiffel Tower. Using the ReAct pattern along with external tools such as a Wikipedia article crawler, the agent’s thought process might look like this:
- Agent: To find the capital of the country with the Eiffel Tower, I first need to find out which country the Eiffel Tower is located in.
- Thought: I need to find out which country the Eiffel Tower is located in.
- Action: wikipedia: Eiffel Tower # (for example, here we call a function/tool to retrieve the article and extract relevant information from the article)
- Observation: The Eiffel Tower is located in Paris, France. # (extract relevant article content and present to agent as an observation)
- Agent: Now that I know the Eiffel Tower is in France, I can look up the capital of France.
- Thought: I need to find the capital of France.
- Action: wikipedia: France
- Observation: The capital city of France is Paris.
- Agent: The capital of the country that hosts the Eiffel Tower is Paris.