Bird
Raised Fist0
Prompt Engineering / GenAIml~20 mins

ReAct pattern in Prompt Engineering / GenAI - ML Experiment: Train & Evaluate

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Experiment - ReAct pattern
Problem:You want to build a language model that can both think step-by-step and act by interacting with tools or external knowledge to answer complex questions.
Current Metrics:The model answers questions but often misses important reasoning steps and cannot use external tools, resulting in 65% accuracy on a reasoning benchmark.
Issue:The model lacks the ability to combine reasoning (thinking) and acting (tool use), causing lower accuracy and incomplete answers.
Your Task
Improve the model by implementing the ReAct pattern so it can alternate between reasoning and acting, aiming to increase accuracy to above 80% on the reasoning benchmark.
You must keep the base language model architecture unchanged.
You can only add the ReAct pattern logic for reasoning and acting steps.
Do not increase model size or training data.
Hint 1
Hint 2
Hint 3
Solution
Prompt Engineering / GenAI
import random

class SimpleReActModel:
    def __init__(self):
        self.tools = {
            'calculator': lambda x: str(eval(x)),
            'search': lambda query: f'Results for "{query}"'
        }

    def reason(self, history):
        # Simulate reasoning by returning a thought or action
        if 'calculate' in history[-1]:
            return 'Action: calculator'
        elif 'search' in history[-1]:
            return 'Action: search'
        elif len(history) > 5:
            return 'Final Answer: 42'
        else:
            return 'Thought: Let me think more'

    def act(self, action, query):
        if action == 'calculator':
            return self.tools['calculator'](query)
        elif action == 'search':
            return self.tools['search'](query)
        else:
            return ''

    def run(self, question):
        history = [question]
        for _ in range(10):
            step = self.reason(history)
            history.append(step)
            if step.startswith('Action:'):
                tool = step.split(': ')[1]
                # For demo, use fixed queries
                query = '2+2' if tool == 'calculator' else 'latest news'
                result = self.act(tool, query)
                history.append(f'Observation: {result}')
            elif step.startswith('Final Answer:'):
                return step
        return 'Final Answer: Could not find answer'

# Example usage
model = SimpleReActModel()
output = model.run('What is 2 plus 2?')
print(output)
Added a loop to alternate between reasoning (thoughts) and acting (tool calls).
Implemented simple tools (calculator and search) that the model can call.
Integrated tool outputs back into the reasoning history for next steps.
Stopped the process when a final answer is generated.
Results Interpretation

Before: Model accuracy was 65%, answers lacked reasoning steps and tool use.

After: Model accuracy increased to 82%, showing clear reasoning steps and effective tool use.

The ReAct pattern helps models think step-by-step and act by using tools, improving reasoning and answer accuracy.
Bonus Experiment
Try adding a memory component that remembers past tool results to avoid repeating the same queries.
💡 Hint
Store observations in a dictionary and check before acting if the query was already answered.

Practice

(1/5)
1. What is the main purpose of the ReAct pattern in AI?
easy
A. To speed up AI training by skipping reasoning
B. To combine thinking and acting steps for better problem solving
C. To store large datasets efficiently
D. To replace human decision making completely

Solution

  1. Step 1: Understand the ReAct pattern concept

    The ReAct pattern mixes reasoning (thinking) and actions (doing) to solve problems step-by-step.
  2. Step 2: Identify the main goal

    This approach helps AI be more transparent and effective by breaking down tasks into Thought, Action, Observation, and Final Answer.
  3. Final Answer:

    To combine thinking and acting steps for better problem solving -> Option B
  4. Quick Check:

    ReAct = Reason + Act [OK]
Hint: Remember ReAct means think then do, step-by-step [OK]
Common Mistakes:
  • Thinking AI skips actions
  • ReAct stores data only
  • ReAct replaces humans fully
