DSPy: Chain-of-Thought vs ReAct
This example demonstrates how to implement the same "math tutor" task with two different prompting strategies in DSPy:
- A Chain-of-Thought (CoT) module that reasons step by step.
- A ReAct agent that can call a calculator tool while reasoning.
Requirements
- Python 3.6+
- An OpenAI API key
Steps for running this example in the shell
- Install dependencies:
bash
python -m venv .venv
# macOS/Linux:
source .venv/bin/activate
# Windows Command Prompt:
.venv\Scripts\activate.bat
# Windows PowerShell:
.venv\Scripts\Activate.ps1
pip install -r requirements.txt- Export your OpenAI API key as an environment variable:
bash
export OPENAI_API_KEY="sk-..." # Windows cmd: set OPENAI_API_KEY="sk-..." # Windows PowerShell: $env:OPENAI_API_KEY="sk-..."- Run the script:
bash
python dspy_cot_vs_react.pyOutput
When you run the script, it will execute both the Chain-of-Thought and ReAct strategies and print their results and reasoning/trajectory.