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