使用Python给自己网站生成llms.txt

本文提供了一个通过CLI和Python API来解析文件并从中创建llms.txt。输入文件应遵循以下格式:

markdown 复制代码
# FastHTML

> FastHTML is a python library which...

When writing FastHTML apps remember to:

- Thing to remember

## Docs

- [Surreal](https://host/README.md): Tiny jQuery alternative with Locality of Behavior
- [FastHTML quick start](https://host/quickstart.html.md): An overview of FastHTML features

## Examples

- [Todo app](https://host/adv_app.py)

## Optional

- [Starlette docs](https://host/starlette-sml.md): A subset of the Starlette docs

安装

复制代码
pip install llms-txt

如何使用

CLI

安装后,llms_txt2ctx在您的终端中可用。

要获取CLI的帮助:

复制代码
llms_txt2ctx -h

要将llms.txt文件到XML上下文并保存到llms.md:

复制代码
llms_txt2ctx llms.txt > llms.md

通过--optional True添加输入文件的 "选项" 部分。

Python模块

javascript 复制代码
from llms_txt import *
ini 复制代码
samp = Path('llms-sample.txt').read_text()

使用parse_llms_file使用llms.txt文件的部分创建数据结构 (还可以添加optional=True如果需要):

scss 复制代码
parsed = parse_llms_file(samp)
list(parsed)
css 复制代码
['title', 'summary', 'info', 'sections']
复制代码
parsed.title,parsed.summary
vbnet 复制代码
('FastHTML',
 'FastHTML is a python library which brings together Starlette, Uvicorn, HTMX, and fastcore's `FT` "FastTags" into a library for creating server-rendered hypermedia applications.')
scss 复制代码
list(parsed.sections)
css 复制代码
['Docs', 'Examples', 'Optional']
css 复制代码
parsed.sections.Optional[0]
rust 复制代码
{ 'desc': 'A subset of the Starlette documentation useful for FastHTML '
          'development.',
  'title': 'Starlette full documentation',
  'url': 'https://gist.githubusercontent.com/jph00/809e4a4808d4510be0e3dc9565e9cbd3/raw/9b717589ca44cedc8aaf00b2b8cacef922964c0f/starlette-sml.md'}

使用create_ctx创建一个包含XML部分的LLM上下文文件,适用于Claude等系统 (这是CLI在幕后调用的)。

ini 复制代码
ctx = create_ctx(samp)
scss 复制代码
print(ctx[:300])
vbnet 复制代码
<project title="FastHTML" summary='FastHTML is a python library which brings together Starlette, Uvicorn, HTMX, and fastcore&#39;s `FT` "FastTags" into a library for creating server-rendered hypermedia applications.'>
Remember:

- Use `serve()` for running uvicorn (`if __name__ == "__main__"` is not

测试和部署

显示解析有多简单llms.txt文件,这里是一个完整的解析器,在 <20行代码中,没有依赖关系:

python 复制代码
from pathlib import Path
import re,itertools

def chunked(it, chunk_sz):
    it = iter(it)
    return iter(lambda: list(itertools.islice(it, chunk_sz)), [])

def parse_llms_txt(txt):
    "Parse llms.txt file contents in `txt` to a `dict`"
    def _p(links):
        link_pat = '-\s*[(?P<title>[^]]+)]((?P<url>[^)]+))(?::\s*(?P<desc>.*))?'
        return [re.search(link_pat, l).groupdict()
                for l in re.split(r'\n+', links.strip()) if l.strip()]

    start,*rest = re.split(fr'^##\s*(.*?$)', txt, flags=re.MULTILINE)
    sects = {k: _p(v) for k,v in dict(chunked(rest, 2)).items()}
    pat = '^#\s*(?P<title>.+?$)\n+(?:^>\s*(?P<summary>.+?$)$)?\n+(?P<info>.*)'
    d = re.search(pat, start.strip(), (re.MULTILINE|re.DOTALL)).groupdict()
    d['sections'] = sects
    return d

我们提供了一个测试套件tests/test-parse.py并确认此实现通过了所有测试。

相关推荐
雪雪9087 小时前
视频音乐升降调工具怎么选?先看音调适配与音质损失
人工智能
码事漫谈7 小时前
五道题,测测你有没有真的入门 AI
后端
加强洁西卡7 小时前
【AI】机器学习常见的指标及其解释
人工智能·机器学习
Qyr997 小时前
2026-2032年全球高可靠性电阻器市场发展全景与竞争格局解析
人工智能·物联网
红海云7 小时前
向机制要活力,以数字促转型:国企“三能”改革深水区攻坚策略
人工智能
weixin_448119947 小时前
Datawhale Easy Data × AI:构建知识与记忆驱动的 Agent笔记8
人工智能·笔记·react.js
Fibocom广和通7 小时前
IMC 2026|从连接到规模部署,广和通深化印度IoT市场布局
人工智能
蜗牛互联网7 小时前
Python + JSON Schema 实现工单结构化输出与本地复核
java·人工智能·后端
华微软件7 小时前
华微软件 AI 领域新增一项国家发明专利
人工智能
jimmyleeee8 小时前
大模型安全之四十五:从数据到输出----GenAI 版权、知识产权与伦理合规实战指南
人工智能·深度学习·安全