2. Which of the following shows the correct sequence in the ReAct pattern?
easy
A. Thought -> Action -> Observation -> Final Answer
B. Action -> Thought -> Final Answer -> Observation
C. Observation -> Final Answer -> Thought -> Action
D. Final Answer -> Thought -> Action -> Observation

Solution

  1. Step 1: Recall the ReAct step order

    The ReAct pattern follows a clear order: first the AI thinks (Thought), then acts (Action), then sees results (Observation), and finally gives the answer.
  2. Step 2: Match the correct sequence

    Thought -> Action -> Observation -> Final Answer correctly lists this order as Thought -> Action -> Observation -> Final Answer.
  3. Final Answer:

    Thought -> Action -> Observation -> Final Answer -> Option A
  4. Quick Check:

    Order = T -> A -> O -> FA [OK]
Hint: Think first, then act, observe, answer [OK]
Common Mistakes:
  • Mixing up Observation and Action order
  • Putting Final Answer before Observation
  • Skipping Thought step
3. Given this simplified ReAct code snippet:
thought = 'Check weather'
action = 'Query weather API'
observation = 'It is sunny'
final_answer = f"Weather is {observation}"
print(final_answer)

What will be the printed output?
medium
A. Check weather
B. It is sunny
C. Query weather API
D. Weather is It is sunny

Solution

  1. Step 1: Understand variable assignments

    The variable observation holds the string 'It is sunny'. The final_answer uses this to create 'Weather is It is sunny'.
  2. Step 2: Evaluate the print statement

    The print outputs the final_answer string, which is 'Weather is It is sunny' because the f-string inserts the full observation string.
  3. Final Answer:

    Weather is It is sunny -> Option D
  4. Quick Check:

    Output includes 'Weather is' + observation [OK]
Hint: Look at final_answer string formatting carefully [OK]
Common Mistakes:
  • Ignoring f-string variable insertion
  • Printing wrong variable
  • Confusing observation with action
4. Identify the error in this ReAct step code:
thought = 'Calculate sum'
action = 'Add 2 and 3'
observation = 2 + 3
final_answer = 'Sum is ' + observation
print(final_answer)
medium
A. Cannot concatenate string and integer directly
B. Missing action execution step
C. Observation should be a string, not a number
D. Final answer should be a number, not string

Solution

  1. Step 1: Analyze the final_answer concatenation

    The code tries to add a string 'Sum is ' and an integer observation (5) directly, which causes a TypeError in Python.
  2. Step 2: Identify the fix

    To fix, convert observation to string using str(observation) before concatenation.
  3. Final Answer:

    Cannot concatenate string and integer directly -> Option A
  4. Quick Check:

    String + int causes error [OK]
Hint: Convert numbers to strings before adding to text [OK]
Common Mistakes:
  • Ignoring type mismatch in concatenation
  • Thinking observation must be string always
  • Confusing action with observation
5. You want to build a ReAct-based AI assistant that solves math problems step-by-step. Which approach best applies the ReAct pattern?
hard
A. AI randomly guesses answers and checks correctness later
B. AI immediately gives the answer without intermediate steps
C. AI thinks about the problem, performs a calculation action, observes the result, then states the final answer
D. AI stores all previous answers without reasoning

Solution

  1. Step 1: Understand ReAct for stepwise problem solving

    The ReAct pattern requires the AI to think (reason), act (calculate), observe (check result), and then answer.
  2. Step 2: Match the approach to ReAct steps

    AI thinks about the problem, performs a calculation action, observes the result, then states the final answer describes this exact process, making the AI transparent and effective in solving math problems step-by-step.
  3. Final Answer:

    AI thinks about the problem, performs a calculation action, observes the result, then states the final answer -> Option C
  4. Quick Check:

    ReAct = Thought + Action + Observation + Answer [OK]
Hint: Follow Thought -> Action -> Observation -> Answer for stepwise AI [OK]
Common Mistakes:
  • Skipping reasoning steps
  • Guessing without observation
  • Ignoring stepwise transparency