llms
nuxt-llms

為您的 Nuxt 應用程式生成 llms.txt 文件

nuxt-llms-social-card

Nuxt LLMs

npm versionnpm downloadsLicenseNuxt

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

功能

  • 自動生成並預渲染 /llms.txt
  • 啟用後生成並預渲染 /llms-full.txt
  • 直接從您的 nuxt.config.ts 定製章節
  • 透過執行時鉤子系統與 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(必填):連結地址
  • 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',
    },
  },
})

使用鉤子擴充套件文件

該模組提供了一個鉤子系統,允許您動態擴充套件兩種文件格式。主要有兩個鉤子

可用鉤子

llms:generate(event, options)

此鉤子在每次請求 /llms.txt 時被呼叫。使用此鉤子修改結構化文件,它允許您新增章節、連結和元資料。

引數

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

llms:generate:llms-full(event, options, contents)

此鉤子在每次請求 /llms-full.txt 時被呼叫。它允許您以任何格式新增自定義內容章節。

引數

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

在您的應用程式中使用鉤子

在您的 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 模組中使用鉤子

如果您正在開發需要擴充套件 LLM 文件的 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 內建了對 LLM 文件的支援。您可以將 nuxt-llms@nuxt/content 結合使用,高效地為您的網站編寫內容和文件,並輕鬆生成 LLM 友好的文件。Content 模組使用 nuxt-llms 鉤子,並自動將您的所有內容新增到 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 LLM 文件 以獲取有關如何使用 nuxt-llms@nuxt/content 自定義 LLM 內容的更多資訊。

💻 開發

  • 克隆倉庫
  • 使用 pnpm install 安裝依賴
  • 使用 pnpm dev:prepare 準備
  • 使用 pnpm prepack 構建
  • 使用 pnpm dev 嘗試演練場
  • 使用 pnpm test 測試

許可證

麻省理工學院許可證

版權所有 (c) NuxtLabs