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(必填):链接的 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 模块中使用钩子

如果您正在开发需要扩展 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 钩子,并自动将所有内容添加到 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-llms@nuxt/content 自定义 LLMs 内容的更多信息,请查看 Nuxt Content llms 文档

💻 开发

  • 克隆仓库
  • 使用 pnpm install 安装依赖
  • 使用 pnpm dev:prepare 进行准备
  • 使用 pnpm prepack 进行构建
  • 使用 pnpm dev 尝试演练场
  • 使用 pnpm test 进行测试

许可证

麻省理工学院许可证

版权所有 (c) NuxtLabs