透過超過 100 個技巧的集合來學習 Nuxt!

llms
nuxt-llms

為你的 Nuxt 應用程式產生 llms.txt 文件

nuxt-llms-social-card

Nuxt LLMs

npm versionnpm downloadsLicenseNuxt

Nuxt LLMs 自動為你的 Nuxt 應用程式產生 llms.txt markdown 文件。它提供執行階段 hooks 以從各種來源(CMS、Nuxt Content 等)收集資料,並以文字格式產生結構化文件。

功能

  • 自動產生 & 預先渲染 /llms.txt
  • 啟用時產生 & 預先渲染 /llms_full.txt
  • 可直接從你的 nuxt.config.ts 自訂章節
  • 透過執行階段 hooks 系統與 Nuxt 模組和你的應用程式整合

快速設定

  1. 安裝模組
npm i nuxt-llms
  1. 在你的 nuxt.config.ts 中註冊 nuxt-llms
export default defineNuxtConfig({
  modules: ['nuxt-llms']
})
  1. 設定你的應用程式詳細資訊
export default defineNuxtConfig({
  modules: ['nuxt-llms'],
  llms: {
    domain: 'https://example.com',
    title: 'My Application',
    description: 'My Application Description',
    sections: [
      {
        title: 'Section 1',
        description: 'Section 1 Description',
        links: [
          {
            title: 'Link 1',
            description: 'Link 1 Description',
            href: '/link-1',
          },
          {
            title: 'Link 2',
            description: 'Link 2 Description',
            href: '/link-2',
          },
        ],
      },
    ],
  },
})

就這樣!你可以訪問 /llms.txt 以查看產生的文件 ✨

選項

  • domain (必填):應用程式的網域
  • title:應用程式的標題,將顯示在文件頂部
  • description:應用程式的描述,將在標題後立即顯示在文件頂部
  • sections:文件的章節。章節由標題、一或多段描述以及可能的連結清單組成。每個章節都是一個具有以下屬性的物件
    • title (必填):章節的標題
    • description:章節的描述
    • links:章節的連結
      • title (必填):連結的標題
      • description:連結的描述
      • href (必填):連結的 href
  • notes:文件的註釋。註釋是一個特殊的章節,始終顯示在文件末尾。註釋可用於新增關於應用程式或文件本身的任何資訊。
  • fullllms_full.txt 設定。設定此選項將啟用 llms_full.txt 路由。
    • title:llms_full 文件的標題
    • description:llms_full 文件的描述

文件格式

此模組產生兩種不同的文件格式

llms.txt

/llms.txt 路由產生一個簡潔、結構化的文件,遵循 llms.txt 規範。此格式針對人類可讀性和 AI 消耗進行了最佳化。它包括

  • 應用程式標題和描述
  • 具有標題和描述的組織化章節
  • 具有標題、描述和 URL 的連結
  • 可選的註釋章節

llms_full.txt

/llms_full.txt 路由提供更詳細、自由格式的文件格式。這有助於減少應用程式上的爬蟲程式流量,並為你的使用者和 LLM 提供更詳細的文件。

預設情況下,模組不會產生 llms_full.txt 路由,你需要透過在你的 nuxt.config.ts 中設定 full.titlefull.description 來啟用它。

export default defineNuxtConfig({
  llms: {
    domain: 'https://example.com',
    title: 'My Application',
    full: {
      title: 'Full Documentation',
      description: 'Full documentation of the application',
    },
  },
})

使用 hooks 擴展文件

此模組提供一個 hooks 系統,讓你可以動態擴展兩種文件格式。主要有兩個 hooks

可用的 Hooks

llms:generate(event, options)

此 hook 針對每個 /llms.txt 的請求呼叫。使用此 hook 修改結構化文件,它允許你新增章節、連結和元資料。

參數

  • event:H3Event - 當前請求事件
  • options:ModuleOptions - 你可以修改的模組選項,以新增章節、連結等。

llms:generate:llms_full(event, options, contents)

此 hook 針對每個 /llms_full.txt 的請求呼叫。它允許你新增任何格式的自訂內容章節。

參數

  • event:H3Event - 當前請求事件
  • options:ModuleOptions - 你可以修改的模組選項,以新增章節、連結等。
  • contents:string - 你可以新增或修改的內容章節陣列

在你的應用程式中使用 Hooks

在你的 server/plugins 目錄中建立伺服器外掛程式

// server/plugins/llms.ts
export default defineNitroPlugin((nitroApp) => {
  // Method 1: Using the hooks directly
  nitroApp.hooks.hook('llms:generate', (event, options) => {
    // Add a new section to llms.txt
    options.sections.push({
      title: 'API Documentation',
      description: 'REST API endpoints and usage',
      links: [
        {
          title: 'Authentication',
          description: 'API authentication methods',
          href: `${options.domain}/api/auth`
        }
      ]
    })
  })

  // Method 2: Using the helper function
  nitroApp.hooks.hook('llms:generate:full', (event, options, contents) => {
    // Add detailed documentation to llms_full.txt
    contents.push(`## API Authentication

### Bearer Token
To authenticate API requests, include a Bearer token in the Authorization header:

\`\`\`
Authorization: Bearer <your-token>
\`\`\`

### API Keys
For server-to-server communication, use API keys:

\`\`\`
X-API-Key: <your-api-key>
\`\`\`
    `)
  })
})

在 Nuxt 模組中使用 Hooks

如果你正在開發需要擴展 LLMs 文件的 Nuxt 模組

  1. 在你的模組中建立伺服器外掛程式
// module/runtime/server/plugins/my-module-llms.ts
export default defineNitroPlugin((nitroApp) => {
  nitroApp.hooks.hook('llms:generate', (event, options) => {
    options.sections.push({
      title: 'My Module',
      description: 'Documentation for my module features',
      links: [/* ... */]
    })
  })
})
  1. 在你的模組設定中註冊外掛程式
import { defineNuxtModule, addServerPlugin } from '@nuxt/kit'
import { fileURLToPath } from 'url'

export default defineNuxtModule({
  setup(options, nuxt) {
    const runtimeDir = fileURLToPath(new URL('./runtime', import.meta.url))
    addServerPlugin(resolve(runtimeDir, 'server/plugins/my-module-llms'))
  }
})

整合

Nuxt Content

Nuxt Content ^3.2.0 隨附對 LLMs 文件的內建支援。你可以將 nuxt-llms@nuxt/content 結合使用,以有效率地為你的網站編寫內容和文件,並產生對 LLM 友善的文件,而無需額外努力。Content 模組使用 nuxt-llms hooks 並自動將你的所有內容新增到 llms.txtllms_full.txt 文件中。

你只需要安裝這兩個模組,並在 content 目錄中編寫你的內容檔案。

export default defineNuxtConfig({
  modules: ['nuxt-llms', '@nuxt/content'],
  llms: {
    domain: 'https://example.com',
    title: 'My Application',
    description: 'My Application Description',
  },
})

查看 Nuxt Content 文件,以取得關於如何編寫內容檔案的更多資訊。

並查看 Nuxt Content llms 文件,以取得關於如何使用 nuxt-llms@nuxt/content 自訂 LLMs 內容的更多資訊。

💻 開發

  • 複製儲存庫
  • 使用 pnpm install 安裝依賴項
  • 使用 pnpm dev:prepare 準備
  • 使用 pnpm prepack 建置
  • 使用 pnpm dev 試用 playground
  • 使用 pnpm test 測試

許可證

MIT 許可證

版權所有 (c) NuxtLabs