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 装进这个项目。

为什么这次运行重要

延伸阅读