English 繁體中文 简体中文

AI 代理如何在沒有工具的情況下回答(ReAct,版本 2,只推理)?

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

上一篇筆記建立了一個 會思考、行動和觀察的代理。本篇刪去三步之中的兩步。沒有行動、沒有觀察 — 模型只能思考,也就是所謂的思維鏈,然後憑 自己的記憶回答。它所說的每一句,都沒有對照真實世界核實。它面對的問題故意 含糊:「Mark 何時推出 Muse?」

程式碼

這是 ReAct02.py 目前的內容:

# "Reason only" (Chain-of-Thought): NO tool and NO loop.
# The model can only THINK step by step and answer from its own memory.
# Nothing is checked against the real world, so the answer may be wrong (hallucination).
# Compare the result with react03.py (Reason + Act), which searches the web with Tavily.
import os
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")

def ask(prompt):
    """Send one prompt to the model and return its text reply."""
    response = client.chat.completions.create(
        model="deepseek-flash",
        messages=[{"role": "user", "content": prompt}],
    )
    return response.choices[0].message.content

task = ("When did Mark launch Muse")

# ---- THINK only (Reasoning) — no Action, no Observation ----
prompt = f"""Task: {task}
You have NO tools. Think step-by-step using only what you already know,
then end with one line: Answer: <your answer>"""
reply = ask(prompt)
print("=== Thought (reasoning trace, no tools) ===")
print(reply)

每一部分的作用

一次真實執行

=== Thought (reasoning trace, no tools) ===
Mark Zuckerberg launched Muse? I don't have knowledge of Mark launching something called Muse. There is a band Muse, but not launched by Mark. Perhaps you mean Meta's AI model? I'm not aware.

Answer: I don't know.

有兩點值得注意。第一,推理確實有用:模型看出當中的含糊(「有一隊叫 Muse 的樂隊,但不是 Mark 推出的」),而不是假裝問題很清楚。第二,最終答案是 誠實的 — 至少這一次。程式裡沒有任何東西強制它誠實。問一個 聽起來更普通的問題,同樣只靠記憶的推理,就可能產生一個自信、流暢但錯誤的 答案:幻覺。這裡沒有觀察步驟可以把它抓住。

在自己的電腦上執行

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

  1. 在 Visual Studio Code 開啟這個資料夾。
  2. 在資料夾內建立一個名為 .env 的檔案,寫入一行:
    DEEPSEEK_API_KEY=your-key-here
    到 DeepSeek 主控台取得金鑰;新帳戶附有試用額度。
  3. 在終端機執行:
    uv sync
    uv run ReAct02.py
    uv sync 只需第一次執行;它會讀取 pyproject.toml,把 openai 和 python-dotenv 安裝到專案內。

試著把任務改成一個有眾所周知答案的問題,然後改成關於一間小型本地公司的 問題。兩次執行的對比,就是這一課。

為甚麼這個實驗重要

延伸閱讀