English 繁體中文 简体中文

如何使用 AI 代理刪去草稿中的重複句子(Research,版本 5)?

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

上一個版本用了三個工具: search_internet、 generate_draft 和 write_draft。

這個版本加入第四個工具 enhance_draft,它讀取已完成的草稿,刪去沒有 帶來新東西的句子。每一句仍然是原文照抄,每一個尾註仍然指向一個網頁。只是 兩者的數目都少了,而剩下的每一句都有它留下的理由。

每次執行只搜尋一次

在新工具之前,搜尋工具有一點小改動。它現在把答案存入名為 tavily_search.json 的檔案,如果檔案已經存在, 就直接讀取它。

    if os.path.exists("tavily_search.json"):
        with open("tavily_search.json", "r", encoding="utf-8") as f:
            response = json.load(f)
    else:
        response = tavily_client.search(query, max_results=10)
        with open("tavily_search.json", "w", encoding="utf-8") as f:
            json.dump(response, f, ensure_ascii=False, indent=2)

os.path.exists 問一個簡單問題:這個檔案是否 已在資料夾裡?如果已在,就用 json.load 讀回 已儲存的答案,不再搜尋。如果不在,就呼叫 Tavily,並用 json.dump 把答案寫出。

在程式接近底部的位置,檔案會在執行開始之前被刪除,所以每次執行的第一次 搜尋都是最新的:

if os.path.exists("tavily_search.json"):
    os.remove("tavily_search.json")  # start each run with a fresh search

indent=2 令你打開檔案時看得清楚,而 ensure_ascii=False 讓非英文字符保持原樣,不會 變成跳脫碼。執行完之後,這個檔案值得打開看看:裡面是 Tavily 回傳的全部 內容,不只是螢幕顯示的 200 個字元。

另外兩個共用的清單

generate_draft 工具本來就要把它的成果放在下 一個工具找得到的地方。enhance_draft 工具還需要 另外兩個地方:

draft_items = []
skipped_items = []

draft_items 存放草稿中每一句,連同它來自的 網頁,也就是新工具讀取的內容。skipped_items 存放被刪去的句子。

第四個工具:enhance_draft

return_direct=True 放在那個結果應該成為最終 答案的工具上。

@tool(return_direct=True)
def enhance_draft(keep: list[int]):
    """
    Write an enhanced draft from the draft made by write_draft.
    keep: the endnote numbers of the sentences to keep. Sentence 1 is always kept.
    Read sentences 2, 3, ... one by one, and keep a sentence only if it adds
    useful new information that the sentences already kept do not give.
    """

輸入是一串整數:keep: list[int]。哪些句子 留下,由模型決定 — 這是關於意思的判斷,而模型擅長這件事。之後的程式碼 負責精確的部分:重新編號尾註,並記錄被丟棄的句子。

工具內部

    skipped_items = []
    s = ""
    endnotes = ""
    endnote_num = 1
    for i, item in enumerate(draft_items, start=1):
        if i != 1 and i not in keep:
            skipped_items.append({"num": i, "sentence": item["sentence"], "url": item["url"]})
            continue  # skip sentences that add little new information
        s = s + item["sentence"] + "[" + str(endnote_num) + "]\n"
        endnotes = endnotes + f"[{endnote_num}] {item['url']}\n"
        endnote_num = endnote_num + 1

enumerate(draft_items, start=1) 逐項走過草稿, 每次都交出兩樣東西:位置 i 和該項目本身。 位置由 1 開始而不是 0,因為編號必須與讀者看到的尾註號碼一致。

這個判斷由兩個條件以 and 連接。 i != 1 保護第一句:無論模型說甚麼,它都會 保留。然後 i not in keep 問這一句是否屬於 模型選擇捨棄的句子。兩個條件都成立,句子才會被刪去。

被刪去的句子會連同原有編號、文字和連結寫入 skipped_items,程式之後就可以報告它。 continue 然後跳到下一句,也就是下面兩行不會 執行,這句不會得到尾註號碼。計數器只為保留下來的句子移動,所以新草稿的 編號是 [1]、 [2]、[3] …,中間沒有跳號。

