Nuxt LLMs 会自动为您的 Nuxt 应用程序生成 llms.txt markdown 文档。它提供运行时钩子,用于从各种来源(CMS、Nuxt Content 等)收集数据,并以文本格式生成结构化文档。
/llms.txt/llms-full.txtnuxt.config.ts 定制部分npm i nuxt-llms
nuxt.config.ts 中注册 nuxt-llmsexport default defineNuxtConfig({
modules: ['nuxt-llms']
})
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(必填):链接的 hrefnotes:文档的备注。备注是一个特殊的部分,始终出现在文档的末尾。备注对于添加有关应用程序或文档本身的任何信息非常有用。full:llms-full.txt 配置。设置此选项将启用 llms-full.txt 路由。title:llms-full 文档的标题description:llms-full 文档的描述该模块生成两种不同的文档格式
/llms.txt 路由生成一份简洁、结构化的文档,遵循 llms.txt 规范。此格式针对人类可读性和 AI 消费进行了优化。它包括
/llms-full.txt 路由提供一种更详细、自由形式的文档格式。这有助于减少应用程序上的爬虫流量,并为您的用户和 LLM 提供更详细的文档。
默认情况下,模块不会生成 llms-full.txt 路由,您需要通过在 nuxt.config.ts 中设置 full.title 和 full.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>
\`\`\`
`)
})
})
如果您正在开发需要扩展 LLMs 文档的 Nuxt 模块
// 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: [/* ... */]
})
})
})
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 ^3.2.0 内置支持 LLMs 文档。您可以将 nuxt-llms 与 @nuxt/content 结合使用,高效地为您的网站编写内容和文档,并轻松生成 LLM 友好的文档。Content 模块使用 nuxt-llms 钩子,并自动将所有内容添加到 llms.txt 和 llms-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