# Clone the class repository to your local machine
git clone https://github.com/python-react-agent-2025
# Navigate into the project folder
cd python-react-agent-2025# Create a new branch named after you.
git checkout -b your-netid-or-something-unique-to-you
# Example: git checkout -b space-drama-cfreder2This creates a separate branch for your work so it doesn't accidentally interfere with other teams. The branch name often hints at the feature or change you are making.
pip install -r requirements.txtCreate a .env file with your OpenAI API key:
OPENAI_API_KEY="The provided key goes inside the quotes"
Duplicate the chatbot.py and rename it to something unique to you.
In this exercise, we will explore a simple ReAct (Reason + Act) agent implemented as a text-based dungeon escape game. You'll modify the code to solve real problems and extend its functionality.
The ReAct pattern is a fundamental agentic loop:
- OBSERVE - Gather input from the environment
- REASON - Think about what to do next
- ACT - Take action based on reasoning
- Repeat until goal is complete
- Run the game:
python chatbot.py - Play through a few turns to understand how it works
- Notice how OBSERVE, REASON, and ACT are displayed separately
The game never ends! The LLM struggles to return GAMEOVER: FALSE even when the player clearly escapes. This is due to the lack of context; the LLM has no concept of how long the game has been running.
Modify your_chat_bot.py so the game ends after a fixed number of turns (e.g., 5 turns).
We have a few approaches to consider:
Option A: Inform the LLM
- Track the turn count in the program
- Pass the turn information to the
reason()function (e.g., "Turn 4 of 5") - Update the prompt so the LLM knows to wrap up the story
Option B: Program Enforces It
- Add a hard limit in the
run()loop - Force the game to end after max turns regardless of LLM output
Option C: Both
- Combine both approaches for reliability
- Look at the
run()function to see where turns happen - Look at the
reason()function to see what information the LLM receives
In tabletop RPGs, dice rolls determine success or failure. Add a d20 (20-sided die) roll to influence the narrative:
- 1 = Critical Failure - Game Over, you lose!
- 2-10 = Unfavorable outcome
- 11-19 = Favorable outcome
- 20 = Critical Success - Game Over, you win!
import random
# Roll a d20 (random number between 1 and 20)
roll = random.randint(1, 20)
print(f"🎲 You rolled: {roll}")Where should the dice roll go in the ReAct loop?
Think about what each step represents:
- OBSERVE gathers information from the environment
- REASON analyzes and plans
- ACT executes based on reasoning
Which step makes the most sense for rolling the dice? Why?
- Decide where in the ReAct loop the dice roll belongs
- Implement the roll and pass it to the appropriate function
- Update the prompts so the LLM uses the roll to determine outcomes
- A roll of 1 should end the game in failure; a roll of 20 should end the game in victory
After completing the tasks, answer these questions:
-
Which approach did you choose for Task 1 (A, B, or C)? Why?
-
What are the tradeoffs between letting the LLM control the ending vs. the program enforcing it?
-
In a real-world agentic application, when would you want the LLM to have full autonomy, and when would you want programmatic guardrails?
-
How does the ReAct pattern (Observe-Reason-Act) help structure the agent's behavior compared to a single LLM call?
-
(Bonus) Where did you put the dice roll and why? What other "environmental state" could you add to the loop?
Make sure all your changes to your_chat_bot.py are saved.
# Stage your modified file
git add your_chat_bot.py
# Commit with a descriptive message
git commit -m "Added turn limit and dice roll features"# Push your branch to GitHub
git push origin your-branch-name
# Example: git push origin team-alphaThis uploads your branch to GitHub where I can see your work.
After everyone pushes their branches, I will demonstrate how to create a Pull Request on GitHub. A pull request is how teams propose merging their changes into the main codebase - it allows for code review and discussion before changes are accepted.
- Modified
your_chat_bot.pywith turn limit fix - (Bonus) Added dice roll feature
- Committed and pushed to your team branch