在示範執行中,模型保留了第 1、2、4、6、7 句,共 5 句,所以它們在增強版 草稿中成為第 1、2、3、4、5 句。

AI 代理

agent = create_agent(
    model=model,
    tools=[search_internet, generate_draft, write_draft, enhance_draft],
    system_prompt="""
You are a helpful assistant.
Always call search_internet, using the user's message exactly as the query.
Do not rephrase, shorten, translate, or add anything to it.
Then call generate_draft to select the useful results.
Then go through each sentence in each selected result, pick the one sentence
closest in meaning to the user's query, and copy it exactly.
A complete sentence has a subject and a verb and ends with a full stop;
table rows, headings, menus and fragments are not complete sentences.
If a result has no complete sentence, use an empty string "" for it.
Call write_draft with these sentences in the same order as the results.
Then read the draft from write_draft. Keep sentence 1.
Read the remaining sentences one by one, and keep a sentence only if it adds
useful new information that the sentences already kept do not give.
Skip a sentence that only repeats a similar figure already given.
Keep a sentence that adds a new fact, such as a ranking, a trend,
a breakdown, or an official government source.
Call enhance_draft with the endnote numbers of the sentences to keep.
""",
)

工具清單現在有 search_internet、 generate_draft、 write_draft 和 enhance_draft。提示詞就是告訴模型該呼叫哪一個 工具、以及先後次序的東西。

提示詞中兩句關鍵的句子如下: 「逐句閱讀其餘句子,只有當某句帶來已保留句子沒有的有用新資訊時,才保留 它。」這兩句可以幫我們減少句子重複。

結果

以下程式碼區塊印出兩個工具的結果: write_draft 和 enhance_draft。

for message in result["messages"]:
    if message.type == "tool" and message.name in ["write_draft", "enhance_draft"]:
        print(f"Tool: {message.name}\n")
        print(message.content)
    elif message.type == "tool":
        response = json.loads(message.content)
        print(f"Tool: {message.name}")

以下印出被 enhance_draft 略過的句子,供我們 參考:

print("Sentences skipped by enhance_draft:\n")
if skipped_items == []:
    print("None\n")
for item in skipped_items:
    print(f"[{item['num']} in write_draft] {item['sentence']}")
    print(f"   {item['url']}\n")

輸出結果

螢幕現在印出四個工具區塊 — search_internet、 generate_draft、 write_draft 和 enhance_draft — 然後是被刪去的句子清單。 以下是增強版草稿:

Tool: enhance_draft

China, Hong Kong SAR 2026 population is estimated at 7,378,602 people at mid-year.[1]
As of 2026, Hong Kong has a total population of 7,378,602, ranking as the 107th most populous nation in the world.[2]
Total current population for Hong Kong in 2026 is 7,500,962, a 0.02% increase from 2025.[3]
Hong Kong's population was approximately 7.50 million in mid-2025.[4]
Hong Kong is a largely homogenous society, with about 91.6% of its people being Chinese (ethnically speaking, Han Chinese).[5]

Endnotes:
[1] https://www.worldometers.info/world-population/china-hong-kong-sar-population
[2] https://worldpopulationreview.com/countries/hong-kong
[3] https://www.macrotrends.net/global-metrics/countries/hkg/hong-kong/population
[4] https://www.gov.hk/en/about/abouthk/facts.htm
[5] https://www.had.gov.hk/rru/en/info/demographics.htm

七句變成五句,尾註由 1 重新編號,所以中間沒有缺號。

接著是令這個選擇可以被核對的部分。這次執行刪去了這些:

Sentences skipped by enhance_draft:

[3 in write_draft] The population of Hong Kong is 7.5 million.
   https://study.com/academy/lesson/hong-kong-facts-population.html

[5 in write_draft] In 2023, the estimated population of Hong Kong was 7.443 million.
   https://ourworldindata.org/profile/population-demography/hong-kong

