File size: 6,733 Bytes
e706de2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# Code Explanation: react-agent.js
This example implements the **ReAct pattern** (Reasoning + Acting), a powerful approach for multi-step problem-solving with tools.
## What is ReAct?
ReAct = **Rea**soning + **Act**ing
The agent alternates between:
1. **Thinking** (reasoning about what to do)
2. **Acting** (using tools)
3. **Observing** (seeing tool results)
4. Repeat until problem is solved
## Key Components
### 1. ReAct System Prompt (Lines 20-52)
```javascript
const systemPrompt = `You are a mathematical assistant that uses the ReAct approach.
CRITICAL: You must follow this EXACT pattern:
Thought: [Explain what calculation you need]
Action: [Call ONE tool]
Observation: [Wait for result]
Thought: [Analyze result]
Action: [Call another tool if needed]
...
Thought: [Once you have all information]
Answer: [Final answer and STOP]
```
**Key instructions:**
- Explicit step-by-step pattern
- One tool call at a time
- Continue until final answer
- Stop after "Answer:"
### 2. Calculator Tools (Lines 60-159)
Four basic math operations:
```javascript
const add = defineChatSessionFunction({...});
const multiply = defineChatSessionFunction({...});
const subtract = defineChatSessionFunction({...});
const divide = defineChatSessionFunction({...});
```
Each tool:
- Takes two numbers (a, b)
- Performs operation
- Logs the call
- Returns result as string
### 3. ReAct Agent Loop (Lines 164-212)
```javascript
async function reactAgent(userPrompt, maxIterations = 10) {
let iteration = 0;
let fullResponse = "";
while (iteration < maxIterations) {
iteration++;
// Prompt the LLM
const response = await session.prompt(
iteration === 1 ? userPrompt : "Continue your reasoning.",
{
functions,
maxTokens: 300,
onTextChunk: (chunk) => {
process.stdout.write(chunk); // Stream output
currentChunk += chunk;
}
}
);
fullResponse += currentChunk;
// Check if final answer reached
if (response.toLowerCase().includes("answer:")) {
return fullResponse;
}
}
}
```
**How it works:**
1. Loop up to maxIterations times
2. On first iteration: send user's question
3. On subsequent iterations: ask to continue
4. Stream output in real-time
5. Stop when "Answer:" appears
6. Return full reasoning trace
### 4. Example Query (Lines 215-220)
```javascript
const queries = [
"A store sells 15 items Monday at $8 each, 20 items Tuesday at $8 each,
10 items Wednesday at $8 each. What's the average items per day and total revenue?"
];
```
Complex problem requiring multiple calculations:
- 15 Γ 8
- 20 Γ 8
- 10 Γ 8
- Sum results
- Calculate average
- Format answer
## The ReAct Flow
### Example Execution
```
USER: "A store sells 15 items at $8 each and 20 items at $8 each. Total revenue?"
Iteration 1:
Thought: First I need to calculate 15 Γ 8
Action: multiply(15, 8)
Observation: 120
Iteration 2:
Thought: Now I need to calculate 20 Γ 8
Action: multiply(20, 8)
Observation: 160
Iteration 3:
Thought: Now I need to add both results
Action: add(120, 160)
Observation: 280
Iteration 4:
Thought: I have the total revenue
Answer: The total revenue is $280
```
**Loop stops** because "Answer:" was detected.
## Why ReAct Works
### Traditional Approach (Fails)
```
User: "Complex math problem"
LLM: [Tries to calculate in head]
β Often wrong due to arithmetic errors
```
### ReAct Approach (Succeeds)
```
User: "Complex math problem"
LLM: "I need to calculate X"
β Calls calculator tool
β Gets accurate result
β Uses result for next step
β Continues until solved
```
## Key Concepts
### 1. Explicit Reasoning
The agent must "show its work":
```
Thought: What do I need to do?
Action: Do it
Observation: What happened?
```
### 2. Tool Use at Each Step
```
Don't calculate: 15 Γ 8 = 120 (may be wrong)
Do calculate: multiply(15, 8) β 120 (always correct)
```
### 3. Iterative Problem Solving
```
Complex Problem β Break into steps β Solve each step β Combine results
```
### 4. Self-Correction
Agent can observe bad results and try again:
```
Thought: That doesn't look right
Action: Let me recalculate
```
## Debug Output
The code includes PromptDebugger (lines 228-234):
```javascript
const promptDebugger = new PromptDebugger({
outputDir: './logs',
filename: 'react_calculator.txt',
includeTimestamp: true
});
await promptDebugger.debugContextState({session, model});
```
Saves complete prompt history to logs for debugging.
## Expected Output
```
========================================================
USER QUESTION: [Problem statement]
========================================================
--- Iteration 1 ---
Thought: First I need to multiply 15 by 8
Action: multiply(15, 8)
π§ TOOL CALLED: multiply(15, 8)
π RESULT: 120
Observation: 120
--- Iteration 2 ---
Thought: Now I need to multiply 20 by 8
Action: multiply(20, 8)
π§ TOOL CALLED: multiply(20, 8)
π RESULT: 160
... continues ...
--- Iteration N ---
Thought: I have all the information
Answer: [Final answer]
========================================================
FINAL ANSWER REACHED
========================================================
```
## Why This Matters
### Enables Complex Tasks
- Multi-step reasoning
- Accurate calculations
- Self-correction
- Transparent process
### Foundation of Modern Agents
This pattern powers:
- LangChain agents
- AutoGPT
- BabyAGI
- Most production agent frameworks
### Observable Reasoning
Unlike "black box" LLMs, you see:
- What the agent is thinking
- Which tools it uses
- Why it makes decisions
- Where it might fail
## Best Practices
1. **Clear system prompt**: Define exact pattern
2. **One tool per action**: Don't combine operations
3. **Limit iterations**: Prevent infinite loops
4. **Stream output**: Show progress
5. **Debug thoroughly**: Use PromptDebugger
## Comparison
```
Simple Agent vs ReAct Agent
ββββββββββββββββββββββββββββ
Single prompt/response Multi-step iteration
One tool call (maybe) Multiple tool calls
No visible reasoning Explicit reasoning
Works for simple tasks Handles complex problems
```
This is the state-of-the-art pattern for building capable AI agents!
|