Ruby on Rails 中的 llms.txt

Rails 会通过网站根目录提供 public/ 文件夹。请将 llms.txt 添加到该文件夹;如果内容是动态的,也可以通过控制器生成。

最近更新:

两种方法

Rails 可以提供 llms.txt 两种方式:放在 public/ (最简单), 或使用从数据构建文件的控制器操作。除非内容必须反映实时记录,否则请使用静态文件。

方法 1:public/ 中的静态文件

位于 public/ 会在域名根目录提供。位于 public/llms.txt 会解析为 /llms.txt.

  1. 创建文件: touch public/llms.txt
  2. 编写你的内容(参见 操作指南):
# Your App Name

> One-sentence description of your app for LLM context.

## Core pages

- [Home](https://yourdomain.com/): what this app does.
- [Pricing](https://yourdomain.com/pricing/): plans and limits.
- [Docs](https://yourdomain.com/docs/): developer documentation.

方法 2:控制器操作

要动态生成该文件,请添加一个返回纯文本的路由和控制器:

# config/routes.rb
get "/llms.txt", to: "llms#show"

# app/controllers/llms_controller.rb
class LlmsController < ApplicationController
  def show
    pages = Page.published.order(:title) # example data source
    body = "# Your App Name\n\n> Description of your app.\n\n## Core pages\n\n"
    body += pages.map { |p| "- [#{p.title}](#{page_url(p)}): #{p.summary}" }.join("\n")
    render plain: body, content_type: "text/plain"
  end
end

在生产环境提供静态文件

验证设置

curl -sI https://yourdomain.com/llms.txt | grep -i content-type
# expect: content-type: text/plain

改用 Laravel 吗?请参阅 Laravel 指南。如需更完整地 导出页面内容,请添加 llms-full.txt.

来源