兩句被丟棄的原因相同:這個數字已經有了。study.com 的頁面說 750 萬,沒有 註明年份;Our World in Data 的頁面給出 2023 年的 744.3 萬;而草稿已經有 2026 年的 7,500,962 和 2025 年中的 750 萬。

結果並不完美,這一點值得說明。第 [1] 句和 第 [2] 句都說 7,378,602,無論怎樣衡量都是 重複;第二句之所以留下,是因為它同時加上了第 107 位的排名。如果工具把所有 出現兩次的內容都刪去,排名也會被一併拿走。

留下的五句仍然互相矛盾:7,378,602、7,500,962 和 750 萬,是同一個問題的 三個不同答案。刪去重複並不能消除分歧,這個版本也沒有這樣聲稱。

我們可以再加一個工具來改進提示詞,例如 generate_summary,讀取每句中的數值,並說出 例如所找到數字的平均值或中位數。

完整程式

import os
import json
from dotenv import load_dotenv
from tavily import TavilyClient
from langchain_core.tools import tool
from langchain_openai import ChatOpenAI
from langchain.agents import create_agent

load_dotenv()
DEEPSEEK_API_KEY = os.getenv("DEEPSEEK_API_KEY")
TAVILY_API_KEY = os.getenv("TAVILY_API_KEY")
tavily_client = TavilyClient(api_key=TAVILY_API_KEY)

last_search = {}
selected_results = []
draft_items = []
skipped_items = []

@tool
def search_internet(query: str):
    """
    Search the internet for up-to-date information about the query
    and return the full Tavily search response.
    """
    global last_search
    if os.path.exists("tavily_search.json"):
        with open("tavily_search.json", "r", encoding="utf-8") as f:
            response = json.load(f)
    else:
        response = tavily_client.search(query, max_results=10)
        with open("tavily_search.json", "w", encoding="utf-8") as f:
            json.dump(response, f, ensure_ascii=False, indent=2)
    last_search = response
    return response

@tool
def generate_draft():
    """
    Go through the results from the latest search_internet call.
    Keep results with a score of 0.5 or higher, but never keep Wikipedia.
    Return the selected results.
    """
    global selected_results
    selected = []
    for r in last_search.get("results", []):
        if r["score"] >= 0.5 and "wikipedia.org" not in r["url"]:
            selected.append(r)

    selected_results = selected
    return {"query": last_search.get("query"), "results": selected}

@tool
def write_draft(sentences: list[str]):
    """
    Write a draft with endnotes from the results of generate_draft.
    sentences: one sentence per result from generate_draft, in the same order.
    Each sentence must be copied exactly from that result's content, and be
    the sentence closest in meaning to the user's query.
    Use an empty string "" for a result that has no complete sentence.
    """
    global draft_items
    draft_items = []
    s = ""
    endnotes = ""
    endnote_num = 1
    for sentence, r in zip(sentences, selected_results):
        if sentence.strip() == "":
            continue  # skip results with no complete sentence
        s = s + sentence + "[" + str(endnote_num) + "]\n"
        endnotes = endnotes + f"[{endnote_num}] {r['url']}\n"
        draft_items.append({"sentence": sentence, "url": r["url"]})
        endnote_num = endnote_num + 1
    s = s + "\nEndnotes:\n" + endnotes
    return s

@tool(return_direct=True)
def enhance_draft(keep: list[int]):
    """
    Write an enhanced draft from the draft made by write_draft.
    keep: the endnote numbers of the sentences to keep. Sentence 1 is always kept.
    Read sentences 2, 3, ... one by one, and keep a sentence only if it adds
    useful new information that the sentences already kept do not give.
    """
    global skipped_items
    skipped_items = []
    s = ""
    endnotes = ""
    endnote_num = 1
    for i, item in enumerate(draft_items, start=1):
        if i != 1 and i not in keep:
            skipped_items.append({"num": i, "sentence": item["sentence"], "url": item["url"]})
            continue  # skip sentences that add little new information
        s = s + item["sentence"] + "[" + str(endnote_num) + "]\n"
        endnotes = endnotes + f"[{endnote_num}] {item['url']}\n"
        endnote_num = endnote_num + 1
    s = s + "\nEndnotes:\n" + endnotes
    return s

