Fumadocs rebuilt on Hamur, the original is fumadocs.dev
跳至內容
Fumadocs

AI 與 LLM

讓 AI 代理能夠讀取您的文件,並加入 AI 聊天功能。

供 LLM 使用的 Markdown

以 Markdown 格式將您的文件提供給 AI 代理:

npx @fumadocs/cli feature llms

接著,您的網站會提供:

  • /llms.txt:文件的索引,並連結至每個頁面的 Markdown。
  • /llms-full.txt:所有頁面合併成的單一檔案。
  • /docs/ui.md:單一頁面的 Markdown;位於 / 的頁面則為 /index.md。

CLI 會在 lib/source.ts 中加入 docsLlms,由它負責算繪這些檔案:

lib/source.ts
import { llms } from 'fumadocs-core/source';

export const docsLlms = llms(source, {
  // 每個頁面的 Markdown
  renderPage: async (page) => `# ${page.data.title} (${page.url})

${await page.data.getText('processed')}`,
  // 排除草稿;`llms.txt` 會改為連結到草稿的 HTML 頁面
  filter: (page) => page.slugs[0] !== 'drafts',
});

MDX 元件在 Markdown 中會維持 JSX 的形式,除非元件有定義 Markdown 形式。

區段

每個根資料夾都是一個區段,並有各自的 llms-full.txt,例如 /docs/ui/llms-full.txt,讓 AI 代理能分段讀取大型文件。您可以透過 section 選擇其他區段:

lib/source.ts
export const docsLlms = llms(source, {
  section(folder) {
    // 收合:在 `llms.txt` 中僅列為一個連結,指向該區段自己的 `llms.txt`
    if (folder.root) return { collapse: true };

    return folder.index !== undefined;
  },
  // ...
});

頁面動作

頁面動作可讓讀者複製頁面的 Markdown 內容,或在 AI 應用程式中開啟該頁面。

AI 頁面動作

將它們加入文件頁面的 React island:

<Docs
  client:load
  markdownUrl={`${page.url}.md`}
  githubUrl={`https://github.com/${owner}/${repo}/blob/main/content/docs/${encodeURI(page.path)}`}
>
  <Content />
</Docs>

Accept 標頭

文件頁面是預先算繪的,因此無法針對 Accept: text/markdown 提供 Markdown。

MCP 伺服器

透過 MCP 伺服器,讓 AI 代理列出、搜尋及讀取您的文件:

npx @fumadocs/cli feature mcp

伺服器會在收到請求時才進行算繪,因此您的專案需要設定轉接器。

接著,將 AI 代理連線至此伺服器:

{
  "mcpServers": {
    "docs": { "url": "https://your-site.com/api/mcp" }
  }
}

若要自訂這些工具,請參閱 fumadocs-core/mcp。

WebMCP

實驗性功能

WebMCP 是一項仍處於早期階段的 Web 標準,目前可在 Chrome 149+ 中透過 #enable-webmcp-testing 旗標及來源試用 (origin trial) 啟用。

WebMCP 為讀者瀏覽器中的 AI 代理提供工具,讓其能搜尋並閱讀您的文件:

npx @fumadocs/cli feature webmcp

請在 <RootProvider /> 內算繪產生的 WebMCP 元件。

詢問 AI

AI 搜尋

在您的文件版面配置中加入 AI 聊天功能:

npx @fumadocs/cli feature ai

請選擇一個供應商,並在 .env.local 中設定其 API 金鑰,例如 OpenRouter 對應的是 OPENROUTER_API_KEY。若要改用其他模型,請編輯產生的 /api/chat 路由。

CLI 會將聊天功能加入您的文件版面配置。如果 CLI 找不到您的版面配置,請手動加入聊天功能:

'use client';
import { DocsLayout as Layout, type DocsLayoutProps } from 'fumadocs-ui/layouts/docs';
import { AIChat, AIChatPanel, useAIChat } from './search';

export function DocsLayout(props: DocsLayoutProps) {
  return (
    <AIChat>
      <ChatLayout {...props} />
    </AIChat>
  );
}

function ChatLayout(props: DocsLayoutProps) {
  const { open, setOpen } = useAIChat();

  return (
    <Layout
      {...props}
      aiChat={{ open, onOpenChange: setOpen, panel: <AIChatPanel />, trigger: true }}
    />
  );
}

How is this guide?

最後更新於