AI 代理如何研究一個問題(ReAct,版本 3,在迴圈中進行網上搜尋)?
上一篇筆記 問一個沒有工具的模型:「Mark 何時推出 Muse?」它仔細思考之後, 誠實地回答:「我不知道。」這篇筆記問同一個問題, 只多加一個工具:由 Tavily 提供的網上搜尋。代理現在可以重複 思考 → 行動 → 觀察這個迴圈 — 最多五輪 — 而且只有在它認為自己 知道答案時才停下來。
腳本
這是 ReAct03.py 目前的內容:
# Same ReAct idea as react01.py, but this task needs MORE THAN ONE action,
# so we repeat Think -> Act -> Observe in a loop until the model says "finish".
# Step 1: THINK -> ask the model to reason about what to do next
# Step 2: ACT -> ask for ONE action: search[...] or finish[...]
# Step 3: OBSERVE-> run the Tavily web search (a real tool), add the result to the prompt, loop again
import os
import re
from datetime import datetime
from tavily import TavilyClient
from openai import OpenAI
from dotenv import load_dotenv
load_dotenv()
client = OpenAI(
api_key=os.environ.get('DEEPSEEK_API_KEY'),
base_url="https://api.deepseek.com")
tavily = TavilyClient(api_key=os.environ.get('TAVILY_API_KEY'))
def ask(prompt, stop=None):
"""Send one prompt to the model and return its text reply."""
response = client.chat.completions.create(
model="deepseek-flash",
messages=[{"role": "user", "content": prompt}],
stop=stop, # stop writing when this text appears
)
return response.choices[0].message.content
# The only tool (action) our agent can use
def search_tavily(query):
"""Return the top web results from Tavily (title, url, short content)."""
results = tavily.search(query=query, max_results=3)["results"]
if not results:
return f"No web results found for '{query}'."
return "\n".join(f"[{r['title']}] ({r['url']}) {r['content'][:400]}"
for r in results)
task = ("When did Mark launch Muse")
prompt = f"""Task: {task}
You can use ONE tool: search[query] searches the web (Tavily) and returns the top results.
Today is {datetime.now().strftime('%Y-%m-%d')}.
Work in steps. Each step has a Thought, an Action, and an Observation."""
for step in range(1, 6): # at most 5 rounds, so it cannot loop forever
# ---- Step 1: THINK (Reasoning) ----
thought = ask(prompt + f"\n\nThought {step}: (reason briefly about what to do next; do not act yet)",
stop=["Action"]) # stop the model inventing its own Action/Observation
print(f"=== Round {step} - Thought ===")
print(thought)
# ---- Step 2: ACT — append the reasoning trace, then ask for an action ----
prompt = prompt + f"\n\nThought {step}: {thought}"
action = ask(prompt + "\n\nWhat action should be taken based on that? "
"Reply with ONLY: search[query] or finish[answer]").strip()
print(f"=== Round {step} - Action chosen by the model ===")
print(action)
if action.startswith("finish"):
answer = re.search(r"finish\[(.*)\]", action, re.S)
print("=== Final Answer ===")
print(answer.group(1) if answer else action)
break
# ---- Step 3: OBSERVE — our Python code (not the model) runs the tool ----
query = re.search(r"search\[(.*)\]", action, re.S)
observation = search_tavily(query.group(1)) if query else "Invalid action."
print(f"=== Round {step} - Observation from Tavily web search ===")
print(observation)
prompt = prompt + f"\nAction {step}: {action}\nObservation {step}: {observation}"
else:
print("=== Stopped: no answer after 5 rounds ===")
每一部分的作用
- 兩把金鑰,兩個服務。DeepSeek 負責思考,Tavily 負責搜尋。 Tavily 是專為代理而設的搜尋服務:你送出一個查詢,它回傳乾淨的標題、 連結和簡短摘錄 — 你不用自己去抓取網頁。它設有免費方案,方便試用。
-
search_tavily(query)只保留最前面的三個結果, 每個結果只取前 400 個字元。一個填滿畫面的觀察,會把推理淹沒。 -
提示詞寫明規則:只有一個工具,寫成
search[query];今天的日期,讓「今天」和「今年」有意義; 以及每一步的格式 — 思考、行動、觀察。 -
迴圈有預算。
range(1, 6)最多只容許五輪。 如果模型始終沒有說finish[...],程式就會停下來,並說明原因。 沒有預算的代理,就像沒有上限的帳單。 -
stop=["Action"]是防護欄。不加限制的話, 聊天模型會樂於順著這個格式繼續寫,自行編造出屬於它的行動和觀察 — 也就是假的搜尋結果。停止詞會在思考之後截斷回覆,所以真正的行動和真正的 觀察,永遠來自你的程式碼,而不是模型的想像。 - 提示詞每一輪都會變長。每個思考、行動和觀察都會附加進去, 所以第 4 輪可以看到第 1–3 輪所發現的一切。提示詞就是代理的記憶。
一次真實執行
四輪。留意第 2 輪怎樣發現兩個來源說法有出入,以及代理如何一直搜尋, 直到日期對得上:
=== Round 1 - Thought ===
Thought 1: I need to find out when Mark (likely Mark Zuckerberg, given "Muse") launched something called Muse. I should search the web for this, but I need to be careful—"Muse" could refer to different things (e.g., a Meta product, an AI model, or a project by another Mark). I'll start with a targeted search query combining "Mark" and "Muse launch" to get relevant results.
=== Round 1 - Action chosen by the model ===
search[Mark Muse launch date]
=== Round 1 - Observation from Tavily web search ===
[Meta Muse in the UK, Europe and Australia: Where It Stands in October 2026] (https://medium.com/@mateohq/meta-muse-in-the-uk-europe-and-australia-where-it-stands-in-october-2026-3342eb499e81) ...Status: not available, no date announced...
[Meta launches Muse for Small Business as Zuckerberg pushes beyond consumer AI market] (https://www.cnbc.com/2026/09/29/meta-launches-muse-for-small-business-zuckerberg-pushes-enterprise-ai.html) ...Published Tue, Sep 29 2026...
[Everything new coming to Meta's AI agent Muse | TechCrunch] (https://techcrunch.com/2026/09/23/everything-new-coming-to-metas-ai-agent-muse) Muse, which launched in early September, interacts across a user's devices and digital accounts...
=== Round 2 - Thought ===
Thought 2: The search results give conflicting or unclear timing: one CNBC article says Meta launched Muse for Small Business on September 29, 2026, while a TechCrunch article says Muse "launched in early September." I need to pin down the original launch date of Muse itself, not just the small-business version. I should search more specifically...
=== Round 2 - Action chosen by the model ===
search[Meta launches Muse AI agent date early September 2026]
=== Round 2 - Observation from Tavily web search ===
[Meta Muse Launches: AI Agent That Shops, Books and Negotiates] (https://www.how2shout.com/ai/meta-muse-personal-ai-agent-launch.html) Meta launched Muse on Tuesday (September 8, 2026), a personal AI agent that books travel, sends email, shops, and negotiates on your behalf...
[Instagram] (https://www.instagram.com/p/Dd_SU5yEbfg) Meta announced Muse on September 8, 2026, describing it as a personal AI agent that can take action—not just answer questions...
[Meta Muse is a secure, personal AI... - Tipp City RoadDog] (https://www.facebook.com/TippCityRoadDog/posts/...) ...Launched in September 2026, it shifts the paradigm from a traditional chatbot...
=== Round 3 - Thought ===
Thought 3: I now have converging evidence that Meta's Muse AI agent launched on September 8, 2026, with a small-business version following on September 29, 2026. The question asks "When did Mark launch Muse" — this almost certainly refers to Mark Zuckerberg and Meta's Muse. The best answer is the original launch date: September 8, 2026. I should do one more search to confirm...
=== Round 3 - Action chosen by the model ===
search[Meta Muse official launch September 8 2026 Zuckerberg announcement]
=== Round 3 - Observation from Tavily web search ===
[Zuckerberg Is Back (And It's Not the Model) - Meta Muse Launch] (https://www.youtube.com/watch?v=-_ovL8w1TMM) On 8 September, Meta launched Muse: a personal AI agent with its own computer and its own wallet...
[Meta Enterprise Platform: What Zuckerberg Announced | CellCog] (https://cellcog.ai/blog/meta-enterprise-platform) On September 28, 2026 ... Mark Zuckerberg announced Meta Enterprise Platform...
[Meta launches Muse for Small Business ... CNBC] (https://www.cnbc.com/2026/09/29/meta-launches-muse-for-small-business-zuckerberg-pushes-enterprise-ai.html) ...
=== Round 4 - Thought ===
Thought 4: The evidence is now consistent and converging. Meta (Mark Zuckerberg's company) launched the Muse personal AI agent on September 8, 2026, with the US-only rollout on iOS, Android, and muse.ai. A small-business/enterprise version followed later in September (announced September 28–29, 2026). The question "When did Mark launch Muse" is best answered with the original consumer launch date. I have enough to finalize.
=== Round 4 - Action chosen by the model ===
finish[Mark Zuckerberg's Meta launched Muse on September 8, 2026. It was a US-only personal AI agent rollout across iOS, Android, and muse.ai. A small-business/enterprise version followed around September 28–29, 2026.]
=== Final Answer ===
Mark Zuckerberg's Meta launched Muse on September 8, 2026. It was a US-only personal AI agent rollout across iOS, Android, and muse.ai. A small-business/enterprise version followed around September 28–29, 2026.
把這個答案與上一篇筆記中沒有工具的答案比較一下。問題完全相同;改變的是, 推理現在有東西可以配合。最精彩的推理發生在第 2 輪:模型讀完 三個結果,發現 CNBC 和 TechCrunch 對日期的說法有出入,於是寫出一條更精準的 查詢來釐清。這個判斷,是任何工具都無法替它做的部分。
在自己的電腦上執行
下載示範程式
(Folder-AI-ReAct03.zip)。zip 內有三個檔案:
ReAct03.py(就是上面的腳本)、pyproject.toml
(套件清單)和 README.md。
- 在 Visual Studio Code 開啟這個資料夾。
-
在資料夾內建立一個名為
.env的檔案,內容有兩行:
第一個金鑰從 DeepSeek 控制台取得,第二個從 Tavily取得; 兩者都有免費的起始額度。DEEPSEEK_API_KEY=your-deepseek-key TAVILY_API_KEY=your-tavily-key - 在終端機執行:
uv sync uv run ReAct03.pyuv sync只需要第一次執行;它會讀取pyproject.toml,並把openai、tavily-python和python-dotenv裝進這個專案。
為甚麼這次執行重要
- 這個迴圈做了真正的研究。不是一次搜尋,而是三次: 每一輪都讀上一輪找到的東西,再選一條更好的查詢。互相衝突的日期被發現並 解決,而不是取平均值。
- 答案帶著證據。最後那句句子裡的每一項說法,都可以追溯到 某個觀察中具名的來源 — 這正是單靠記憶的答案無法提供的。
- 預算讓它保持誠實。本來有五輪可用,實際只需四輪。模型 自己決定何時停手,而就算它不停,程式也會把它停下來。
- 同一個問題,三個答案。沒有工具時:「我不知道。」 只用一次工具時:對日期的一次猜測。有了迴圈:一個經過核實、附有出處的 答案。這就是這個系列一路走來的軌跡。
延伸閱讀
- ReAct: Synergizing Reasoning and Acting in Language Models — 為這個模式命名的論文,也是本系列所建立的基礎。
- Tavily 文件 — 搜尋選項、免費方案與定價。
- DeepSeek API 文件 — 模型、金鑰與定價。