Skip to content

cfreder2/python-react-agent-2025

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ReAct Agent

Setup Instructions

1. Clone the Repository

# 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

2. Create Your Team Branch

# Create a new branch named after you.
git checkout -b your-netid-or-something-unique-to-you

# Example: git checkout -b space-drama-cfreder2

This 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.

3. Install Dependencies

pip install -r requirements.txt

4. Set Up Your API Key

Create a .env file with your OpenAI API key:

OPENAI_API_KEY="The provided key goes inside the quotes"

5. Copy the file

Duplicate the chatbot.py and rename it to something unique to you.


Overview

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

The ReAct pattern is a fundamental agentic loop:

  1. OBSERVE - Gather input from the environment
  2. REASON - Think about what to do next
  3. ACT - Take action based on reasoning
  4. Repeat until goal is complete

Getting Started

  1. Run the game: python chatbot.py
  2. Play through a few turns to understand how it works
  3. Notice how OBSERVE, REASON, and ACT are displayed separately

Task 1: Fix the Turn Limit

The Problem

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.

Your Task

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

Hints

  • Look at the run() function to see where turns happen
  • Look at the reason() function to see what information the LLM receives

Task 2: Add a D20 Dice Roll (Bonus)

The Idea

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!

Code Snippet

import random

# Roll a d20 (random number between 1 and 20)
roll = random.randint(1, 20)
print(f"🎲 You rolled: {roll}")

Key Question

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?

Your Task

  1. Decide where in the ReAct loop the dice roll belongs
  2. Implement the roll and pass it to the appropriate function
  3. Update the prompts so the LLM uses the roll to determine outcomes
  4. A roll of 1 should end the game in failure; a roll of 20 should end the game in victory

Reflection Questions

After completing the tasks, answer these questions:

  1. Which approach did you choose for Task 1 (A, B, or C)? Why?

  2. What are the tradeoffs between letting the LLM control the ending vs. the program enforcing it?

  3. In a real-world agentic application, when would you want the LLM to have full autonomy, and when would you want programmatic guardrails?

  4. How does the ReAct pattern (Observe-Reason-Act) help structure the agent's behavior compared to a single LLM call?

  5. (Bonus) Where did you put the dice roll and why? What other "environmental state" could you add to the loop?


Submission Instructions

1. Save Your Work

Make sure all your changes to your_chat_bot.py are saved.

2. Stage and Commit Your Changes

# 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"

3. Push Your Branch to the Remote Repository

# Push your branch to GitHub
git push origin your-branch-name

# Example: git push origin team-alpha

This uploads your branch to GitHub where I can see your work.

4. What Happens Next

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.


Submission Checklist

  • Modified your_chat_bot.py with turn limit fix
  • (Bonus) Added dice roll feature
  • Committed and pushed to your team branch

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages