|
| 1 | + |
1 | 2 | # Thought: Internal Reasoning and the ReAct Approach
|
2 | 3 |
|
3 |
| -<Tip> |
4 |
| -In this section, we dive into the inner workings of an AI agent—its ability to reason and plan. We’ll explore how the agent leverages its internal dialogue to analyze information, break down complex problems into manageable steps, and decide what action to take next. Additionally, we introduce the ReAct approach, a prompting technique that encourages the model to think “step by step” before acting. |
| 4 | +<Tip> |
| 5 | + |
| 6 | +In this section, we dive into the inner workings of an AI agent—its ability to reason and plan. We’ll explore how the agent leverages its internal dialogue to analyze information, break down complex problems into manageable steps, and decide what action to take next. |
| 7 | + |
| 8 | +Additionally, we introduce the ReAct approach, a prompting technique that encourages the model to think “step by step” before acting. |
| 9 | + |
5 | 10 | </Tip>
|
6 | 11 |
|
7 | 12 | Thoughts represent the **Agent's internal reasoning and planning processes** to solve the task.
|
8 | 13 |
|
9 |
| -This utilises the agent's Large Language Model (LLM) capacity **to analyze information when presented in its prompt**. |
| 14 | +This utilises the agent's Large Language Model (LLM) capacity **to analyze information when presented in its prompt** — essentially, its inner monologue as it works through a problem. |
10 | 15 |
|
11 |
| -Think of it as the agent's internal dialogue, where it considers the task at hand and strategizes its approach. |
| 16 | +The Agent's thoughts help it assess current observations and decide what the next action(s) should be. Through this process, the agent can **break down complex problems into smaller, more manageable steps**, reflect on past experiences, and continuously adjust its plans based on new information. |
12 | 17 |
|
13 |
| -The Agent's thoughts are responsible for assessing current observations and decide what the next action(s) should be. |
14 | 18 |
|
15 |
| -Through this process, the agent can **break down complex problems into smaller, more manageable steps**, reflect on past experiences, and continuously adjust its plans based on new information. |
| 19 | +## 🧠 Examples of Common Thought Types |
16 | 20 |
|
17 |
| -Here are some examples of common thoughts: |
18 |
| - |
19 |
| -| Type of Thought | Example | |
20 |
| -|----------------|---------| |
21 |
| -| Planning | "I need to break this task into three steps: 1) gather data, 2) analyze trends, 3) generate report" | |
22 |
| -| Analysis | "Based on the error message, the issue appears to be with the database connection parameters" | |
23 |
| -| Decision Making | "Given the user's budget constraints, I should recommend the mid-tier option" | |
24 |
| -| Problem Solving | "To optimize this code, I should first profile it to identify bottlenecks" | |
| 21 | +| Type of Thought | Example | |
| 22 | +|--------------------|-------------------------------------------------------------------------| |
| 23 | +| Planning | "I need to break this task into three steps: 1) gather data, 2) analyze trends, 3) generate report" | |
| 24 | +| Analysis | "Based on the error message, the issue appears to be with the database connection parameters" | |
| 25 | +| Decision Making | "Given the user's budget constraints, I should recommend the mid-tier option" | |
| 26 | +| Problem Solving | "To optimize this code, I should first profile it to identify bottlenecks" | |
25 | 27 | | Memory Integration | "The user mentioned their preference for Python earlier, so I'll provide examples in Python" |
|
26 |
| -| Self-Reflection | "My last approach didn't work well, I should try a different strategy" | |
27 |
| -| Goal Setting | "To complete this task, I need to first establish the acceptance criteria" | |
28 |
| -| Prioritization | "The security vulnerability should be addressed before adding new features" | |
| 28 | +| Self-Reflection | "My last approach didn't work well, I should try a different strategy" | |
| 29 | +| Goal Setting | "To complete this task, I need to first establish the acceptance criteria" | |
| 30 | +| Prioritization | "The security vulnerability should be addressed before adding new features" | |
| 31 | + |
| 32 | +> **Note:** In the case of LLMs fine-tuned for function-calling, the thought process is optional. More details will be covered in the Actions section. |
| 33 | +
|
| 34 | + |
| 35 | +## 🔗 Chain-of-Thought (CoT) |
| 36 | + |
| 37 | +**Chain-of-Thought (CoT)** is a prompting technique that guides a model to **think through a problem step-by-step before producing a final answer.** |
29 | 38 |
|
30 |
| -> **Note:** In the case of LLMs fine-tuned for function-calling, the thought process is optional. |
31 |
| -> *In case you're not familiar with function-calling, there will be more details in the Actions section.* |
| 39 | +It typically starts with: |
| 40 | +> *"Let's think step by step."* |
32 | 41 |
|
33 |
| -## The ReAct Approach |
| 42 | +This approach helps the model **reason internally**, especially for logical or mathematical tasks, **without interacting with external tools**. |
34 | 43 |
|
35 |
| -A key method is the **ReAct approach**, which is the concatenation of "Reasoning" (Think) with "Acting" (Act). |
| 44 | +### ✅ Example (CoT) |
| 45 | +``` |
| 46 | +Question: What is 15% of 200? |
| 47 | +Thought: Let's think step by step. 10% of 200 is 20, and 5% of 200 is 10, so 15% is 30. |
| 48 | +Answer: 30 |
| 49 | +``` |
36 | 50 |
|
37 |
| -ReAct is a simple prompting technique that appends "Let's think step by step" before letting the LLM decode the next tokens. |
38 | 51 |
|
39 |
| -Indeed, prompting the model to think "step by step" encourages the decoding process toward next tokens **that generate a plan**, rather than a final solution, since the model is encouraged to **decompose** the problem into *sub-tasks*. |
| 52 | +## ⚙️ ReAct: Reasoning + Acting |
40 | 53 |
|
41 |
| -This allows the model to consider sub-steps in more detail, which in general leads to less errors than trying to generate the final solution directly. |
| 54 | +A key method is the **ReAct approach**, which combines "Reasoning" (Think) with "Acting" (Act). |
| 55 | + |
| 56 | +ReAct is a prompting technique that encourages the model to think step-by-step and interleave actions (like using tools) between reasoning steps. |
| 57 | + |
| 58 | +This enables the agent to solve complex multi-step tasks by alternating between: |
| 59 | +- Thought: internal reasoning |
| 60 | +- Action: tool usage |
| 61 | +- Observation: receiving tool output |
| 62 | + |
| 63 | +### 🔄 Example (ReAct) |
| 64 | +``` |
| 65 | +Thought: I need to find the latest weather in Paris. |
| 66 | +Action: Search["weather in Paris"] |
| 67 | +Observation: It's 18°C and cloudy. |
| 68 | +Thought: Now that I know the weather... |
| 69 | +Action: Finish["It's 18°C and cloudy in Paris."] |
| 70 | +``` |
42 | 71 |
|
43 | 72 | <figure>
|
44 |
| -<img src="https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit1/ReAct.png" alt="ReAct"/> |
45 |
| -<figcaption>The (d) is an example of ReAct approach where we prompt "Let's think step by step" |
46 |
| -</figcaption> |
| 73 | + <img src="https://huggingface.co/datasets/agents-course/course-images/resolve/main/en/unit1/ReAct.png" alt="ReAct"/> |
| 74 | + <figcaption> |
| 75 | + (d) is an example of the ReAct approach, where we prompt "Let's think step by step", and the model acts between thoughts. |
| 76 | + </figcaption> |
47 | 77 | </figure>
|
48 | 78 |
|
| 79 | + |
| 80 | +## 🔁 Comparison: ReAct vs. CoT |
| 81 | + |
| 82 | +| Feature | Chain-of-Thought (CoT) | ReAct | |
| 83 | +|----------------------|-----------------------------|-------------------------------------| |
| 84 | +| Step-by-step logic | ✅ Yes | ✅ Yes | |
| 85 | +| External tools | ❌ No | ✅ Yes (Actions + Observations) | |
| 86 | +| Best suited for | Logic, math, internal tasks | Info-seeking, dynamic multi-step tasks | |
| 87 | + |
49 | 88 | <Tip>
|
50 |
| -We have recently seen a lot of interest for reasoning strategies. This is what's behind models like Deepseek R1 or OpenAI's o1, which have been fine-tuned to "think before answering". |
51 | 89 |
|
52 |
| -These models have been trained to always include specific _thinking_ sections (enclosed between `<think>` and `</think>` special tokens). This is not just a prompting technique like ReAct, but a training method where the model learns to generate these sections after analyzing thousands of examples that show what we expect it to do. |
53 |
| -</Tip> |
| 90 | +Recent models like **Deepseek R1** or **OpenAI’s o1** were fine-tuned to *think before answering*. They use structured tokens like `<think>` and `</think>` to explicitly separate the reasoning phase from the final answer. |
54 | 91 |
|
55 |
| ---- |
56 |
| -Now that we better understand the Thought process, let's go deeper on the second part of the process: Act. |
| 92 | +Unlike ReAct or CoT — which are prompting strategies — this is a **training-level technique**, where the model learns to think via examples. |
| 93 | + |
| 94 | +</Tip> |
0 commit comments