English 繁體中文 简体中文

AI 代理如何研究一個問題(ReAct,版本 3,在迴圈中進行網上搜尋)?

林邦源博士 · AI 筆記 · 2026 年 11 月 9 日 · 約 5 分鐘閱讀

上一篇筆記 問一個沒有工具的模型:「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 ===")

每一部分的作用

一次真實執行

四輪。留意第 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.

摘錄為求易讀而經過縮短(…);完整執行會印出每個結果的前 400 個字元。

把這個答案與上一篇筆記中沒有工具的答案比較一下。問題完全相同;改變的是, 推理現在有東西可以配合。最精彩的推理發生在第 2 輪:模型讀完 三個結果,發現 CNBC 和 TechCrunch 對日期的說法有出入,於是寫出一條更精準的 查詢來釐清。這個判斷,是任何工具都無法替它做的部分。

在自己的電腦上執行

下載示範程式 (Folder-AI-ReAct03.zip)。zip 內有三個檔案: ReAct03.py(就是上面的腳本)、pyproject.toml (套件清單)和 README.md。

  1. 在 Visual Studio Code 開啟這個資料夾。
  2. 在資料夾內建立一個名為 .env 的檔案,內容有兩行:
    DEEPSEEK_API_KEY=your-deepseek-key
    TAVILY_API_KEY=your-tavily-key
    第一個金鑰從 DeepSeek 控制台取得,第二個從 Tavily取得; 兩者都有免費的起始額度。
  3. 在終端機執行:
    uv sync
    uv run ReAct03.py
    uv sync 只需要第一次執行;它會讀取 pyproject.toml,並把 openai、 tavily-python 和 python-dotenv 裝進這個專案。

為甚麼這次執行重要

延伸閱讀