Recipes

文档转 llm.txt

把任意文档站点合成单个可直接喂给 LLM 的 Markdown 文件

把整个文档站点蒸馏(Distill)成一份 llm.txt,可以直接粘贴到任意 LLM 上下文、RAG 流水线或本地模型里。适用于不熟悉的库、内部 wiki 以及产品文档。

流程

  1. include: ["links"] 蒸馏首页,发现所有外链 URL
  2. 按 URL 模式过滤链接列表(例如 /docs//guide/
  3. 把过滤后的 URL 喂给 /batch/distill
  4. 把生成的 Markdown 拼接成单个文件

实现

import httpx, re

API = "https://openapi.thunderbit.com/openapi/v1"
H = {"Authorization": "Bearer YOUR_API_KEY"}

# 1. Pull the index page + outbound links
index = httpx.post(f"{API}/distill",
                   headers=H,
                   json={"url": "https://docs.example.com",
                         "include": ["links"]}).json()["data"]

# 2. Filter to docs paths
doc_urls = [u for u in index["links"] if re.search(r"/docs/", u)]

# 3. Batch distill
job = httpx.post(f"{API}/batch/distill",
                 headers=H,
                 json={"urls": doc_urls}).json()["data"]

# 4. Poll, concatenate
# (poll loop omitted; see RAG Knowledge Base recipe)

with open("llm.txt", "w") as f:
    for r in job["results"]:
        if r["status"] == "SUCCEEDED":
            f.write(f"# {r['url']}\n\n{r['markdown']}\n\n---\n\n")

小贴士

  • 加个体积上限 —— llm.txt 超过 ~1 MB 之后会开始膨胀 token 预算
  • 按 URL 或按章节排序,便于多次运行间产生稳定的 diff
  • 配合 CI 任务,让 llm.txt 随源文档同步更新

相关

这份 recipe 正在补充分块和去重策略,敬请期待。