This repository provides a minimal yet complete implementation of a ReAct (Reasoning + Acting) agent with observability and a lightweight visualization, supporting Gemini by default and optionally Kimi K2 via an OpenAI-compatible interface.
- Step-by-step implementation of the ReAct pattern
- Multiple examples showcasing ReAct agents in action
- Optimizations specific to the Gemini model
- Tools integration (Google Search and Wikipedia)
- Python 3.10+
- Git
- Poetry (for dependency management)
-
Clone the repository:
git clone https://github.com/syccxdr/ReAct-Agent-demo.git cd react-from-scratch -
Set up a virtual environment (venv or conda):
# venv example python -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate # or conda example # conda create -n react-agent python=3.11 -y # conda activate react-agent -
Install Poetry (if not already installed):
pip install poetry -
Install project dependencies:
poetry install -
Set up environment variables:
export PYTHONDONTWRITEBYTECODE=1 export PYTHONPATH=$PYTHONPATH:.
-
Create a
credentialsfolder in the project root:mkdir credentials -
Set up GCP service account credentials (if using Gemini):
- Go to the Google Cloud Console (https://console.cloud.google.com/).
- Create a new project or select an existing one.
- Navigate to "APIs & Services" > "Credentials".
- Click "Create Credentials" > "Service Account Key".
- Select your service account, choose JSON as the key type, and click "Create".
- Save the downloaded JSON file as
key.jsonin thecredentialsfolder.
-
Set up SERP API credentials:
- Sign up for a SERP API account at https://serpapi.com/.
- Obtain your API key from the dashboard.
- Create a file named
key.ymlin thecredentialsfolder. - Add your SERP API token in the following format:
serp: key: your_serp_api_key_here
Note: The credentials folder is included in .gitignore to prevent sensitive information from being committed.
src/tools/: Contains implementations for Google Search (via SERP API) and Wikipedia search.src/react/: Houses the core ReAct agent implementation.data/input/: Stores input prompts for the ReAct agent.data/output/: Contains output traces from example runs.
-
Ensure you're in the project root directory with your virtual environment activated.
-
Configure environment (choose Gemini or Kimi):
- Gemini (Vertex AI):
export GOOGLE_APPLICATION_CREDENTIALS=/absolute/path/to/key.json # model name comes from config/config.yml -> model_name
- Kimi K2 (OpenAI-compatible Chat Completions):
export PROVIDER=kimi export KIMI_API_KEY=YOUR_KIMI_API_KEY export KIMI_BASE_URL=https://api.moonshot.cn/v1 export KIMI_MODEL=kimi-k2-0905-preview
- Gemini (Vertex AI):
-
Run the ReAct agent:
python -m src.react.agent -
The agent uses the prompt from
./data/input/react.txtand generates output traces in./data/output/. -
To run individual tools:
- Google Search:
python src/tools/serp.py - Wikipedia Search:
python src/tools/wiki.py
- Google Search:
-
For a non-agentic approach with programmatic routing:
-
Ensure you exported the Kimi env vars as above.
-
Run the agent once to produce traces:
python src/react/agent.py- Start the Streamlit trace viewer:
python -m streamlit run app/trace_viewer.pyExpected:
-
data/output/trace.jsonlcontains events: think/decide/act/final/stats, withapi_calls/token_in/token_outpopulated for Kimi responses.python src/tools/manager.py
This project is licensed under the MIT License. See the LICENSE file for details.
For a detailed explanation of the ReAct pattern and this implementation, check out this accompanying Medium article: [Building ReAct Agents from Scratch: A Hands-On Guide using Gemini]
For a Agent Visualization, check out this accompanying Medium article: [Agent Visualization]

