代理會夢見電子羊嗎?ReAct 架構

AI Agents 入門

Adel Nehme

VP of AI Curriculum, DataCamp

模型思維的不同類型

1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
AI Agents 入門

模型思維的不同類型

Thought Example
Planning 「為了幫他們搬家,我需要找搬家公司、比較價格,並確認他們日期的可用性」
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
AI Agents 入門

模型思維的不同類型

Thought Example
Planning 「為了幫他們搬家,我需要找搬家公司、比較價格,並確認他們日期的可用性」
Analysis 「看他們的消費模式,他們為很少使用的訂閱付了太多錢」
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
AI Agents 入門

模型思維的不同類型

Thought Example
Planning 「為了幫他們搬家,我需要找搬家公司、比較價格,並確認他們日期的可用性」
Analysis 「看他們的消費模式,他們為很少使用的訂閱付了太多錢」
Decision-making 「因為他們明天就需要,我應該建議選快遞運送,雖然會多一些費用」
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
AI Agents 入門

模型思維的不同類型

Thought Example
Planning 「為了幫他們搬家,我需要找搬家公司、比較價格,並確認他們日期的可用性」
Analysis 「看他們的消費模式,他們為很少使用的訂閱付了太多錢」
Decision-making 「因為他們明天就需要,我應該建議選快遞運送,雖然會多一些費用」
Problem-solving 「要最佳化這段程式碼,我應先進行 profiling 找出瓶頸」
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
AI Agents 入門

模型思維的不同類型

Thought Example
Memory integration 「他們上週提到乳糖不耐,所以我會把乳製品排除在這些食譜建議之外」
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
AI Agents 入門

模型思維的不同類型

Thought Example
Memory integration 「他們上週提到乳糖不耐,所以我會把乳製品排除在這些食譜建議之外」
Self-reflection 「我剛剛的說明太技術了——我來用日常比喻把它簡化」
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
AI Agents 入門

模型思維的不同類型

Thought Example
Memory integration 「他們上週提到乳糖不耐,所以我會把乳製品排除在這些食譜建議之外」
Self-reflection 「我剛剛的說明太技術了——我來用日常比喻把它簡化」
Goal-setting 「在規劃他們的運動菜單之前,我需要先了解他們的體能程度與可用時間」
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
AI Agents 入門

模型思維的不同類型

Thought Example
Memory integration 「他們上週提到乳糖不耐,所以我會把乳製品排除在這些食譜建議之外」
Self-reflection 「我剛剛的說明太技術了——我來用日常比喻把它簡化」
Goal-setting 「在規劃他們的運動菜單之前,我需要先了解他們的體能程度與可用時間」
Prioritization 「應該先訂機票再訂飯店,因為機票漲得比較快」
1 The Hugging Face Agents Course: https://github.com/huggingface/agents-course
AI Agents 入門

ReAct 架構:思維從何而來

react

1 Yao, S., Zhao, J., Yu, D., Du, N., Shafran, I., Narasimhan, K., Cao, Y. (2022). ReAct: Synergizing Reasoning and Acting in Language Models. arXiv preprint arXiv:2210.03629.
AI Agents 入門

ReAct 架構:推理與行動

Reasoning

Acting

1 Yao, S., Zhao, J., Yu, D., Du, N., Shafran, I., Narasimhan, K., Cao, Y. (2022). ReAct: Synergizing Reasoning and Acting in Language Models. arXiv preprint arXiv:2210.03629.
AI Agents 入門

ReAct 架構:推理與行動

Reasoning

Chain of thought prompting

[Prompt...]。逐步思考。

Acting

1 Yao, S., Zhao, J., Yu, D., Du, N., Shafran, I., Narasimhan, K., Cao, Y. (2022). ReAct: Synergizing Reasoning and Acting in Language Models. arXiv preprint arXiv:2210.03629.
AI Agents 入門

ReAct 架構:推理與行動

Reasoning

Chain of thought prompting

[Prompt...]。逐步思考。

Acting

Thought > Action > Observation 範例

[Prompt...]。請依此格式:

  • Thought: [先想要計算什麼]
  • Action: [執行計算]
  • Observation: [計算結果]
  • ...視需要重複...
  • Final Answer: [完整解答]
1 Yao, S., Zhao, J., Yu, D., Du, N., Shafran, I., Narasimhan, K., Cao, Y. (2022). ReAct: Synergizing Reasoning and Acting in Language Models. arXiv preprint arXiv:2210.03629.
AI Agents 入門

ReAct 實作:簡單算術

gpt-4o

AI Agents 入門

ReAct 實作:簡單算術

$$

The question

若我買 3 台筆電,每台 $899,享 15% 折扣並加 8% 銷售稅,請計算總金額

$$

The correct answer

$2475.85

AI Agents 入門

ReAct 實作:簡單算術

incorrect

使用的 Prompt

若我買 3 台筆電,每台 $899,享 15% 折扣並加 8% 銷售稅,請計算總金額。只回傳答案。

AI Agents 入門

ReAct 實作:簡單算術

correct

使用的 Prompt

Calculate the total cost if I buy 3 laptops at $899 
each with a 15% discount and 8% sales tax. Think step by step.

Follow this format:

Thought: [Think about what to calculate first]
Action: [Perform calculation]
Observation: [Result of calculation]
...repeat as needed...
Final Answer: [Complete solution]

Example:

Thought: Calculate base cost first
Action: 2 × $50 = $100
Thought: Apply 10% discount
Action: $100 - ($100 × 0.10) = $90
Final Answer: Total is $90
AI Agents 入門

沒有使用 ReAct 提示

incorrect_answer

正確答案 $2475.85

ChatGPT 回答: $2,776.63

AI Agents 入門

使用 ReAct 提示後

correct_answer

正確答案 $2475.85

ChatGPT 回答: $2475.85

AI Agents 入門

ReAct 作為思考方式

ReAct 是模型系統提示的一部分

System prompt 是一組隱藏指令,指示模型在整段對話中的行為方式。

1 Image generated with GPT-4o
AI Agents 入門

推理模型與 ReAct

$$

  • ReAct 對傳統語言模型(如 GPT 系列)特別有用
  • 新一代推理模型經過明確訓練會逐步思考,無需 ReAct 提示

$$ 推理模型範例

  • OpenAI o-series 系列
  • DeepSeek R-series 系列
  • Gemini thinking 模型
AI Agents 入門

如何辨識推理模型

thinking_models

AI Agents 入門

一起來練習吧!

AI Agents 入門

Preparing Video For Download...