Integrations
LLMs 模块
学习如何使用 Nuxt Content 和 Nuxt LLMs 模块生成适用于人工智能的内容文件。
Nuxt Content 模块集成了 nuxt-llms,用于为大型语言模型 (LLMs) 准备内容。当检测到 nuxt-llms 时,Content 模块会自动扩展 LLMs 模块,并将类型为 page 的集合注入到 LLMs 模块中。🚀
设置
安装所需模块
terminal
npm install nuxt-llms
配置你的 nuxt.config.ts
nuxt.config.ts
export default defineNuxtConfig({
modules: ['@nuxt/content', 'nuxt-llms'],
llms: {
domain: 'https://your-site.com',
title: '你的站点名称',
description: '你站点的简要描述',
},
})
就是这么简单 🚀 /llms.txt 文件会被自动生成并预渲染。
Raw markdown 访问
启用 nuxt-llms 后,Nuxt Content 还会暴露一个原始 markdown 端点,使你可以直接流式获取适用于 LLM 的源文件,而无需通过完整的渲染管道。
- 端点:
/raw/<content-path>.md(路径与页面 URL 相同,去除末尾的/index,保留.md扩展名),返回text/markdown; charset=utf-8。 - 范围:仅包含
page集合;可通过llms.contentRawMarkdown.excludeCollections排除特定集合。设置llms.contentRawMarkdown = false可完全禁用该端点。 - 输出:如果请求的文档缺少顶级标题或描述,路由会在返回前将标题和描述附加到 markdown 内容体之前。
- llms.txt 链接:
llms.txt中生成的文档链接会自动重写为/raw/...md端点(除非集合被排除或功能关闭),这样代理可以获取精简的 markdown,而非完整 HTML,减少 tokens 使用并提升响应速度。通过llms.contentRawMarkdown.rewriteLLMSTxt控制(默认启用true)。
nuxt.config.ts
export default defineNuxtConfig({
modules: ['@nuxt/content', 'nuxt-llms'],
llms: {
contentRawMarkdown: {
// 可选:防止暴露特定 page 集合
excludeCollections: ['blog'],
// 可选:保持 llms.txt 链接指向渲染页面
rewriteLLMSTxt: false,
},
},
})
分区
在生成内容时,你可以创建自定义分区,将内容处理为适合 LLM 使用的格式。
你可以向 llms.sections 数组中添加自定义分区,并为每个分区定义 contentCollection 和 contentFilters 选项。
如果在
contentCollection 选项中未定义分区,模块将仅把 page 集合添加到 LLMs 模块。contentCollection
此选项指定要使用哪个内容集合作为来源。
nuxt.config.ts
export default defineNuxtConfig({
llms: {
sections: [
{
title: '文档',
description: '技术文档和指南',
contentCollection: 'docs',
},
],
},
})
contentFilters
此选项定义用于选择集合中特定内容的过滤器。
你可以精确控制包含哪些内容。每个过滤器包含:
field:要检查的内容属性operator:比较运算符(=、<>、>、<、LIKE、IN、NOT IN、IS NULL、IS NOT NULL等)value:比较的值
nuxt.config.ts
export default defineNuxtConfig({
llms: {
sections: [
{
title: '文档',
description: '技术文档和指南',
contentCollection: 'docs',
contentFilters: [
// 只包含 Markdown 文件
{ field: 'extension', operator: '=', value: 'md' },
// 只包含已发布的内容
{ field: 'draft', operator: '<>', value: true },
// 按目录过滤
{ field: 'path', operator: 'LIKE', value: '/guide%' },
]
},
],
},
})