model = ChatOpenAI(
    model="deepseek-flash",
    api_key=DEEPSEEK_API_KEY,
    base_url="https://api.deepseek.com",
    temperature=0,
)

agent = create_agent(
    model=model,
    tools=[search_internet, generate_draft, write_draft, enhance_draft],
    system_prompt="""
You are a helpful assistant.
Always call search_internet, using the user's message exactly as the query.
Do not rephrase, shorten, translate, or add anything to it.
Then call generate_draft to select the useful results.
Then go through each sentence in each selected result, pick the one sentence
closest in meaning to the user's query, and copy it exactly.
A complete sentence has a subject and a verb and ends with a full stop;
table rows, headings, menus and fragments are not complete sentences.
If a result has no complete sentence, use an empty string "" for it.
Call write_draft with these sentences in the same order as the results.
Then read the draft from write_draft. Keep sentence 1.
Read the remaining sentences one by one, and keep a sentence only if it adds
useful new information that the sentences already kept do not give.
Skip a sentence that only repeats a similar figure already given.
Keep a sentence that adds a new fact, such as a ranking, a trend,
a breakdown, or an official government source.
Call enhance_draft with the endnote numbers of the sentences to keep.
""",
)

if os.path.exists("tavily_search.json"):
    os.remove("tavily_search.json")  # start each run with a fresh search

prompt = "What is the population of Hong Kong?"
result = agent.invoke({
    "messages": [{"role": "user", "content": prompt}]
})

for message in result["messages"]:
    if message.type == "tool" and message.name in ["write_draft", "enhance_draft"]:
        print(f"Tool: {message.name}\n")
        print(message.content)
    elif message.type == "tool":
        response = json.loads(message.content)
        print(f"Tool: {message.name}")
        print(f"Query: {response['query']}\n")
        for i, r in enumerate(response["results"], start=1):
            print(f"{i}. {r['title']}  (score: {r['score']:.2f})")
            print(f"   {r['url']}")
            print(f"   {r['content'][:200].replace(chr(10), ' ')}...\n")

print("Sentences skipped by enhance_draft:\n")
if skipped_items == []:
    print("None\n")
for item in skipped_items:
    print(f"[{item['num']} in write_draft] {item['sentence']}")
    print(f"   {item['url']}\n")

在自己的電腦上執行

下載示範程式 (Folder-AI-Research05.zip)。zip 內有三個檔案: research05.py(完整腳本)、pyproject.toml 和 README.md。這個版本會呼叫語言模型,所以需要 Tavily 金鑰和 DeepSeek 金鑰。

  1. 在 Visual Studio Code 中開啟這個資料夾。
  2. 在資料夾裡建立一個名為 .env 的檔案,內容有兩行:
    TAVILY_API_KEY=your-tavily-key
    DEEPSEEK_API_KEY=your-deepseek-key
    Tavily 金鑰可在 Tavily 控制台取得,DeepSeek 金鑰則可在 DeepSeek 控制台取得。使用量由各供應商收費,所以執行之前,請先在該處查看目前的 收費條款。
  3. 在終端機逐行執行以下三行:
    uv python pin 3.12.10
    uv sync
    uv run research05.py
  4. 你應該會依次看到四個工具區塊 — 搜尋的十個結果、 generate_draft 的七個結果、來自 write_draft 的七句草稿,然後是 enhance_draft 縮短後的草稿 — 接著是 被略過句子的清單。這次執行只需幾秒鐘。

如果你還未設定好 uv,請先看 如何安裝 Visual Studio Code?,然後看 uv 筆記的 Windows 或 Mac 版本。

限制

延伸閱讀